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

Use sort_by_cached_key when the key function is not trivial/free #55821

Merged
merged 1 commit into from
Nov 30, 2018

Conversation

ljedrz
Copy link
Contributor

@ljedrz ljedrz commented Nov 9, 2018

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.

@rust-highfive
Copy link
Collaborator

r? @cramertj

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 9, 2018
@Mark-Simulacrum
Copy link
Member

@bors try for perf

r? @eddyb

@rust-highfive rust-highfive assigned eddyb and unassigned cramertj Nov 15, 2018
@bors
Copy link
Contributor

bors commented Nov 15, 2018

⌛ Trying commit 1649c2e with merge a22afbf...

bors added a commit that referenced this pull request Nov 15, 2018
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.
@eddyb
Copy link
Member

eddyb commented Nov 15, 2018

r? @nikomatsakis

@rust-highfive rust-highfive assigned nikomatsakis and unassigned eddyb Nov 15, 2018
@bors
Copy link
Contributor

bors commented Nov 15, 2018

☀️ Test successful - status-travis
State: approved= try=True

@@ -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());
Copy link
Contributor

@sinkuu sinkuu Nov 16, 2018

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.

@nikomatsakis
Copy link
Contributor

@rust-timer build a22afbf

@rust-timer
Copy link
Collaborator

Success: Queued a22afbf with parent 9649c1f, comparison URL.

Copy link
Contributor

@nikomatsakis nikomatsakis left a 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));
Copy link
Contributor

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 ?

Copy link
Member

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:

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));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also here, of course

@@ -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());
Copy link
Contributor

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());
Copy link
Contributor

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... :)

@rust-timer
Copy link
Collaborator

Finished benchmarking try commit a22afbf

@ljedrz
Copy link
Contributor Author

ljedrz commented Nov 17, 2018

Looks like a win:

inflate-check
	avg: -2.4%	min: -3.3%	max: 0.3%
keccak-check
	avg: -2.1%	min: -3.1%	max: 0.5%
keccak-opt
	avg: -1.6%	min: -2.7%	max: 0.4%
keccak-debug
	avg: -1.5%	min: -2.5%	max: 0.3%
inflate-debug
	avg: -1.6%	min: -2.3%	max: 0.3%

I can update the commit taking into account the comments - theoretically it could make the wins even nicer.

@ljedrz
Copy link
Contributor Author

ljedrz commented Nov 17, 2018

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.

@nikomatsakis
Copy link
Contributor

Yeah, we should try again. I'm a bit surprised to see a 3% swing, I have to admit.

@nikomatsakis
Copy link
Contributor

@bors try

@bors
Copy link
Contributor

bors commented Nov 20, 2018

⌛ Trying commit df9526808c94b90bec3599bcd25ecb089ab934c5 with merge c992107af31574b1105b4058931d9e371cfaf8ad...

@bors
Copy link
Contributor

bors commented Nov 20, 2018

☀️ Test successful - status-travis
State: approved= try=True

@michaelwoerister
Copy link
Member

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 cgu.name().clone()) looked fine to me though.

@nikomatsakis
Copy link
Contributor

@rust-timer build c992107af31574b1105b4058931d9e371cfaf8ad

@rust-timer
Copy link
Collaborator

Success: Queued c992107af31574b1105b4058931d9e371cfaf8ad with parent 3991bfb, comparison URL.

@rust-timer
Copy link
Collaborator

Finished benchmarking try commit c992107af31574b1105b4058931d9e371cfaf8ad

@ljedrz
Copy link
Contributor Author

ljedrz commented Nov 27, 2018

Looks like @michaelwoerister was right - the original gains were lost.

I've restored the earlier changes without the Copy (that's free) and primary_span (in error reporting and pretty trivial). This should bring back the greens.

@michaelwoerister
Copy link
Member

lgtm: @bors r+

@bors
Copy link
Contributor

bors commented Nov 27, 2018

📌 Commit d4a6e73 has been approved by michaelwoerister

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 27, 2018
kennytm added a commit to kennytm/rust that referenced this pull request Nov 27, 2018
…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.
pietroalbini added a commit to pietroalbini/rust that referenced this pull request Nov 28, 2018
…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.
pietroalbini added a commit to pietroalbini/rust that referenced this pull request Nov 29, 2018
…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.
kennytm added a commit to kennytm/rust that referenced this pull request Nov 30, 2018
…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.
kennytm added a commit to kennytm/rust that referenced this pull request Nov 30, 2018
…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.
bors added a commit that referenced this pull request Nov 30, 2018
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)
@bors bors merged commit d4a6e73 into rust-lang:master Nov 30, 2018
@ljedrz ljedrz deleted the cached_key_sorts branch November 30, 2018 22:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants