Skip to content

Latest iteration on ngrams#138

Open
aneubeck wants to merge 8 commits into
mainfrom
aneubeck/fasterngrams
Open

Latest iteration on ngrams#138
aneubeck wants to merge 8 commits into
mainfrom
aneubeck/fasterngrams

Conversation

@aneubeck

Copy link
Copy Markdown
Collaborator

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.

@aneubeck aneubeck requested a review from a team as a code owner July 14, 2026 16:28
Copilot AI review requested due to automatic review settings July 14, 2026 16:28

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 NGram to pack (len + payload) into 27 bits with a bijective mixing permutation; update extraction to build grams from a rolling u64 window.
  • Update docs/benchmarks and add a frozen benchmark fixture corpus; remove the old deque.rs implementation.
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;
Comment thread crates/sparse-ngrams/README.md Outdated
## 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants