-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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 when the key function is not trivial/free #55821
Conversation
r? @cramertj (rust_highfive has picked a reviewer for you, use r? to override) |
Use sort_by_cached_key when the key function is not trivial/free I'm not 100% sure about `def_path_hash` (everything it does is inlined) but it seems like a good idea at least for the rest, as they are cloning.
☀️ Test successful - status-travis |
@@ -585,7 +585,7 @@ fn merge_codegen_units<'tcx>(tcx: TyCtxt<'_, 'tcx, 'tcx>, | |||
// smallest into each other) we're sure to start off with a deterministic | |||
// order (sorted by name). This'll mean that if two cgus have the same size | |||
// the stable sort below will keep everything nice and deterministic. | |||
codegen_units.sort_by_key(|cgu| cgu.name().clone()); | |||
codegen_units.sort_by_cached_key(|cgu| cgu.name().clone()); |
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't it be sort_by(|a, b| a.name().cmp(b.name())) to avoid heap allocations completely?
name()
returns a ref to InternedString
which is Copy
, so this clone
is free.
@rust-timer build a22afbf |
Success: Queued a22afbf with parent 9649c1f, comparison URL. |
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.
Let's see what perf says. I think that the primary_span
and def_path_hash
calls are likely not a win. Not sure about the others.
@@ -1573,7 +1573,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { | |||
.collect(); | |||
|
|||
// ensure that we issue lints in a repeatable order | |||
def_ids.sort_by_key(|&def_id| self.tcx.def_path_hash(def_id)); | |||
def_ids.sort_by_cached_key(|&def_id| self.tcx.def_path_hash(def_id)); |
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 you noted, I think this is already cached -- it's something we do a lot in incremental. You agree, @michaelwoerister ?
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.
It's cached, yes, but still not entirely trivial. I.e. it has to do the local/upstream crate dispatch and for upstream DefIds
it has to go through a number of function calls:
rust/src/librustc/ty/context.rs
Line 1325 in 780658a
pub fn def_path_hash(self, def_id: DefId) -> hir_map::DefPathHash { |
So, I can imagine this being a win for sorting where each key might be compared against multiple times.
@@ -408,7 +408,7 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> { | |||
.collect::<Vec<_>>(); | |||
|
|||
// existential predicates need to be in a specific order | |||
associated_types.sort_by_key(|item| self.def_path_hash(item.def_id)); | |||
associated_types.sort_by_cached_key(|item| self.def_path_hash(item.def_id)); |
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 here, of course
src/librustc_mir/borrow_check/mod.rs
Outdated
@@ -341,7 +341,7 @@ fn do_mir_borrowck<'a, 'gcx, 'tcx>( | |||
} | |||
|
|||
if !mbcx.errors_buffer.is_empty() { | |||
mbcx.errors_buffer.sort_by_key(|diag| diag.span.primary_span()); | |||
mbcx.errors_buffer.sort_by_cached_key(|diag| diag.span.primary_span()); |
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 is the 'error reporting' path, so it really doesn't matter... also primary_span
is a very simple operation... doesn't strike me as worthwhile.
@@ -985,7 +985,7 @@ fn collect_and_partition_mono_items<'a, 'tcx>( | |||
output.push_str(" @@"); | |||
let mut empty = Vec::new(); | |||
let cgus = item_to_cgus.get_mut(i).unwrap_or(&mut empty); | |||
cgus.as_mut_slice().sort_by_key(|&(ref name, _)| name.clone()); | |||
cgus.as_mut_slice().sort_by_cached_key(|&(ref name, _)| name.clone()); |
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 might make sense. Not sure what type name
has here... :)
Finished benchmarking try commit a22afbf |
Looks like a win:
I can update the commit taking into account the comments - theoretically it could make the wins even nicer. |
1649c2e
to
df95268
Compare
Not much was left after including the remarks ^^. We might want to run another round of perf to make sure we didn't lose the gains reported by the first run. |
Yeah, we should try again. I'm a bit surprised to see a 3% swing, I have to admit. |
@bors try |
⌛ Trying commit df9526808c94b90bec3599bcd25ecb089ab934c5 with merge c992107af31574b1105b4058931d9e371cfaf8ad... |
☀️ Test successful - status-travis |
The only change left at this point is in debugging code that is only executed by the test suite -- so I expect no wins there. The original changes (except for the |
@rust-timer build c992107af31574b1105b4058931d9e371cfaf8ad |
Success: Queued c992107af31574b1105b4058931d9e371cfaf8ad with parent 3991bfb, comparison URL. |
Finished benchmarking try commit c992107af31574b1105b4058931d9e371cfaf8ad |
df95268
to
d4a6e73
Compare
Looks like @michaelwoerister was right - the original gains were lost. I've restored the earlier changes without the |
lgtm: @bors r+ |
📌 Commit d4a6e73 has been approved by |
…oerister Use sort_by_cached_key when the key function is not trivial/free I'm not 100% sure about `def_path_hash` (everything it does is inlined) but it seems like a good idea at least for the rest, as they are cloning.
…oerister Use sort_by_cached_key when the key function is not trivial/free I'm not 100% sure about `def_path_hash` (everything it does is inlined) but it seems like a good idea at least for the rest, as they are cloning.
…oerister Use sort_by_cached_key when the key function is not trivial/free I'm not 100% sure about `def_path_hash` (everything it does is inlined) but it seems like a good idea at least for the rest, as they are cloning.
…oerister Use sort_by_cached_key when the key function is not trivial/free I'm not 100% sure about `def_path_hash` (everything it does is inlined) but it seems like a good idea at least for the rest, as they are cloning.
…oerister Use sort_by_cached_key when the key function is not trivial/free I'm not 100% sure about `def_path_hash` (everything it does is inlined) but it seems like a good idea at least for the rest, as they are cloning.
Rollup of 19 pull requests Successful merges: - #55011 (Add libstd Cargo feature "panic_immediate_abort") - #55821 (Use sort_by_cached_key when the key function is not trivial/free) - #56014 (add test for issue #21335) - #56131 (Assorted tweaks) - #56214 (Implement chalk unification routines) - #56216 (Add TryFrom<&[T]> for [T; $N] where T: Copy) - #56268 (Reuse the `P` in `InvocationCollector::fold_{,opt_}expr`.) - #56324 (Use raw_entry for more efficient interning) - #56336 (Clean up and streamline the pretty-printer) - #56337 (Fix const_fn ICE with non-const function pointer) - #56339 (Remove not used option) - #56341 (Rename conversion util; remove duplicate util in librustc_codegen_llvm.) - #56349 (rustc 1.30.0's linker flavor inference is a non-backwards compat change to -Clinker) - #56355 (Add inline attributes and add unit to CommonTypes) - #56360 (Optimize local linkchecker program) - #56364 (Fix panic with outlives in existential type) - #56365 (Stabilize self_struct_ctor feature.) - #56367 (Moved some feature gate tests to correct location) - #56373 (Update books)
I'm not 100% sure about
def_path_hash
(everything it does is inlined) but it seems like a good idea at least for the rest, as they are cloning.