Repository navigation
perf(search): add windowed Myers prefilter and single-pass token normalization - #866
Closed
maccarlton25 wants to merge 1 commit into
Closed
maccarlton25 wants to merge 1 commit into
maccarlton25 wants to merge 1 commit into
Conversation
…alization
## Summary
Builds on `0d691dc` / `830626a` (`Int32Array` row reuse and char-code alphabet
lookup) to avoid multi-level Bitap scans on non-matching fields and repeated
text normalization in `TokenSearch`:
1. Windowed Myers (1999) bit-vector prefilter (`src/search/bitap/canMatch.ts`)
- In multi-field searches, most fields never match within `threshold`, yet
every field still entered Wu and Manber's multi-level Bitap loop (`O(m*n)`
across up to `pattern.length` error levels).
- `canMatch` runs Myers' single-pass bit-parallel edit-distance automaton
over the exact `[expected - w, expected + w + 2m]` window that Bitap can
accept (`8` bitwise ops per char, `0` allocations), rejecting non-matches
in one pass.
- Direct `ignoreLocation` fast path: when `ignoreLocation: true` and match
indices are not needed (`!includeMatches && minMatchCharLength <= 1`,
including `TokenSearch` probes without `includeMatches`), Myers' minimum
edit count yields each chunk's exact score (`max(0.001, d / m)`), skipping
the multi-level kernel on matches too.
- Short-circuits exact substring hits via `String.prototype.includes` when
scanning windows wider than 32 characters.
2. Single-pass character mask & hoisted `calcScore` (`src/search/bitap/search.ts`)
- Precomputes a 256-entry `Int32Array` Latin-1 mask table per chunk (falling
back to `PatternAlphabet` above `U+00FF`) and resolves `[low, finish]`
masks once into a shared `charBuf` (capped at `MAX_SHARED_ROW_LENGTH`)
instead of probing `patternAlphabet` on every error level.
- Hoists `calcScore` to module scope to avoid per-call closure allocation
and zeroes only the unwritten `[low, j]` tail between error levels.
3. Single-pass `TokenSearch` normalization (`src/search/token/index.ts`)
- Normalizes each field (`toLowerCase` + `stripDiacritics`) once in
`TokenSearch.searchIn` rather than inside each term's `BitapSearch`.
4. Hole-sift `MaxHeap` (`src/tools/MaxHeap.ts`)
- Replaces 3-write element swaps in `_bubbleUp` and `_sinkDown` with
single-write hole sifts.
## Benchmarks (`node bench/search.mjs` vs. `main` @ `fe90da8`)
Measured on Node.js v24 (10k-doc corpus from `bench/search.mjs`, plus 50 cold
unique `ignoreLocation: true` queries on `books.json` scaled to 4,600 books):
| Benchmark | `main` | This PR | Speedup |
| --- | --- | --- | --- |
| `fuzzy "apple"` (10k docs) | `34.13 ms` | `16.59 ms` | `2.06x` |
| `fuzzy "javascrpt"` (10k docs) | `45.32 ms` | `11.65 ms` | `3.89x` |
| `fuzzy "apple" +matches` (10k docs) | `40.12 ms` | `22.35 ms` | `1.80x` |
| `fuzzy "javascrpt" +matches` (10k docs) | `51.58 ms` | `14.26 ms` | `3.62x` |
| `fuzzy "apple" limit=10` (10k docs) | `29.90 ms` | `14.40 ms` | `2.08x` |
| `token "apple"` (10k docs) | `44.13 ms` | `15.23 ms` | `2.90x` |
| `token "javascrpt"` (10k docs) | `64.01 ms` | `16.88 ms` | `3.79x` |
| `token "javascript optimization"` (10k) | `129.60 ms` | `19.03 ms` | `6.81x` |
| Scaling `fuzzy "apple"` (50k docs) | `165.29 ms` | `89.37 ms` | `1.85x` |
| 50 cold queries on `books.json` (4.6k) | `38.8 ms` | `4.3 ms` | `9.06x` |
## Bundle Size
| Artifact | `main` (raw / gzip) | This PR (raw / gzip) | Delta |
| --- | --- | --- | --- |
| `dist/fuse.min.mjs` | `29,679 / 10,329 B` | `30,959 / 10,903 B` | `+574 B gz` |
| `dist/fuse.basic.min.mjs` | `19,743 / 7,410 B` | `20,894 / 7,925 B` | `+515 B gz` |
TypeScript declaration outputs (`dist/*.d.ts`, `dist/*.d.cts`) have zero diff
against `main`.
## Verification
- `npm run lint` — 0 errors, 0 warnings
- `npm run typecheck` — pass
- `npm run build` — all CJS, ESM, minified, worker, and `.d.ts` targets built
- `npm test` — 28/28 test files (`551/551` tests) pass
- `npm run docs:build` — VitePress build clean
Owner
|
Thanks for this, really nice work, and the benchmarks are thorough. I've actually been working on the same problem from a slightly different angle: replacing Bitap with a single-pass Myers matcher entirely rather than using it as a prefilter, which ends up faster across the board and always reports the exact best score. That's landing in the next 7.6 beta, so I'm going to close this one rather than merge it. Two parts of this stand on their own though: normalizing each field once in |
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.
Summary
Builds on
0d691dc/830626a(Int32Arrayrow reuse and char-code alphabetlookup) to avoid multi-level Bitap scans on non-matching fields and repeated
text normalization in
TokenSearch:Windowed Myers (1999) bit-vector prefilter (
src/search/bitap/canMatch.ts)threshold, yetevery field still entered Wu and Manber's multi-level Bitap loop (
O(m*n)across up to
pattern.lengtherror levels).canMatchruns Myers' single-pass bit-parallel edit-distance automatonover the exact
[expected - w, expected + w + 2m]window that Bitap canaccept (
8bitwise ops per char,0allocations), rejecting non-matchesin one pass.
ignoreLocationfast path: whenignoreLocation: trueand matchindices are not needed (
!includeMatches && minMatchCharLength <= 1,including
TokenSearchprobes withoutincludeMatches), Myers' minimumedit count yields each chunk's exact score (
max(0.001, d / m)), skippingthe multi-level kernel on matches too.
String.prototype.includeswhenscanning windows wider than 32 characters.
Single-pass character mask & hoisted
calcScore(src/search/bitap/search.ts)Int32ArrayLatin-1 mask table per chunk (fallingback to
PatternAlphabetaboveU+00FF) and resolves[low, finish]masks once into a shared
charBuf(capped atMAX_SHARED_ROW_LENGTH)instead of probing
patternAlphabeton every error level.calcScoreto module scope to avoid per-call closure allocationand zeroes only the unwritten
[low, j]tail between error levels.Single-pass
TokenSearchnormalization (src/search/token/index.ts)toLowerCase+stripDiacritics) once inTokenSearch.searchInrather than inside each term'sBitapSearch.Hole-sift
MaxHeap(src/tools/MaxHeap.ts)_bubbleUpand_sinkDownwithsingle-write hole sifts.
Benchmarks (
node bench/search.mjsvs.main@fe90da8)Measured on Node.js v24 (10k-doc corpus from
bench/search.mjs, plus 50 coldunique
ignoreLocation: truequeries onbooks.jsonscaled to 4,600 books):mainfuzzy "apple"(10k docs)34.13 ms16.59 ms2.06xfuzzy "javascrpt"(10k docs)45.32 ms11.65 ms3.89xfuzzy "apple" +matches(10k docs)40.12 ms22.35 ms1.80xfuzzy "javascrpt" +matches(10k docs)51.58 ms14.26 ms3.62xfuzzy "apple" limit=10(10k docs)29.90 ms14.40 ms2.08xtoken "apple"(10k docs)44.13 ms15.23 ms2.90xtoken "javascrpt"(10k docs)64.01 ms16.88 ms3.79xtoken "javascript optimization"(10k)129.60 ms19.03 ms6.81xfuzzy "apple"(50k docs)165.29 ms89.37 ms1.85xbooks.json(4.6k)38.8 ms4.3 ms9.06xBundle Size
main(raw / gzip)dist/fuse.min.mjs29,679 / 10,329 B30,959 / 10,903 B+574 B gzdist/fuse.basic.min.mjs19,743 / 7,410 B20,894 / 7,925 B+515 B gzTypeScript declaration outputs (
dist/*.d.ts,dist/*.d.cts) have zero diffagainst
main.Verification
npm run lint— 0 errors, 0 warningsnpm run typecheck— passnpm run build— all CJS, ESM, minified, worker, and.d.tstargets builtnpm test— 28/28 test files (551/551tests) passnpm run docs:build— VitePress build clean