Skip to content

Commit f48780f

Browse files
committed
optimization: use ahash in uniq
1 parent 1160386 commit f48780f

File tree

3 files changed

+163
-5
lines changed

3 files changed

+163
-5
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ keywords = ["cli", "uniq"]
1212

1313
[dependencies]
1414
clap = "^2.33.0"
15-
anyhow = "1.0"
15+
anyhow = "1.0"
16+
ahash = "0.2.18"

src/main.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
extern crate clap;
2+
extern crate ahash;
23

3-
use std::collections::{HashSet, HashMap, hash_map, hash_map::DefaultHasher};
4+
use std::collections::{HashSet, HashMap, hash_map};
45
use std::hash::{Hasher, BuildHasher};
56
use std::io::{stdin, BufRead, BufReader, stdout, Write, BufWriter};
67
use std::slice;
8+
use ahash::ABuildHasher;
79
use clap::{Arg, App};
810
use anyhow::Result;
911

@@ -54,8 +56,8 @@ impl BuildHasher for BuildIdentityHasher {
5456
}
5557
}
5658

57-
fn hash<T: std::hash::Hash>(v: &T) -> u64 {
58-
let mut s = DefaultHasher::new();
59+
fn calc_hash<T: BuildHasher, U: std::hash::Hash>(build: &T, v: &U) -> u64 {
60+
let mut s = build.build_hasher();
5961
v.hash(&mut s);
6062
s.finish()
6163
}
@@ -65,6 +67,7 @@ fn uniq_cmd(delim: u8) -> Result<()> {
6567
let inp = stdin();
6668
let mut out = BufWriter::new(out.lock());
6769
let mut inp = BufReader::new(inp.lock());
70+
let hasher = ABuildHasher::new();
6871
let mut set = HashSet::<u64, BuildIdentityHasher>::default();
6972
let mut line = Vec::<u8>::new();
7073
while inp.read_until(delim, &mut line)? > 0 {
@@ -73,7 +76,7 @@ fn uniq_cmd(delim: u8) -> Result<()> {
7376
line.pop();
7477
}
7578

76-
if set.insert(hash(&line)) {
79+
if set.insert(calc_hash(&hasher, &line)) {
7780
out.write(&line)?;
7881
out.write(slice::from_ref(&delim))?;
7982
}

0 commit comments

Comments
 (0)