Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate better code for "large" vec![] macro invocations #17857

Closed
wants to merge 1 commit into from

Conversation

dotdash
Copy link
Contributor

@dotdash dotdash commented Oct 7, 2014

Even when optimized, something like vec![1i,2,3,4,5,6,7,8,9,10] will
allocate three times, because LLVM can only drop the call based on the
observed size of the previous allocation. Using a simple macro to count
the number of elements, we can initialize the vector with the correct
size and avoid reallocations.

Since LLVM already does constant folding for us, the new count!() macro
results in a single integer constant even if building without
optimizations, so we don't end up with an awful lot of additions in the
generated code.

Even when optimized, something like vec![1i,2,3,4,5,6,7,8,9,10] will
allocate three times, because LLVM can only drop the call based on the
observed size of the previous allocation. Using a simple macro to count
the number of elements, we can initialize the vector with the correct
size and avoid reallocations.

Since LLVM already does constant folding for us, the new count!() macro
results in a single integer constant even if building without
optimizations, so we don't end up with an awful lot of additions in the
generated code.
@jfager
Copy link
Contributor

jfager commented Oct 7, 2014

This has come up before, see #16204. There was pushback on the basis that counting should be a proper macro feature, not something that spits out 1+1+1...

@dotdash dotdash closed this Oct 7, 2014
@thestinger thestinger reopened this Oct 8, 2014
@thestinger
Copy link
Contributor

@dotdash: Can you think of a way to hide this as an implementation detail (no new macro export)? As long as it's an internal implementation detail then it's fine.

@thestinger thestinger closed this Oct 8, 2014
@dotdash dotdash deleted the vec_opt branch July 13, 2016 07:38
lnicola pushed a commit to lnicola/rust that referenced this pull request Aug 29, 2024
…p, r=Veykril

feat: Allow declaring cfg groups in rust-project.json, to help sharing common cfgs

Closes rust-lang#17815.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants