Skip to content

Commit 41392e7

Browse files
authored
feat(xxhash): Add xxhash support and some utilities for making it easier to use (#51)
Fixes: #47
1 parent 1ac1d64 commit 41392e7

File tree

3 files changed

+261
-23
lines changed

3 files changed

+261
-23
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ serde_derive = "1.0.130"
2424
serde_json = "1.0.68"
2525
sha1 = "0.10.5"
2626
sha2 = "0.10.6"
27-
ssri = "8.1.0"
27+
ssri = "9.0.0"
2828
tempfile = "3.4.0"
2929
thiserror = "1.0.40"
3030
tokio = { version = "1.12.0", features = [

benches/benchmarks.rs

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,17 @@ fn read_hash_sync(c: &mut Criterion) {
104104
});
105105
}
106106

107+
fn read_hash_sync_xxh3(c: &mut Criterion) {
108+
let tmp = tempfile::tempdir().unwrap();
109+
let cache = tmp.path().to_owned();
110+
let data = b"hello world".to_vec();
111+
let sri =
112+
cacache::write_sync_with_algo(cacache::Algorithm::Xxh3, &cache, "hello", data).unwrap();
113+
c.bench_function("get::data_hash_sync::xxh3", move |b| {
114+
b.iter(|| cacache::read_hash_sync(black_box(&cache), black_box(&sri)).unwrap())
115+
});
116+
}
117+
107118
fn read_hash_many_sync(c: &mut Criterion) {
108119
let tmp = tempfile::tempdir().unwrap();
109120
let cache = tmp.path().to_owned();
@@ -124,6 +135,28 @@ fn read_hash_many_sync(c: &mut Criterion) {
124135
});
125136
}
126137

138+
fn read_hash_many_sync_xxh3(c: &mut Criterion) {
139+
let tmp = tempfile::tempdir().unwrap();
140+
let cache = tmp.path().to_owned();
141+
let data: Vec<_> = (0..)
142+
.take(NUM_REPEATS)
143+
.map(|i| format!("test_file_{i}"))
144+
.collect();
145+
let sris: Vec<_> = data
146+
.iter()
147+
.map(|datum| {
148+
cacache::write_sync_with_algo(cacache::Algorithm::Xxh3, &cache, "hello", datum).unwrap()
149+
})
150+
.collect();
151+
c.bench_function("get::data_hash_many_sync::xxh3", move |b| {
152+
b.iter(|| {
153+
for sri in sris.iter() {
154+
cacache::read_hash_sync(black_box(&cache), black_box(sri)).unwrap();
155+
}
156+
})
157+
});
158+
}
159+
127160
fn read_sync(c: &mut Criterion) {
128161
let tmp = tempfile::tempdir().unwrap();
129162
let cache = tmp.path().to_owned();
@@ -144,6 +177,17 @@ fn read_hash_sync_big_data(c: &mut Criterion) {
144177
});
145178
}
146179

180+
fn read_hash_sync_big_data_xxh3(c: &mut Criterion) {
181+
let tmp = tempfile::tempdir().unwrap();
182+
let cache = tmp.path().to_owned();
183+
let data = vec![1; 1024 * 1024 * 5];
184+
let sri =
185+
cacache::write_sync_with_algo(cacache::Algorithm::Xxh3, &cache, "hello", data).unwrap();
186+
c.bench_function("get_hash_big_data::xxh3", move |b| {
187+
b.iter(|| cacache::read_hash_sync(black_box(&cache), black_box(&sri)).unwrap())
188+
});
189+
}
190+
147191
fn read_hash_many_async(c: &mut Criterion) {
148192
let tmp = tempfile::tempdir().unwrap();
149193
let cache = tmp.path().to_owned();
@@ -195,6 +239,38 @@ fn read_hash_async_big_data(c: &mut Criterion) {
195239
});
196240
}
197241

242+
fn write_hash(c: &mut Criterion) {
243+
let tmp = tempfile::tempdir().unwrap();
244+
let cache = tmp.path().to_owned();
245+
c.bench_function("put::data::sync", move |b| {
246+
b.iter_custom(|iters| {
247+
let start = std::time::Instant::now();
248+
for i in 0..iters {
249+
cacache::write_hash_sync(&cache, format!("hello world{i}")).unwrap();
250+
}
251+
start.elapsed()
252+
})
253+
});
254+
}
255+
256+
fn write_hash_xxh3(c: &mut Criterion) {
257+
let tmp = tempfile::tempdir().unwrap();
258+
let cache = tmp.path().to_owned();
259+
c.bench_function("put::data::sync::xxh3", move |b| {
260+
b.iter_custom(|iters| {
261+
let start = std::time::Instant::now();
262+
for i in 0..iters {
263+
cacache::write_hash_sync_with_algo(
264+
cacache::Algorithm::Xxh3,
265+
&cache,
266+
format!("hello world{i}"),
267+
)
268+
.unwrap();
269+
}
270+
start.elapsed()
271+
})
272+
});
273+
}
198274
fn write_hash_async(c: &mut Criterion) {
199275
let tmp = tempfile::tempdir().unwrap();
200276
let cache = tmp.path().to_owned();
@@ -209,6 +285,25 @@ fn write_hash_async(c: &mut Criterion) {
209285
});
210286
}
211287

288+
fn write_hash_async_xxh3(c: &mut Criterion) {
289+
let tmp = tempfile::tempdir().unwrap();
290+
let cache = tmp.path().to_owned();
291+
c.bench_function("put::data::xxh3", move |b| {
292+
b.iter_custom(|iters| {
293+
let start = std::time::Instant::now();
294+
for i in 0..iters {
295+
block_on(cacache::write_hash_with_algo(
296+
cacache::Algorithm::Xxh3,
297+
&cache,
298+
format!("hello world{i}"),
299+
))
300+
.unwrap();
301+
}
302+
start.elapsed()
303+
})
304+
});
305+
}
306+
212307
#[cfg(feature = "link_to")]
213308
fn create_tmpfile(tmp: &tempfile::TempDir, buf: &[u8]) -> PathBuf {
214309
let dir = tmp.path().to_owned();
@@ -294,12 +389,18 @@ criterion_group!(
294389
read_hash_async,
295390
read_hash_many_async,
296391
read_async,
392+
write_hash,
393+
write_hash_xxh3,
297394
write_hash_async,
395+
write_hash_async_xxh3,
298396
read_hash_sync,
397+
read_hash_sync_xxh3,
299398
read_hash_many_sync,
399+
read_hash_many_sync_xxh3,
300400
read_sync,
301401
read_hash_async_big_data,
302-
read_hash_sync_big_data
402+
read_hash_sync_big_data,
403+
read_hash_sync_big_data_xxh3,
303404
);
304405

305406
#[cfg(feature = "link_to")]

0 commit comments

Comments
 (0)