Improve prediction popup latency: domain settings cache + remove tabs.get pre-flight#291
Merged
bartekplus merged 1 commit intomasterfrom Mar 9, 2026
Merged
Conversation
…ing tabs.get pre-flight
Two performance bottlenecks were causing ~15-25 ms of avoidable overhead on
every keystroke prediction:
1. `resolveDomainRuntimeSettings` was called on every prediction request,
performing 5 parallel chrome.storage reads (~5-15 ms) even though domain
settings virtually never change between keystrokes.
Fix: introduce `DomainSettingsCache` (500 ms TTL, per-domain key) in
`MessageRouter`. The cache is immediately invalidated when
`CMD_OPTIONS_PAGE_CONFIG_CHANGE` is received so settings changes still
take effect without waiting for expiry.
2. `BackgroundServiceWorker.runPrediction` called `chrome.tabs.get` before
every `sendMessage` as a tab-existence pre-flight, adding ~5-10 ms of
extra IPC latency. `sendMessage` already throws when the tab/frame is
gone, so the pre-flight was redundant.
Fix: call `chrome.tabs.sendMessage` directly and catch the error.
Performance gain per keystroke: ~15-25 ms less background-side overhead.
50 consecutive predictions for the same domain now incur 1 storage read
instead of 250.
Tests added:
- `tests/DomainSettingsCache.test.ts` — 11 unit tests covering cache hit/miss,
TTL expiry, per-domain isolation, invalidation, and a latency benchmark
asserting cached requests are ≥5× faster than uncached ones.
- Two regression tests in `tests/background.routing.test.ts`:
- Verifies `chrome.tabs.get` is NOT called during prediction dispatch.
- Verifies the domain settings cache is invalidated after a config change
so the next prediction sees fresh language/settings values.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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
DomainSettingsCache— cachesresolveDomainRuntimeSettingsper domain URL with a 500 ms TTL inMessageRouter. Previously, every keystroke prediction triggered 5 parallelchrome.storagereads (≈5–15 ms overhead). Now 50 consecutive predictions for the same domain cost 1 storage read instead of 250. Cache is immediately invalidated onCMD_OPTIONS_PAGE_CONFIG_CHANGE.chrome.tabs.getpre-flight —BackgroundServiceWorker.runPredictionwas callingchrome.tabs.get(tabId, callback)before everysendMessagejust to check if the tab existed, adding ≈5–10 ms of IPC latency.sendMessagealready throws if the tab is gone, so the check was redundant. Now sends directly.Total saving per keystroke: ~15–25 ms of background overhead.
Tests run
bun run lint✅bun run format:check✅bun run test✅ — 1304 pass, 0 failbun run test:e2e(pending CI)bun run check:e2e:coverage(pending CI)New tests added:
tests/DomainSettingsCache.test.ts— 11 unit tests: cache hit/miss, TTL expiry, per-domain isolation, invalidation, and a latency benchmark (cached path ≥5× faster than uncached with 2 ms simulated storage delay).tests/background.routing.test.ts:chrome.tabs.getis not called during prediction response dispatch.CMD_OPTIONS_PAGE_CONFIG_CHANGEso the next prediction sees the updated language.🤖 Generated with Claude Code