Skip to content

Commit b520c42

Browse files
committed
Prefer sort_unstable*() over sort*()
1 parent 6a3db03 commit b520c42

File tree

41 files changed

+68
-68
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+68
-68
lines changed

src/librustc/hir/lowering.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4803,6 +4803,6 @@ fn body_ids(bodies: &BTreeMap<hir::BodyId, hir::Body>) -> Vec<hir::BodyId> {
48034803
// Sorting by span ensures that we get things in order within a
48044804
// file, and also puts the files in a sensible order.
48054805
let mut body_ids: Vec<_> = bodies.keys().cloned().collect();
4806-
body_ids.sort_by_key(|b| bodies[b].value.span);
4806+
body_ids.sort_unstable_by_key(|b| bodies[b].value.span);
48074807
body_ids
48084808
}

src/librustc/hir/pat_util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ impl hir::Pat {
157157
}
158158
true
159159
});
160-
variants.sort();
160+
variants.sort_unstable();
161161
variants.dedup();
162162
variants
163163
}

src/librustc/infer/error_reporting/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
434434
};
435435

436436
// sort the errors by span, for better error message stability.
437-
errors.sort_by_key(|u| match *u {
437+
errors.sort_unstable_by_key(|u| match *u {
438438
RegionResolutionError::ConcreteFailure(ref sro, _, _) => sro.span(),
439439
RegionResolutionError::GenericBoundFailure(ref sro, _, _) => sro.span(),
440440
RegionResolutionError::SubSupConflict(ref rvo, _, _, _, _) => rvo.span(),

src/librustc/infer/lexical_region_resolve/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -568,8 +568,8 @@ impl<'cx, 'gcx, 'tcx> LexicalResolver<'cx, 'gcx, 'tcx> {
568568
_ => 2,
569569
}
570570
}
571-
lower_bounds.sort_by_key(region_order_key);
572-
upper_bounds.sort_by_key(region_order_key);
571+
lower_bounds.sort_unstable_by_key(region_order_key);
572+
upper_bounds.sort_unstable_by_key(region_order_key);
573573

574574
for lower_bound in &lower_bounds {
575575
for upper_bound in &upper_bounds {

src/librustc/middle/resolve_lifetime.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1426,7 +1426,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
14261426
.collect();
14271427

14281428
// ensure that we issue lints in a repeatable order
1429-
def_ids.sort_by_key(|&def_id| self.tcx.def_path_hash(def_id));
1429+
def_ids.sort_unstable_by_key(|&def_id| self.tcx.def_path_hash(def_id));
14301430

14311431
for def_id in def_ids {
14321432
debug!(

src/librustc/session/code_stats.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ impl CodeStats {
100100

101101
// Primary sort: large-to-small.
102102
// Secondary sort: description (dictionary order)
103-
sorted.sort_by(|info1, info2| {
103+
sorted.sort_unstable_by(|info1, info2| {
104104
// (reversing cmp order to get large-to-small ordering)
105105
match info2.overall_size.cmp(&info1.overall_size) {
106106
Ordering::Equal => info1.type_description.cmp(&info2.type_description),
@@ -151,7 +151,7 @@ impl CodeStats {
151151

152152
// We want to print fields by increasing offset.
153153
let mut fields = fields.clone();
154-
fields.sort_by_key(|f| f.offset);
154+
fields.sort_unstable_by_key(|f| f.offset);
155155

156156
for field in fields.iter() {
157157
let FieldInfo { ref name, offset, size, align } = *field;

src/librustc/session/config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2401,7 +2401,7 @@ mod dep_tracking {
24012401
impl DepTrackingHash for Vec<$t> {
24022402
fn hash(&self, hasher: &mut DefaultHasher, error_format: ErrorOutputType) {
24032403
let mut elems: Vec<&$t> = self.iter().collect();
2404-
elems.sort();
2404+
elems.sort_unstable();
24052405
Hash::hash(&elems.len(), hasher);
24062406
for (index, elem) in elems.iter().enumerate() {
24072407
Hash::hash(&index, hasher);

src/librustc/ty/layout.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -288,15 +288,15 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
288288
match kind {
289289
StructKind::AlwaysSized |
290290
StructKind::MaybeUnsized => {
291-
optimizing.sort_by_key(|&x| {
291+
optimizing.sort_unstable_by_key(|&x| {
292292
// Place ZSTs first to avoid "interesting offsets",
293293
// especially with only one or two non-ZST fields.
294294
let f = &fields[x as usize];
295295
(!f.is_zst(), cmp::Reverse(field_align(f)))
296296
});
297297
}
298298
StructKind::Prefixed(..) => {
299-
optimizing.sort_by_key(|&x| field_align(&fields[x as usize]));
299+
optimizing.sort_unstable_by_key(|&x| field_align(&fields[x as usize]));
300300
}
301301
}
302302
}

src/librustc/util/time_graph.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ impl TimeGraph {
127127
let mut threads: Vec<PerThread> =
128128
table.values().map(|data| data.clone()).collect();
129129

130-
threads.sort_by_key(|timeline| timeline.timings[0].start);
130+
threads.sort_unstable_by_key(|timeline| timeline.timings[0].start);
131131

132132
let earliest_instant = threads[0].timings[0].start;
133133
let latest_instant = threads.iter()

src/librustc_codegen_llvm/back/write.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1923,7 +1923,7 @@ fn start_executing_work(tcx: TyCtxt,
19231923
// Regardless of what order these modules completed in, report them to
19241924
// the backend in the same order every time to ensure that we're handing
19251925
// out deterministic results.
1926-
compiled_modules.sort_by(|a, b| a.name.cmp(&b.name));
1926+
compiled_modules.sort_unstable_by(|a, b| a.name.cmp(&b.name));
19271927

19281928
let compiled_metadata_module = compiled_metadata_module
19291929
.expect("Metadata module not compiled?");

src/librustc_codegen_llvm/base.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,7 @@ pub fn codegen_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
925925
println!("n_inlines: {}", all_stats.n_inlines);
926926
println!("n_closures: {}", all_stats.n_closures);
927927
println!("fn stats:");
928-
all_stats.fn_stats.sort_by_key(|&(_, insns)| insns);
928+
all_stats.fn_stats.sort_unstable_by_key(|&(_, insns)| insns);
929929
for &(ref name, insns) in all_stats.fn_stats.iter() {
930930
println!("{} insns, {}", insns, *name);
931931
}
@@ -1037,7 +1037,7 @@ fn collect_and_partition_mono_items<'a, 'tcx>(
10371037
output.push_str(" @@");
10381038
let mut empty = Vec::new();
10391039
let cgus = item_to_cgus.get_mut(i).unwrap_or(&mut empty);
1040-
cgus.as_mut_slice().sort_by_key(|&(ref name, _)| name.clone());
1040+
cgus.as_mut_slice().sort_unstable_by_key(|&(ref name, _)| name.clone());
10411041
cgus.dedup();
10421042
for &(ref cgu_name, (linkage, _)) in cgus.iter() {
10431043
output.push_str(" ");
@@ -1065,7 +1065,7 @@ fn collect_and_partition_mono_items<'a, 'tcx>(
10651065
})
10661066
.collect();
10671067

1068-
item_keys.sort();
1068+
item_keys.sort_unstable();
10691069

10701070
for item in item_keys {
10711071
println!("MONO_ITEM {}", item);

src/librustc_driver/driver.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1002,7 +1002,7 @@ where
10021002
.iter()
10031003
.cloned()
10041004
.collect();
1005-
missing_fragment_specifiers.sort();
1005+
missing_fragment_specifiers.sort_unstable();
10061006
for span in missing_fragment_specifiers {
10071007
let lint = lint::builtin::MISSING_FRAGMENT_SPECIFIER;
10081008
let msg = "missing fragment specifier";
@@ -1520,7 +1520,7 @@ pub fn collect_crate_types(session: &Session, attrs: &[ast::Attribute]) -> Vec<c
15201520
session,
15211521
));
15221522
}
1523-
base.sort();
1523+
base.sort_unstable();
15241524
base.dedup();
15251525
}
15261526

@@ -1552,7 +1552,7 @@ pub fn compute_crate_disambiguator(session: &Session) -> CrateDisambiguator {
15521552
let mut metadata = session.opts.cg.metadata.clone();
15531553
// We don't want the crate_disambiguator to dependent on the order
15541554
// -C metadata arguments, so sort them:
1555-
metadata.sort();
1555+
metadata.sort_unstable();
15561556
// Every distinct -C metadata value is only incorporated once:
15571557
metadata.dedup();
15581558

src/librustc_driver/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1051,7 +1051,7 @@ impl RustcDefaultCalls {
10511051
match *req {
10521052
TargetList => {
10531053
let mut targets = rustc_target::spec::get_targets().collect::<Vec<String>>();
1054-
targets.sort();
1054+
targets.sort_unstable();
10551055
println!("{}", targets.join("\n"));
10561056
},
10571057
Sysroot => println!("{}", sess.sysroot().display()),
@@ -1117,7 +1117,7 @@ impl RustcDefaultCalls {
11171117
});
11181118
}
11191119

1120-
cfgs.sort();
1120+
cfgs.sort_unstable();
11211121
for cfg in cfgs {
11221122
println!("{}", cfg);
11231123
}
@@ -1229,8 +1229,8 @@ Available lint options:
12291229
fn sort_lint_groups(lints: Vec<(&'static str, Vec<lint::LintId>, bool)>)
12301230
-> Vec<(&'static str, Vec<lint::LintId>)> {
12311231
let mut lints: Vec<_> = lints.into_iter().map(|(x, y, _)| (x, y)).collect();
1232-
lints.sort_by(|&(x, _): &(&'static str, Vec<lint::LintId>),
1233-
&(y, _): &(&'static str, Vec<lint::LintId>)| {
1232+
lints.sort_unstable_by(|&(x, _): &(&'static str, Vec<lint::LintId>),
1233+
&(y, _): &(&'static str, Vec<lint::LintId>)| {
12341234
x.cmp(y)
12351235
});
12361236
lints

src/librustc_driver/profile/trace.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,8 @@ pub fn write_counts(count_file: &mut File, counts: &mut HashMap<String,QueryMetr
208208
for (ref cons, ref qm) in counts.iter() {
209209
data.push((cons.clone(), qm.count.clone(), qm.dur_total.clone(), qm.dur_self.clone()));
210210
};
211-
data.sort_by(|&(_,_,_,self1),&(_,_,_,self2)|
212-
if self1 > self2 { Ordering::Less } else { Ordering::Greater } );
211+
data.sort_unstable_by(|&(_,_,_,self1),&(_,_,_,self2)|
212+
if self1 > self2 { Ordering::Less } else { Ordering::Greater } );
213213
for (cons, count, dur_total, dur_self) in data {
214214
write!(count_file, "{}, {}, {}, {}\n",
215215
cons, count,

src/librustc_errors/emitter.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ impl EmitterWriter {
196196
line_index,
197197
annotations: vec![ann],
198198
});
199-
slot.lines.sort();
199+
slot.lines.sort_unstable();
200200
return;
201201
}
202202
}
@@ -265,7 +265,7 @@ impl EmitterWriter {
265265
}
266266

267267
// Find overlapping multiline annotations, put them at different depths
268-
multiline_annotations.sort_by(|a, b| {
268+
multiline_annotations.sort_unstable_by(|a, b| {
269269
(a.1.line_start, a.1.line_end).cmp(&(b.1.line_start, b.1.line_end))
270270
});
271271
for item in multiline_annotations.clone() {
@@ -403,7 +403,7 @@ impl EmitterWriter {
403403
// otherwise the lines would end up needing to go over a message.
404404

405405
let mut annotations = line.annotations.clone();
406-
annotations.sort_by(|a,b| b.start_col.cmp(&a.start_col));
406+
annotations.sort_unstable_by(|a,b| b.start_col.cmp(&a.start_col));
407407

408408
// First, figure out where each label will be positioned.
409409
//
@@ -681,7 +681,7 @@ impl EmitterWriter {
681681
// | | |
682682
// | | something about `foo`
683683
// | something about `fn foo()`
684-
annotations_position.sort_by(|a, b| {
684+
annotations_position.sort_unstable_by(|a, b| {
685685
// Decreasing order
686686
a.1.len().cmp(&b.1.len()).reverse()
687687
});

src/librustc_errors/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ impl CodeSuggestion {
155155
self.substitutions.iter().cloned().map(|mut substitution| {
156156
// Assumption: all spans are in the same file, and all spans
157157
// are disjoint. Sort in ascending order.
158-
substitution.parts.sort_by_key(|part| part.span.lo());
158+
substitution.parts.sort_unstable_by_key(|part| part.span.lo());
159159

160160
// Find the bounding span.
161161
let lo = substitution.parts.iter().map(|part| part.span.lo()).min().unwrap();
@@ -618,7 +618,7 @@ impl Handler {
618618
})
619619
.collect::<Vec<_>>();
620620
if !error_codes.is_empty() {
621-
error_codes.sort();
621+
error_codes.sort_unstable();
622622
if error_codes.len() > 1 {
623623
let limit = if error_codes.len() > 9 { 9 } else { error_codes.len() };
624624
self.failure(&format!("Some errors occurred: {}{}",

src/librustc_metadata/cstore_impl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ pub fn provide<'tcx>(providers: &mut Providers<'tcx>) {
356356
// we believe that libstd is consistently assigned crate
357357
// num 1, so it should be enough to resolve #46112.
358358
let mut crates: Vec<CrateNum> = (*tcx.crates()).clone();
359-
crates.sort();
359+
crates.sort_unstable();
360360

361361
for &cnum in crates.iter() {
362362
// Ignore crates without a corresponding local `extern crate` item.

src/librustc_metadata/encoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1438,7 +1438,7 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
14381438
})
14391439
.collect::<Vec<_>>();
14401440

1441-
deps.sort_by_key(|&(cnum, _)| cnum);
1441+
deps.sort_unstable_by_key(|&(cnum, _)| cnum);
14421442

14431443
{
14441444
// Sanity-check the crate numbers

src/librustc_mir/borrow_check/move_errors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
324324
}
325325
}
326326

327-
binds_to.sort();
327+
binds_to.sort_unstable();
328328
binds_to.dedup();
329329
for local in binds_to {
330330
let bind_to = &self.mir.local_decls[local];
@@ -344,7 +344,7 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
344344
GroupedMoveError::MovesFromPattern { mut binds_to, .. } => {
345345
// Suggest ref, since there might be a move in
346346
// another match arm
347-
binds_to.sort();
347+
binds_to.sort_unstable();
348348
binds_to.dedup();
349349
for local in binds_to {
350350
let bind_to = &self.mir.local_decls[local];

src/librustc_mir/borrow_check/nll/region_infer/dump_mir.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
7777
}
7878

7979
let mut constraints: Vec<_> = self.constraints.iter().collect();
80-
constraints.sort();
80+
constraints.sort_unstable();
8181
for constraint in &constraints {
8282
let OutlivesConstraint {
8383
sup,

src/librustc_mir/borrow_check/nll/region_infer/error_reporting/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
227227
debug!("report_error: categorized_path={:?}", categorized_path);
228228

229229
// Find what appears to be the most interesting path to report to the user.
230-
categorized_path.sort_by(|p0, p1| p0.0.cmp(&p1.0));
230+
categorized_path.sort_unstable_by(|p0, p1| p0.0.cmp(&p1.0));
231231
debug!("report_error: sorted_path={:?}", categorized_path);
232232

233233
// Get a span

src/librustc_mir/borrow_check/nll/region_infer/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
418418

419419
debug!("propagate_constraints: constraints={:#?}", {
420420
let mut constraints: Vec<_> = self.constraints.iter().collect();
421-
constraints.sort();
421+
constraints.sort_unstable();
422422
constraints
423423
});
424424

src/librustc_mir/build/matches/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
192192
let source_info = self.source_info(span);
193193

194194
let mut otherwise = otherwise;
195-
otherwise.sort();
195+
otherwise.sort_unstable();
196196
otherwise.dedup(); // variant switches can introduce duplicate target blocks
197197
for block in otherwise {
198198
self.cfg.terminate(block, source_info, TerminatorKind::Unreachable);
@@ -644,7 +644,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
644644
-> BasicBlock
645645
{
646646
let source_info = self.source_info(span);
647-
otherwise.sort();
647+
otherwise.sort_unstable();
648648
otherwise.dedup(); // variant switches can introduce duplicate target blocks
649649
if otherwise.len() == 1 {
650650
otherwise[0]

src/librustc_mir/interpret/memory.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
408408
return;
409409
}
410410
use std::fmt::Write;
411-
allocs.sort();
411+
allocs.sort_unstable();
412412
allocs.dedup();
413413
let mut allocs_to_print = VecDeque::from(allocs);
414414
let mut allocs_seen = FxHashSet::default();

src/librustc_mir/monomorphize/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub fn assert_symbols_are_distinct<'a, 'tcx, I>(tcx: TyCtxt<'a, 'tcx, 'tcx>, mon
2929
(mono_item, mono_item.symbol_name(tcx))
3030
}).collect();
3131

32-
(&mut symbols[..]).sort_by(|&(_, ref sym1), &(_, ref sym2)|{
32+
(&mut symbols[..]).sort_unstable_by(|&(_, ref sym1), &(_, ref sym2)|{
3333
sym1.cmp(sym2)
3434
});
3535

src/librustc_mir/monomorphize/partitioning.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ pub fn partition<'a, 'tcx, I>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
262262
internalization_candidates: _,
263263
} = post_inlining;
264264

265-
result.sort_by(|cgu1, cgu2| {
265+
result.sort_unstable_by(|cgu1, cgu2| {
266266
cgu1.name().cmp(cgu2.name())
267267
});
268268

@@ -502,7 +502,7 @@ fn merge_codegen_units<'tcx>(initial_partitioning: &mut PreInliningPartitioning<
502502
// smallest into each other) we're sure to start off with a deterministic
503503
// order (sorted by name). This'll mean that if two cgus have the same size
504504
// the stable sort below will keep everything nice and deterministic.
505-
codegen_units.sort_by_key(|cgu| cgu.name().clone());
505+
codegen_units.sort_unstable_by_key(|cgu| cgu.name().clone());
506506

507507
// Merge the two smallest codegen units until the target size is reached.
508508
while codegen_units.len() > target_cgu_count {

src/librustc_mir/transform/check_unsafety.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ pub fn check_unsafety<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) {
501501
}
502502

503503
let mut unsafe_blocks: Vec<_> = unsafe_blocks.into_iter().collect();
504-
unsafe_blocks.sort();
504+
unsafe_blocks.sort_unstable();
505505
let used_unsafe: FxHashSet<_> = unsafe_blocks.iter()
506506
.flat_map(|&&(id, used)| if used { Some(id) } else { None })
507507
.collect();

src/librustc_mir/util/patch.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ impl<'tcx> MirPatch<'tcx> {
156156
}
157157

158158
let mut new_statements = self.new_statements;
159-
new_statements.sort_by(|u,v| u.0.cmp(&v.0));
159+
new_statements.sort_unstable_by(|u,v| u.0.cmp(&v.0));
160160

161161
let mut delta = 0;
162162
let mut last_bb = START_BLOCK;

0 commit comments

Comments
 (0)