Skip to content

Commit

Permalink
feat(xxhash): Add xxhash support and some utilities for making it eas…
Browse files Browse the repository at this point in the history
…ier to use

Fixes: #47
  • Loading branch information
zkat committed May 21, 2023
1 parent 81bc84b commit 678acef
Show file tree
Hide file tree
Showing 3 changed files with 261 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ serde_derive = "1.0.130"
serde_json = "1.0.68"
sha1 = "0.10.5"
sha2 = "0.10.6"
ssri = "8.1.0"
ssri = "9.0.0"
tempfile = "3.4.0"
thiserror = "1.0.40"
tokio = { version = "1.12.0", features = [
Expand Down
103 changes: 102 additions & 1 deletion benches/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,17 @@ fn read_hash_sync(c: &mut Criterion) {
});
}

fn read_hash_sync_xxh3(c: &mut Criterion) {
let tmp = tempfile::tempdir().unwrap();
let cache = tmp.path().to_owned();
let data = b"hello world".to_vec();
let sri =
cacache::write_sync_with_algo(cacache::Algorithm::Xxh3, &cache, "hello", data).unwrap();
c.bench_function("get::data_hash_sync::xxh3", move |b| {
b.iter(|| cacache::read_hash_sync(black_box(&cache), black_box(&sri)).unwrap())
});
}

fn read_hash_many_sync(c: &mut Criterion) {
let tmp = tempfile::tempdir().unwrap();
let cache = tmp.path().to_owned();
Expand All @@ -124,6 +135,28 @@ fn read_hash_many_sync(c: &mut Criterion) {
});
}

fn read_hash_many_sync_xxh3(c: &mut Criterion) {
let tmp = tempfile::tempdir().unwrap();
let cache = tmp.path().to_owned();
let data: Vec<_> = (0..)
.take(NUM_REPEATS)
.map(|i| format!("test_file_{i}"))
.collect();
let sris: Vec<_> = data
.iter()
.map(|datum| {
cacache::write_sync_with_algo(cacache::Algorithm::Xxh3, &cache, "hello", datum).unwrap()
})
.collect();
c.bench_function("get::data_hash_many_sync::xxh3", move |b| {
b.iter(|| {
for sri in sris.iter() {
cacache::read_hash_sync(black_box(&cache), black_box(sri)).unwrap();
}
})
});
}

fn read_sync(c: &mut Criterion) {
let tmp = tempfile::tempdir().unwrap();
let cache = tmp.path().to_owned();
Expand All @@ -144,6 +177,17 @@ fn read_hash_sync_big_data(c: &mut Criterion) {
});
}

fn read_hash_sync_big_data_xxh3(c: &mut Criterion) {
let tmp = tempfile::tempdir().unwrap();
let cache = tmp.path().to_owned();
let data = vec![1; 1024 * 1024 * 5];
let sri =
cacache::write_sync_with_algo(cacache::Algorithm::Xxh3, &cache, "hello", data).unwrap();
c.bench_function("get_hash_big_data::xxh3", move |b| {
b.iter(|| cacache::read_hash_sync(black_box(&cache), black_box(&sri)).unwrap())
});
}

fn read_hash_many_async(c: &mut Criterion) {
let tmp = tempfile::tempdir().unwrap();
let cache = tmp.path().to_owned();
Expand Down Expand Up @@ -195,6 +239,38 @@ fn read_hash_async_big_data(c: &mut Criterion) {
});
}

fn write_hash(c: &mut Criterion) {
let tmp = tempfile::tempdir().unwrap();
let cache = tmp.path().to_owned();
c.bench_function("put::data::sync", move |b| {
b.iter_custom(|iters| {
let start = std::time::Instant::now();
for i in 0..iters {
cacache::write_hash_sync(&cache, format!("hello world{i}")).unwrap();
}
start.elapsed()
})
});
}

fn write_hash_xxh3(c: &mut Criterion) {
let tmp = tempfile::tempdir().unwrap();
let cache = tmp.path().to_owned();
c.bench_function("put::data::sync::xxh3", move |b| {
b.iter_custom(|iters| {
let start = std::time::Instant::now();
for i in 0..iters {
cacache::write_hash_sync_with_algo(
cacache::Algorithm::Xxh3,
&cache,
format!("hello world{i}"),
)
.unwrap();
}
start.elapsed()
})
});
}
fn write_hash_async(c: &mut Criterion) {
let tmp = tempfile::tempdir().unwrap();
let cache = tmp.path().to_owned();
Expand All @@ -209,6 +285,25 @@ fn write_hash_async(c: &mut Criterion) {
});
}

fn write_hash_async_xxh3(c: &mut Criterion) {
let tmp = tempfile::tempdir().unwrap();
let cache = tmp.path().to_owned();
c.bench_function("put::data::xxh3", move |b| {
b.iter_custom(|iters| {
let start = std::time::Instant::now();
for i in 0..iters {
block_on(cacache::write_hash_with_algo(
cacache::Algorithm::Xxh3,
&cache,
format!("hello world{i}"),
))
.unwrap();
}
start.elapsed()
})
});
}

#[cfg(feature = "link_to")]
fn create_tmpfile(tmp: &tempfile::TempDir, buf: &[u8]) -> PathBuf {
let dir = tmp.path().to_owned();
Expand Down Expand Up @@ -294,12 +389,18 @@ criterion_group!(
read_hash_async,
read_hash_many_async,
read_async,
write_hash,
write_hash_xxh3,
write_hash_async,
write_hash_async_xxh3,
read_hash_sync,
read_hash_sync_xxh3,
read_hash_many_sync,
read_hash_many_sync_xxh3,
read_sync,
read_hash_async_big_data,
read_hash_sync_big_data
read_hash_sync_big_data,
read_hash_sync_big_data_xxh3,
);

#[cfg(feature = "link_to")]
Expand Down
Loading

0 comments on commit 678acef

Please sign in to comment.