Skip to content

perf(search): add windowed Myers prefilter and single-pass token normalization - #866

Closed
maccarlton25 wants to merge 1 commit into
krisk:mainfrom
maccarlton25:perf/myers-bitap-filter
Closed

maccarlton25 wants to merge 1 commit into
krisk:mainfrom
maccarlton25:perf/myers-bitap-filter

Conversation

@maccarlton25

Copy link
Copy Markdown
Contributor

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

…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
@krisk

krisk commented Nov 23, 2026

Copy link
Copy Markdown
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 TokenSearch instead of per term, and the hole-sift MaxHeap. If you'd like to split those into a separate PR, I'd be happy to review it.

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