-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Sort mono items by symbol name #145358
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
base: master
Are you sure you want to change the base?
Sort mono items by symbol name #145358
Conversation
r? @nnethercote rustbot has assigned @nnethercote. Use |
This comment has been minimized.
This comment has been minimized.
@bors2 try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Sort mono items by symbol name
This comment has been minimized.
This comment has been minimized.
// use it. | ||
// However, it seems that actually moving related things (probably mainly different | ||
// monomorphizations of the same item) close to one another is actually beneficial for | ||
// perf. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you clarify how symbol_name
puts related things together? The reasoning is not clear to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this is just a hunch, it's quite possible that it doesn't have any effect or that the perf. results are just a flake. But my reasoning goes something like this:
- LLVM codegens statics and functions in the order we pass them to it.
- If LLVM codegens things that are related in succession, similar data structures will be accessed repeatedly, because the codegened functions will be similar/related (especially due to monomorphization), and thus memory cache utilization could be better, and the branch predictor will also see similar things "in a row", thus helping branch prediction.
Suppose that we codegen a, c::<u32>, b, c::<i16>, d, c::<bool>
. My expectation is that it is beneficial to codegen the three monomorphizations of c
right after one another, which should be achieved by sorting by symbol name. I strongly suspect that this sort does put monomorphized things next to one another, but I haven't actually checked whether that wasn't already the case before the sort though (I'll check).
CC @nikic if you have any thoughts about the fact that LLVM codegenning items in a specific order seems to improve runtime perf. statistics (less cycle counts/cache misses/branch misses).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. This would be good information for a comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or it can be something else entirely, e.g. symbol_name
is computed later anyway for the individual mono items, and it helps perf. if we precompute the symbol names at the same time and cache them 🤷♂️ I will try to investigate more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok I am now more confident that this is indeed what happens. The wins are in LLVM codegen (https://perf.rust-lang.org/detailed-query.html?commit=938563cd053ec624756cb9f5284a22631f9a413b&benchmark=cargo-0.87.1-debug&scenario=full&base_commit=350d0ef0ec0493e6d21cfb265cb8211a0e74d766), and when I tried to print the symbols before and after sort, it's clear that related items are not always next to each other in the original order.
Here is a log of symbols when building cargo
, which had the biggest wins in rustc-perf:
cargo.tar.gz
It's clear that the sorting groups related/monomorphized things next to each other, which likely improves LLVM codegen performance.
I will expand the comment.
Finished benchmarking commit (938563c): comparison URL. Overall result: ❌✅ regressions and improvements - please read the text belowBenchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf. Next Steps: If you can justify the regressions found in this try perf run, please do so in sufficient writing along with @bors rollup=never Instruction countOur most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.
Max RSS (memory usage)Results (primary -2.5%, secondary -1.1%)A less reliable metric. May be of interest, but not used to determine the overall result above.
CyclesResults (primary -3.4%, secondary -3.6%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeResults (primary 0.0%, secondary 0.0%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Bootstrap: 468.291s -> 464.071s (-0.90%) |
The increase in instruction count is due to the extra sorting. |
f5481f1
to
6608a17
Compare
Added more details to the comment. |
6608a17
to
6b8b875
Compare
// monomorphizations of the same function) close to one another is actually beneficial | ||
// for LLVM performance. | ||
// LLVM will codegen the items in the order we pass them to it, and when it handles | ||
// similar things in succession, it seems that it leads to better cache utilization, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you put the a, c::<u32>, b, c::<i16>, d, c::<bool>
example in here? I think that makes it clearer what "similar things" are, which is still a bit vague without the example. Because in the absence of generics alphabetical order would be no better than random, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that even without monomorphizations it sort of makes sense, things that have a similar symbol typically live in a similar module, or they are methods on the same type (unless the symbol is heavily mangled or really random), so they will e.g. reference similar types, call similar functions etc. But the monomorphization example is indeed the best one, I added it.
r=me with the comment tweak above. |
6b8b875
to
6f1c998
Compare
@bors r=nnethercote |
Trying to claw back cycles/branch/cache miss losses from #144722.