Skip to content

Commit 558eee4

Browse files
committed
Auto merge of #5732 - Eh2406:sort_unstable, r=alexcrichton`,
most sorts can be unstable Inspired by [this](https://github.com/rust-lang/cargo/blob/94f7058a483b05ad742da5efb66dd1c2d4b8619c/src/bin/cargo/main.rs#L112-L122) witch was improved in #5691, I did a quick review of `sort`s in the code. Most can be unstable, some can use a `_key` form, and none had unnecessary allocation.
2 parents e325bff + c74aa94 commit 558eee4

File tree

7 files changed

+11
-11
lines changed

7 files changed

+11
-11
lines changed

src/cargo/core/resolver/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1054,7 +1054,8 @@ fn check_cycles(resolve: &Resolve, activations: &Activations) -> CargoResult<()>
10541054
.collect();
10551055

10561056
// Sort packages to produce user friendly deterministic errors.
1057-
let all_packages = resolve.iter().collect::<BinaryHeap<_>>().into_sorted_vec();
1057+
let mut all_packages: Vec<_> = resolve.iter().collect();
1058+
all_packages.sort_unstable();
10581059
let mut checked = HashSet::new();
10591060
for pkg in all_packages {
10601061
if !checked.contains(pkg) {

src/cargo/core/resolver/resolve.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ unable to verify that `{0}` is the same as when the lockfile was generated
193193

194194
pub fn features_sorted(&self, pkg: &PackageId) -> Vec<&str> {
195195
let mut v = Vec::from_iter(self.features(pkg).iter().map(|s| s.as_ref()));
196-
v.sort();
196+
v.sort_unstable();
197197
v
198198
}
199199

src/cargo/ops/cargo_install.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ where
538538
return Ok((pkg.clone(), Box::new(source)));
539539

540540
fn multi_err(kind: &str, mut pkgs: Vec<&Package>) -> String {
541-
pkgs.sort_by(|a, b| a.name().cmp(&b.name()));
541+
pkgs.sort_unstable_by_key(|a| a.name());
542542
format!(
543543
"multiple packages with {} found: {}",
544544
kind,

src/cargo/ops/cargo_package.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub fn package(ws: &Workspace, opts: &PackageOpts) -> CargoResult<Option<FileLoc
5151
if include_lockfile(pkg) {
5252
list.push("Cargo.lock".into());
5353
}
54-
list.sort();
54+
list.sort_unstable();
5555
for file in list.iter() {
5656
println!("{}", file.display());
5757
}

src/cargo/sources/path.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -447,10 +447,9 @@ impl<'cfg> PathSource<'cfg> {
447447
// TODO: Drop collect and sort after transition period and dropping warning tests.
448448
// See <https://github.com/rust-lang/cargo/issues/4268>
449449
// and <https://github.com/rust-lang/cargo/pull/4270>
450-
let mut entries: Vec<fs::DirEntry> = fs::read_dir(path)?.map(|e| e.unwrap()).collect();
451-
entries.sort_by(|a, b| a.path().as_os_str().cmp(b.path().as_os_str()));
452-
for entry in entries {
453-
let path = entry.path();
450+
let mut entries: Vec<PathBuf> = fs::read_dir(path)?.map(|e| e.unwrap().path()).collect();
451+
entries.sort_unstable_by(|a, b| a.as_os_str().cmp(b.as_os_str()));
452+
for path in entries {
454453
let name = path.file_name().and_then(|s| s.to_str());
455454
// Skip dotfile directories
456455
if name.map(|s| s.starts_with('.')) == Some(true) {

src/cargo/util/rustc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ fn process_fingerprint(cmd: &ProcessBuilder) -> u64 {
239239
let mut hasher = SipHasher::new_with_keys(0, 0);
240240
cmd.get_args().hash(&mut hasher);
241241
let mut env = cmd.get_envs().iter().collect::<Vec<_>>();
242-
env.sort();
242+
env.sort_unstable();
243243
env.hash(&mut hasher);
244244
hasher.finish()
245245
}

tests/testsuite/out_dir.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ fn check_dir_contents(
280280

281281
let actual = list_dir(out_dir);
282282
let mut expected = expected.iter().map(|s| s.to_string()).collect::<Vec<_>>();
283-
expected.sort();
283+
expected.sort_unstable();
284284
assert_eq!(actual, expected);
285285
}
286286

@@ -290,6 +290,6 @@ fn list_dir(dir: &Path) -> Vec<String> {
290290
let entry = entry.unwrap();
291291
res.push(entry.file_name().into_string().unwrap());
292292
}
293-
res.sort();
293+
res.sort_unstable();
294294
res
295295
}

0 commit comments

Comments
 (0)