Skip to content

Commit 970f6b2

Browse files
committed
sort: Use ahash
1 parent 3d56517 commit 970f6b2

File tree

5 files changed

+69
-56
lines changed

5 files changed

+69
-56
lines changed

Cargo.lock

Lines changed: 14 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# coreutils (uutils)
22
# * see the repository LICENSE, README, and CONTRIBUTING files for more information
33

4-
# spell-checker:ignore (libs) bigdecimal datetime serde wincode gethostid kqueue libselinux mangen memmap uuhelp startswith constness expl unnested logind cfgs interner
4+
# spell-checker:ignore (libs) ahash bigdecimal datetime serde wincode gethostid kqueue libselinux mangen memmap uuhelp startswith constness expl unnested logind cfgs interner
55

66
[package]
77
name = "coreutils"
@@ -311,6 +311,7 @@ readme = "README.package.md"
311311
version = "0.6.0"
312312

313313
[workspace.dependencies]
314+
ahash = "0.8.12"
314315
ansi-width = "0.1.0"
315316
bigdecimal = "0.4"
316317
binary-heap-plus = "0.5.0"
@@ -329,7 +330,6 @@ dns-lookup = { version = "3.0.0" }
329330
exacl = "0.12.0"
330331
file_diff = "1.0.0"
331332
filetime = "0.2.23"
332-
fnv = "1.0.7"
333333
fs_extra = "1.3.0"
334334
fts-sys = "0.2.16"
335335
gcd = "2.3"

fuzz/Cargo.lock

Lines changed: 42 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/uu/sort/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# spell-checker:ignore bigdecimal
1+
# spell-checker:ignore ahash bigdecimal
22

33
[package]
44
name = "uu_sort"
@@ -29,7 +29,6 @@ bigdecimal = { workspace = true }
2929
binary-heap-plus = { workspace = true }
3030
clap = { workspace = true }
3131
compare = { workspace = true }
32-
fnv = { workspace = true }
3332
itertools = { workspace = true }
3433
memchr = { workspace = true }
3534
rand = { workspace = true }
@@ -45,6 +44,7 @@ uucore = { workspace = true, features = [
4544
"i18n-collator",
4645
] }
4746
fluent = { workspace = true }
47+
ahash = { workspace = true }
4848

4949
[target.'cfg(not(target_os = "redox"))'.dependencies]
5050
ctrlc = { workspace = true }

src/uu/sort/src/sort.rs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,21 @@ mod merge;
1818
mod numeric_str_cmp;
1919
mod tmp_dir;
2020

21+
use ahash::AHashMap;
2122
use bigdecimal::BigDecimal;
2223
use chunks::LineData;
2324
use clap::builder::ValueParser;
2425
use clap::{Arg, ArgAction, ArgMatches, Command};
2526
use custom_str_cmp::custom_str_cmp;
26-
2727
use ext_sort::ext_sort;
28-
use fnv::FnvHasher;
2928
use numeric_str_cmp::{NumInfo, NumInfoParseSettings, human_numeric_str_cmp, numeric_str_cmp};
3029
use rand::{Rng, rng};
3130
use rayon::prelude::*;
3231
use std::cmp::Ordering;
3332
use std::env;
3433
use std::ffi::{OsStr, OsString};
3534
use std::fs::{File, OpenOptions};
36-
use std::hash::{Hash, Hasher};
35+
use std::hash::{BuildHasher, Hash, Hasher};
3736
use std::io::{BufRead, BufReader, BufWriter, Read, Write, stdin, stdout};
3837
use std::num::{IntErrorKind, NonZero};
3938
use std::ops::Range;
@@ -1681,7 +1680,7 @@ fn index_legacy_warnings(processed_args: &[OsString], legacy_warnings: &mut [Leg
16811680
return;
16821681
}
16831682

1684-
let mut index_by_arg = std::collections::HashMap::new();
1683+
let mut index_by_arg = AHashMap::default();
16851684
for (warning_idx, warning) in legacy_warnings.iter().enumerate() {
16861685
index_by_arg.insert(warning.arg_index, warning_idx);
16871686
}
@@ -2909,7 +2908,8 @@ fn salt_from_random_source(path: &Path) -> UResult<[u8; SALT_LEN]> {
29092908
let mut reader = open_with_open_failed_error(path)?;
29102909
let mut buf = [0u8; BUF_LEN];
29112910
let mut total = 0usize;
2912-
let mut hasher = FnvHasher::default();
2911+
// freeze seed for --random-source
2912+
let mut hasher = ahash::RandomState::with_seeds(1, 1, 1, 1).build_hasher();
29132913

29142914
loop {
29152915
let n = reader
@@ -2934,7 +2934,8 @@ fn salt_from_random_source(path: &Path) -> UResult<[u8; SALT_LEN]> {
29342934
}
29352935

29362936
let first = hasher.finish();
2937-
let mut second_hasher = FnvHasher::default();
2937+
// freeze seed for --random-source
2938+
let mut second_hasher = ahash::RandomState::with_seeds(2, 2, 2, 2).build_hasher();
29382939
second_hasher.write(RANDOM_SOURCE_TAG);
29392940
second_hasher.write_u64(first);
29402941
let second = second_hasher.finish();
@@ -2946,9 +2947,8 @@ fn salt_from_random_source(path: &Path) -> UResult<[u8; SALT_LEN]> {
29462947
}
29472948

29482949
fn get_hash<T: Hash>(t: &T) -> u64 {
2949-
let mut s = FnvHasher::default();
2950-
t.hash(&mut s);
2951-
s.finish()
2950+
// Is reproducibility of get_hash itself needed for --random-source ?
2951+
ahash::RandomState::with_seeds(0, 0, 0, 0).hash_one(t)
29522952
}
29532953

29542954
fn random_shuffle(a: &[u8], b: &[u8], salt: &[u8]) -> Ordering {
@@ -3086,13 +3086,6 @@ mod tests {
30863086
buffer
30873087
}
30883088

3089-
#[test]
3090-
fn test_get_hash() {
3091-
let a = "Ted".to_string();
3092-
3093-
assert_eq!(2_646_829_031_758_483_623, get_hash(&a));
3094-
}
3095-
30963089
#[test]
30973090
fn test_random_shuffle() {
30983091
let a = b"Ted";

0 commit comments

Comments
 (0)