Avoid Unstable Sort (#12455)

* dont use unstable sort

* remove comment

* add clippy rule
This commit is contained in:
Shawn Tabrizi
2022-10-11 14:41:43 -04:00
committed by GitHub
parent fbdfa84cd7
commit 023aa03fea
9 changed files with 14 additions and 17 deletions
+2 -2
View File
@@ -181,7 +181,7 @@ impl Analysis {
})
.collect();
values.sort_unstable();
values.sort();
let mid = values.len() / 2;
Some(Self {
@@ -311,7 +311,7 @@ impl Analysis {
}
for (_, rs) in results.iter_mut() {
rs.sort_unstable();
rs.sort();
let ql = rs.len() / 4;
*rs = rs[ql..rs.len() - ql].to_vec();
}
@@ -1976,11 +1976,7 @@ pub mod env {
data_len: u32,
) -> Result<(), TrapReason> {
fn has_duplicates<T: Ord>(items: &mut Vec<T>) -> bool {
// # Warning
//
// Unstable sorts are non-deterministic across architectures. The usage here is OK
// because we are rejecting duplicates which removes the non determinism.
items.sort_unstable();
items.sort();
// Find any two consecutive equal elements.
items.windows(2).any(|w| match &w {
&[a, b] => a == b,
@@ -155,7 +155,7 @@ pub fn trim_helpers() -> TrimHelpers {
seq_phragmen(desired_targets as usize, targets.clone(), voters.clone(), None).unwrap();
// sort by decreasing order of stake
assignments.sort_unstable_by_key(|assignment| {
assignments.sort_by_key(|assignment| {
std::cmp::Reverse(stakes.get(&assignment.who).cloned().unwrap_or_default())
});
@@ -63,7 +63,7 @@ benchmarks! {
}
}: {
// The benchmark execution phase could also be a closure with custom code
m.sort_unstable();
m.sort();
}
// This line generates test cases for benchmarking, and could be run by: