Skip to content

Commit

Permalink
Auto merge of #55821 - ljedrz:cached_key_sorts, r=<try>
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Nov 15, 2018
2 parents 9649c1f + 1649c2e commit a22afbf
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/librustc/middle/resolve_lifetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));

for def_id in def_ids {
debug!(
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/traits/object_safety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,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));

let projection_predicates = associated_types.into_iter().map(|item| {
ty::ExistentialPredicate::Projection(ty::ExistentialProjection {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/borrow_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());

if tcx.migrate_borrowck() {
// When borrowck=migrate, check if AST-borrowck would
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_mir/monomorphize/partitioning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());

// Merge the two smallest codegen units until the target size is reached.
while codegen_units.len() > target_cgu_count {
Expand Down Expand Up @@ -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());
cgus.dedup();
for &(ref cgu_name, (linkage, _)) in cgus.iter() {
output.push_str(" ");
Expand Down

0 comments on commit a22afbf

Please sign in to comment.