-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Use sort_by_cached_key where appropriate #49525
Conversation
Your PR failed on Travis. Through arcane magic we have determined that the following fragments from the build log may contain information about the problem. Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
a89ce71
to
51a10da
Compare
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.
Nice to see wins from this!
src/librustc_resolve/lib.rs
Outdated
@@ -3577,7 +3576,7 @@ impl<'a> Resolver<'a> { | |||
|
|||
let name = path[path.len() - 1].node.name; | |||
// Make sure error reporting is deterministic. | |||
names.sort_by_key(|name| name.as_str()); | |||
names.sort_by_cached_key(|name| name.as_str()); |
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.
.as_str()
is typically -> &str
and thus cheap, so maybe double-check this one.
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.
This method is coming from:
rust/src/libsyntax_pos/symbol.rs
Lines 113 to 119 in e38eca6
pub fn as_str(self) -> InternedString { | |
with_interner(|interner| unsafe { | |
InternedString { | |
string: ::std::mem::transmute::<&str, &str>(interner.get(self)) | |
} | |
}) | |
} |
which seemed non-trivial (but perhaps it can be optimised quite effectively?).
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.
Hmm, it's probably fairly cheap, but might be worth caching it anyway so the sort code is easier for the compiler to deal with. Thanks for resolving the type :)
src/librustc_trans/base.rs
Outdated
@@ -825,7 +825,7 @@ pub fn trans_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, | |||
// a bit more efficiently. | |||
let codegen_units = { | |||
let mut codegen_units = codegen_units; | |||
codegen_units.sort_by_key(|cgu| usize::MAX - cgu.size_estimate()); | |||
codegen_units.sort_by_cached_key(|cgu| usize::MAX - cgu.size_estimate()); |
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.
This sure looks like it wants |cgu| cmp::Reverse(cgu::size_estimate())
, though I don't know if that's this PR
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.
Oh, that's a good point; I may as well fix that one here in passing.
@@ -799,7 +799,7 @@ impl<'a, 'gcx, 'tcx> ProbeContext<'a, 'gcx, 'tcx> { | |||
.collect(); | |||
|
|||
// sort them by the name so we have a stable result | |||
names.sort_by_key(|n| n.as_str()); | |||
names.sort_by_cached_key(|n| n.as_str()); |
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.
Same as_str
comment
@@ -1435,9 +1435,7 @@ impl<'a, 'tcx, 'rcx> AutoTraitFinder<'a, 'tcx, 'rcx> { | |||
// involved (impls rarely have more than a few bounds) means that it | |||
// shouldn't matter in practice. |
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.
Also probably not this PR, but this comment makes me wonder if it would be worth adding if len < 2 { return }
to the checks excerpted below, since sort_by_cached_key is currently a pessimization in the len==1 case, as it'll compute the key and allocate, which ordinary sort_by_key wouldn't.
Lines 1402 to 1406 in 1c5283b
let len = self.len(); | |
if sz_u8 < sz_u16 && len <= ( u8::MAX as usize) { return sort_by_key!( u8, self, f) } | |
if sz_u16 < sz_u32 && len <= (u16::MAX as usize) { return sort_by_key!(u16, self, f) } | |
if sz_u32 < sz_usize && len <= (u32::MAX as usize) { return sort_by_key!(u32, self, f) } | |
sort_by_key!(usize, self, f) |
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.
That's another good point: I'll add that extra check in! (len == 2
could also be special cased to not allocate, as only one comparison will be executed, but that's a less likely case to encounter.)
Your PR failed on Travis. Through arcane magic we have determined that the following fragments from the build log may contain information about the problem. Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
24c8f8f
to
af6027c
Compare
Your PR failed on Travis. Through arcane magic we have determined that the following fragments from the build log may contain information about the problem. Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
Can you also use rust/src/librustc_mir/monomorphize/partitioning.rs Lines 192 to 196 in 9ceaa56
This could just be let mut items: Vec<_> = self.items().iter().map(|(&i, &l)| (i, l)).collect();
items.sort_by_cached_key(|&(i, _)| item_sort_key(tcx, i));
items |
@sinkuu: Good catch, thanks! |
☔ The latest upstream changes (presumably #49661) made this pull request unmergeable. Please resolve the merge conflicts. |
df5520c
to
e7aa139
Compare
} | ||
}); | ||
// The sort doesn't case-fold but it's doubtful we care. | ||
lints.sort_by_cached_key(|x: &&Lint| (x.default_level(sess), x.name)); |
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.
oh cute!
@bors r=scottmcm |
📌 Commit e7aa139 has been approved by |
…n, r=scottmcm Use sort_by_cached_key where appropriate A follow-up to rust-lang#48639, converting various slice sorting calls to `sort_by_cached_key` when the key functions are more expensive.
Rollup of 14 pull requests Successful merges: - #49525 (Use sort_by_cached_key where appropriate) - #49575 (Stabilize `Option::filter`.) - #49614 (in which the non-shorthand patterns lint keeps its own counsel in macros) - #49665 (Small nits to make couple of tests pass on mips targets.) - #49781 (add regression test for #16223 (NLL): use of collaterally moved value) - #49795 (Properly look for uninhabitedness of variants in niche-filling check) - #49809 (Stop emitting color codes on TERM=dumb) - #49856 (Do not uppercase-lint #[no_mangle] statics) - #49863 (fixed typo) - #49857 (Fix "fp" target feature for AArch64) - #49849 (Add --enable-debug flag to musl CI build script) - #49734 (proc_macro: Generalize `FromIterator` impl) - #49730 (Fix ICE with impl Trait) - #48270 (Replace `structurally_resolved_type` in casts check.) Failed merges:
A follow-up to #48639, converting various slice sorting calls to
sort_by_cached_key
when the key functions are more expensive.