Skip to content

Commit d41ed38

Browse files
authored
tsort: Use rustc-hash - improve performance by 16.74% (#10680)
1 parent 8c406d7 commit d41ed38

5 files changed

Lines changed: 12 additions & 8 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ rayon = "1.10"
371371
regex = "1.10.4"
372372
rlimit = "0.11.0"
373373
rstest = "0.26.0"
374+
rustc-hash = "2.1.1"
374375
rust-ini = "0.21.0"
375376
same-file = "1.0.6"
376377
self_cell = "1.0.4"

src/uu/shuf/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ rand_core = { workspace = true }
2626
sha3 = { workspace = true }
2727
uucore = { workspace = true }
2828
fluent = { workspace = true }
29-
rustc-hash = "2.1.1"
29+
rustc-hash = { workspace = true }
3030

3131
[[bin]]
3232
name = "shuf"

src/uu/tsort/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ fluent = { workspace = true }
2424
string-interner = { workspace = true }
2525
thiserror = { workspace = true }
2626
uucore = { workspace = true }
27+
rustc-hash = { workspace = true }
2728

2829
[target.'cfg(unix)'.dependencies]
2930
nix = { workspace = true, features = ["fs"] }

src/uu/tsort/src/tsort.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
//spell-checker:ignore TAOCP indegree fadvise FADV
66
//spell-checker:ignore (libs) interner uclibc
77
use clap::{Arg, ArgAction, Command};
8+
use rustc_hash::FxHashMap;
9+
use std::collections::VecDeque;
810
use std::collections::hash_map::Entry;
9-
use std::collections::{HashMap, VecDeque};
1011
use std::ffi::OsString;
1112
use std::fs::File;
1213
use std::io::{self, BufRead, BufReader};
@@ -19,7 +20,7 @@ use uucore::{format_usage, show, translate};
1920

2021
// short types for switching interning behavior on the fly.
2122
type Sym = string_interner::symbol::SymbolUsize;
22-
type Interner = StringInterner<BucketBackend<Sym>>;
23+
type Interner = StringInterner<BucketBackend<Sym>, rustc_hash::FxBuildHasher>;
2324

2425
mod options {
2526
pub const FILE: &str = "file";
@@ -224,18 +225,18 @@ impl Node {
224225

225226
struct Graph {
226227
name_sym: Sym,
227-
nodes: HashMap<Sym, Node>,
228+
nodes: FxHashMap<Sym, Node>,
228229
interner: Interner,
229230
}
230231

231232
impl Graph {
232233
fn new(name: String) -> Self {
233-
let mut interner = Interner::new();
234+
let mut interner = Interner::with_hasher(rustc_hash::FxBuildHasher);
234235
let name_sym = interner.get_or_intern(name);
235236
Self {
236237
name_sym,
237238
interner,
238-
nodes: HashMap::default(),
239+
nodes: FxHashMap::default(),
239240
}
240241
}
241242

@@ -357,7 +358,7 @@ impl Graph {
357358
let mut nodes: Vec<_> = self.nodes.keys().copied().collect();
358359
nodes.sort_unstable_by(|a, b| self.get_node_name(*a).cmp(self.get_node_name(*b)));
359360

360-
let mut visited = HashMap::new();
361+
let mut visited = FxHashMap::default();
361362
let mut stack = Vec::with_capacity(self.nodes.len());
362363
for &node in &nodes {
363364
if self.dfs(node, &mut visited, &mut stack) {
@@ -376,7 +377,7 @@ impl Graph {
376377
fn dfs<'a>(
377378
&'a self,
378379
node: Sym,
379-
visited: &mut HashMap<Sym, VisitedState>,
380+
visited: &mut FxHashMap<Sym, VisitedState>,
380381
stack: &mut Vec<(Sym, &'a [Sym])>,
381382
) -> bool {
382383
stack.push((

0 commit comments

Comments
 (0)