Latest iteration on ngrams#138
Open
aneubeck wants to merge 8 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors sparse-ngrams to reduce bigram-priority memory footprint and to speed up extraction by removing per-gram slicing/hashing, replacing it with (1) a compact factored bigram-priority model and (2) a rolling 8-byte window for NGram construction.
Changes:
- Replace the old lazily-built 256×256 bigram table with a compact factored model (
BIGRAM_H+ packed 4-bit per-bigram correction). - Redesign
NGramto pack (len + payload) into 27 bits with a bijective mixing permutation; update extraction to build grams from a rollingu64window. - Update docs/benchmarks and add a frozen benchmark fixture corpus; remove the old
deque.rsimplementation.
Show a summary per file
| File | Description |
|---|---|
| crates/sparse-ngrams/src/table.rs | Replaces full bigram lookup table with a compact factored bigram-priority model and adds tests. |
| crates/sparse-ngrams/src/ngram.rs | Redesigns NGram encoding (27-bit packed + mix/unmix), changes constructors, and updates tests/debug formatting. |
| crates/sparse-ngrams/src/lib.rs | Updates crate docs and re-exports bigram_priority; removes old table-size constant export. |
| crates/sparse-ngrams/src/extract.rs | Reworks extraction to use rolling window + rolling bigram priority; updates brute-force reference + tests. |
| crates/sparse-ngrams/src/deque.rs | Deletes the old fixed deque implementation (no longer used). |
| crates/sparse-ngrams/README.md | Updates algorithm/model documentation and performance notes to match the new implementation. |
| crates/sparse-ngrams/benchmarks/performance.rs | Switches benchmark input to a frozen fixture file. |
| crates/sparse-ngrams/benchmarks/fixtures/sample_code.txt | Adds a committed “large” benchmark corpus to prevent benchmark drift. |
Review details
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 8/11 changed files
- Comments generated: 5
- Review effort level: Low
Comment on lines
+16
to
+18
| //! * **Payload** (bits 0..24): for substrings of at most 3 bytes the bytes are packed | ||
| //! losslessly (left-aligned, so distinct short grams never collide); longer substrings are | ||
| //! hashed down to 24 bits with a multiplicative hash. |
Comment on lines
+150
to
155
| /// Whether this represents an empty gram. Valid n-grams are always at least 2 bytes long, so | ||
| /// this only holds for a default-constructed placeholder. | ||
| #[inline] | ||
| pub fn is_empty(&self) -> bool { | ||
| self.len() == 0 | ||
| } |
| let mut queue = FixedDeque::<MAX_SPARSE_GRAM_SIZE>::new(); | ||
| let mut prefix_hashes = [0u32; MAX_SPARSE_GRAM_SIZE]; | ||
| prefix_hashes[1] = content[0] as u32; | ||
| const MASK: usize = MAX_SPARSE_GRAM_SIZE - 1; |
| assert!(out.len() >= max_sparse_grams(n)); | ||
|
|
||
| let table = get_bigram_table(); | ||
| const MASK: usize = MAX_SPARSE_GRAM_SIZE - 1; |
| ## Caveats | ||
|
|
||
| The integrated bigram table contains only lowercase ASCII bigrams. Callers should lowercase and normalize input before extraction (e.g. fold uppercase to lowercase, map non-ASCII bytes to a single sentinel value). This makes the implementation suitable for case-insensitive search indexes. | ||
| The bigram priority model only scores lowercase ASCII byte pairs; any byte with the high bit set resolves to priority `0`. Callers should lowercase and normalize input before extraction (e.g. fold uppercase to lowercase, map non-ASCII bytes to high-bit-set bytes). This makes the implementation suitable for case-insensitive search indexes. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Note: I might test a different table with more bigrams from a fresh index...
But that requires integration with the reindex code and comparing actually disk size.