forked from fjall-rs/lsm-tree
-
Notifications
You must be signed in to change notification settings - Fork 1
perf(compression): cache pre-compiled Dictionary across block decompress calls #227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
polaz
merged 4 commits into
main
from
feat/#217-perfcompression-cache-pre-compiled-dictionary-acro
Apr 7, 2026
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4ee8981
perf(compression): cache pre-compiled Dictionary across block decompr…
polaz 7bc51d4
fix(compression): use 64-bit TLS cache key and enforce capacity in pu…
polaz 56edf08
fix(compression): restore decode_all_to_vec with post-decode capacity…
polaz 76fb725
fix(compression): align doc comments and test assertion with caching …
polaz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| // Copyright (c) 2025-present, Structured World Foundation | ||
| // This source code is licensed under the Apache 2.0 License | ||
| // (found in the LICENSE-APACHE file in the repository) | ||
|
|
||
| //! Benchmark: per-block zstd dictionary decompression latency. | ||
| //! | ||
| //! Measures the cost of `decompress_with_dict` for a single compressed block, | ||
| //! both cold (first call, dictionary not yet cached in the backend's TLS / | ||
| //! `OnceLock`) and warm (subsequent calls, dictionary already cached). | ||
| //! | ||
| //! Run with: | ||
| //! | ||
| //! ```text | ||
| //! cargo bench --bench zstd_dict --features zstd # C FFI backend | ||
| //! cargo bench --bench zstd_dict --features zstd-pure # pure Rust backend | ||
| //! ``` | ||
|
|
||
| use criterion::{Criterion, criterion_group, criterion_main}; | ||
|
|
||
| #[cfg(zstd_any)] | ||
| use criterion::BatchSize; | ||
| #[cfg(zstd_any)] | ||
| use lsm_tree::compression::ZstdDictionary; | ||
|
|
||
| // --- test fixtures (only needed when a zstd backend is enabled) ---------- | ||
|
|
||
| #[cfg(zstd_any)] | ||
| /// Pre-trained zstd dictionary (206 bytes). | ||
| /// | ||
| /// Generated with the `zstd` C library from 100 samples × 32 bytes | ||
| /// (cycling pattern 0..4). | ||
| const DICT: &[u8] = &[ | ||
| 55, 164, 48, 236, 98, 64, 12, 7, 42, 16, 120, 62, 7, 204, 192, 51, 240, 12, 60, 3, 207, 192, | ||
| 51, 240, 12, 60, 3, 207, 192, 51, 24, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 48, | ||
| 165, 148, 2, 227, 76, 8, 33, 132, 16, 66, 136, 136, 136, 60, 84, 160, 64, 65, 65, 65, 65, 65, | ||
| 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 193, 231, 162, | ||
| 40, 138, 162, 40, 138, 162, 40, 165, 148, 82, 74, 169, 170, 234, 1, 100, 160, 170, 193, 96, 48, | ||
| 24, 12, 6, 131, 193, 96, 48, 12, 195, 48, 12, 195, 48, 12, 195, 48, 198, 24, 99, 140, 153, 29, | ||
| 1, 0, 0, 0, 4, 0, 0, 0, 8, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, | ||
| 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, | ||
| 2, 2, 2, 2, 2, 2, | ||
| ]; | ||
|
|
||
| #[cfg(zstd_any)] | ||
| /// Compressed frame for b"hello world hello world hello world" (35 bytes). | ||
| const COMPRESSED: &[u8] = &[ | ||
| 40, 181, 47, 253, 35, 98, 64, 12, 7, 35, 149, 0, 0, 96, 104, 101, 108, 108, 111, 32, 119, 111, | ||
| 114, 108, 100, 32, 1, 0, 175, 75, 18, | ||
| ]; | ||
|
|
||
| #[cfg(zstd_any)] | ||
| const PLAINTEXT_LEN: usize = 35; | ||
|
|
||
| // ------------------------------------------------------------------------- | ||
|
|
||
| #[cfg(zstd_any)] | ||
| fn bench_decompress_with_dict(c: &mut Criterion) { | ||
| use lsm_tree::compression::{CompressionProvider, ZstdBackend as Backend}; | ||
|
|
||
| // Warm benchmark: cache is populated before timing starts. | ||
| // Represents steady-state per-block decompression cost. | ||
| let warm_dict = ZstdDictionary::new(DICT); | ||
| Backend::decompress_with_dict(COMPRESSED, &warm_dict, PLAINTEXT_LEN + 1) | ||
| .expect("pre-warm decompression failed"); | ||
|
|
||
| c.bench_function("decompress_with_dict/warm", |b| { | ||
| b.iter(|| { | ||
| Backend::decompress_with_dict( | ||
| std::hint::black_box(COMPRESSED), | ||
| std::hint::black_box(&warm_dict), | ||
| PLAINTEXT_LEN + 1, | ||
| ) | ||
| .expect("decompression failed") | ||
| }); | ||
| }); | ||
|
|
||
| // "Cold" benchmark: each iteration gets a fresh `ZstdDictionary` handle | ||
| // (new `OnceLock` for the C FFI backend, same dict bytes for both). | ||
| // | ||
| // For the C FFI backend this truly measures first-call cost: a fresh | ||
| // `ZstdDictionary` has an unpopulated `OnceLock`, so `ZSTD_createDDict` | ||
| // is invoked on the first decompression and cached in the handle. | ||
| // | ||
| // For the pure Rust backend the result is different: the TLS decoder is | ||
| // keyed by the 64-bit content hash. All iterations share the same DICT | ||
| // bytes and therefore the same hash, so after the first iteration the TLS | ||
| // entry is still live — subsequent iterations measure the TLS-hit path, not | ||
| // dict parsing. True "cold" cost for the pure Rust backend is therefore | ||
| // only observable on the very first iteration of the first benchmark run. | ||
| c.bench_function("decompress_with_dict/cold", |b| { | ||
| b.iter_batched( | ||
| || ZstdDictionary::new(DICT), | ||
| |d| { | ||
| Backend::decompress_with_dict( | ||
| std::hint::black_box(COMPRESSED), | ||
| std::hint::black_box(&d), | ||
| PLAINTEXT_LEN + 1, | ||
| ) | ||
| .expect("decompression failed") | ||
| }, | ||
| BatchSize::SmallInput, | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| #[cfg(not(zstd_any))] | ||
| fn bench_decompress_with_dict(_c: &mut Criterion) { | ||
| // Neither zstd nor zstd-pure feature enabled — nothing to bench. | ||
| } | ||
|
|
||
| criterion_group!(benches, bench_decompress_with_dict); | ||
| criterion_main!(benches); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.