Skip to content

perf(global-search): cache normalized fields across keystrokes - #3960

Merged
kev1n77 merged 1 commit into
openBitFun:mainfrom
Raing5Days:openbitfun/search-match-field-cache
Nov 19, 2026
Merged

kev1n77 merged 1 commit into
openBitFun:mainfrom
Raing5Days:openbitfun/search-match-field-cache

Conversation

@Raing5Days

Copy link
Copy Markdown
Contributor

Summary

scoreTextMatch runs once per candidate row per keystroke. Every call re-derived the same normalized strings:

  • rawField.trim().toLocaleLowerCase() ran for every field of every row on every keystroke, although field text does not change between keystrokes while the query does.
  • The multi-token working list was allocated even for single-token queries, which are the common case.
  • Per-token scoring spread a mapped array into Math.max, allocating an argument list per token per row.

Fields now come from a small bounded cache keyed by the raw string (empty results are not cached, and the cache clears past 1024 entries), the working list is built only when the query has more than one token, and the scoring loop accumulates in a loop. Behaviour is unchanged; searchMatching.test.ts and an equivalence check against the previous implementation (15 queries × 8 field sets, warm and cold cache, plus churn past the cache cap) agree on every input.

The same change-set also skips entering the fence regex in sanitizeInlineAiMarkdownResponse when a streamed reply does not start with a fence — the pattern is anchored at ^, so the match could not succeed, but String.match still built its match state. Output is identical across 27 hand-written and 336 generated fence/body/tail combinations.

Validation

  • node scripts/run-vitest.mjs run src/app/global-search/searchMatching.test.ts src/tools/editor/meditor/utils/inlineAi.test.ts — 9 passed
  • pnpm run type-check:web — passed
  • Microbenchmark, scoreTextMatch('provider', [title, subtitle, keywords]): 0.617 µs → 0.320 µs per call (median of 3 runs after the change, worst call 0.372 µs); the budget derived from the pre-change baseline was 0.432 µs
  • Microbenchmark, sanitizeInlineAiMarkdownResponse on a 1.7 KB reply: 0.073 µs → 0.040 µs per call
  • pnpm run check:web — fails on main at the miniapp-chrome-theme generated-artifact check; the failing output is identical with and without this diff

Both numbers are per-call costs under these workloads, not frame times. The search benchmark passes a short query and three short fields, which is how the federated search engine calls it; the sanitizer benchmark passes a 1.7 KB reply.

Generated with OpenBitFun

@Raing5Days
Raing5Days marked this pull request as ready for review November 17, 2026 03:12
scoreTextMatch runs once per candidate row per keystroke, and it re-derived every
row's lowercase form on every call: `rawField.trim().toLocaleLowerCase()` for each
field, plus an allocation for the multi-token working list.

Fields do not change between keystrokes, so the normalized form is now looked up
from a small bounded cache keyed by the raw string, the working list is only built
for multi-token queries, and the per-token scoring loop no longer spreads a mapped
array into Math.max.

Measured 0.617 us -> 0.320 us per call for a realistic workload (query "provider"
against a row's title, subtitle and keywords); behaviour is unchanged.
@Raing5Days
Raing5Days force-pushed the openbitfun/search-match-field-cache branch from ad7b1f7 to b574079 Compare November 17, 2026 03:47
@kev1n77
kev1n77 merged commit bcefe1a into openBitFun:main Nov 19, 2026
9 checks passed
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