Repository navigation
perf(hopper): resolve string search records once per query - #1229
Conversation
search_strings called _string_inventory(document) once per match, and each call sorts every string address and rebuilds the record dict, so a query cost O(matches x strings log strings). Resolve the inventory once per search. Warm search, 50% matches, fake document, best of 7: 1000 strings 186 -> 0.5 ms, 4000 strings 3161 -> 2.1 ms, 8000 strings 13121 -> 4.9 ms; output byte-identical.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: c1afce8f66
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
Resolving the full string inventory for a query with no matches added a sort of every string address that the previous code never performed. Return early when nothing matches. Zero-match search on a warm document, best of 7: 64000 strings 35.9 ms before this change, 4.0 ms after (main: 3.9 ms).
|
Thank you for this Hopper performance fix. I reviewed how search_strings now resolves the string records once per query instead of rebuilding the sorted inventory and record map for every match, with byte identical results. Dropping the warm search from about 12.6 seconds to milliseconds at 8000 strings is a huge practical win. Sincerely appreciate the sharp profiling and the new probe coverage. |
) * perf(hopper): resolve string search records once per query search_strings called _string_inventory(document) once per match, and each call sorts every string address and rebuilds the record dict, so a query cost O(matches x strings log strings). Resolve the inventory once per search. Warm search, 50% matches, fake document, best of 7: 1000 strings 186 -> 0.5 ms, 4000 strings 3161 -> 2.1 ms, 8000 strings 13121 -> 4.9 ms; output byte-identical. * perf(hopper): skip record resolution for empty string searches Resolving the full string inventory for a query with no matches added a sort of every string address that the previous code never performed. Return early when nothing matches. Zero-match search on a warm document, best of 7: 64000 strings 35.9 ms before this change, 4.0 ms after (main: 3.9 ms).
Summary
search_stringson a Hopper document becomes quadratic in the number of matches: every matching string re-runs_string_inventory(document), which sorts every string address in the document and rebuilds a dictionary of all records. On a document with 8,000 strings where half match, one warm search takes about 12.6 s; at 16,000 strings it takes about 58 s. This change resolves the string records once per query, so the same searches take 4.4 ms and 11.1 ms, with byte-identical results.No issue is linked: this is a small, self-contained performance fix, and the measurements below describe the problem and the result.
Problem and expected behavior
Trigger:
search_stringswith a literal pattern that matches many strings in a document with many strings.In
bridge/hopper_bridge.py,_search_resultsbuilds the string results with:_string_inventory(document)is evaluated once per selected match. Withaddresses=None, each call runssorted()over every string address and builds a new{hex address: record}dictionary for all of them. A query with M matches over S strings therefore costs O(M × S log S), although the records themselves are already cached in_search_inventory_cache(the"string"search inventory is built from_strings(), which populates every record on first use).Expected: one search costs one pass over the inventory, O(S log S + M), and returns exactly the same records in the same order.
Change and scope
_search_resultsnow calls_string_inventory(document)once for a string query and indexes that dictionary for each match:The full-inventory form is kept on purpose: it is the same call with the same arguments, so record content, key normalization, decoding-unavailable records and ordering are unchanged. Procedure and name searches are untouched.
Not included: no new caches, no change to
_string_inventory,_stringsor_search_inventory, and no other bridge changes.Contract and boundary impact
_search_resultsinbridge/hopper_bridge.py.search_stringsinput and output are unchanged.docs/public/product-catalog.json), package, or installation impact: none; no generated outputs committed.Evidence and regression coverage
Tests added or updated:
tests/fixtures/hopperStringSearchProbe.py: loads the real bridge against a Hopper-shaped document with three segments listed out of address order, ASCII and UTF-16 strings, and one string whose object extent is invalid. Expected results are computed independently in the probe from the raw string table. Seven queries run twice, so the second pass exercises the cached inventory.tests/boundary/providers/hopper/hopperStringSearch.test.ts:mainbefore the change and after it, confirming the output is unchanged.Base reproduction or other evidence: the new tests pass on unchanged
main(b34bdcdb2) and on this branch, so the output is unchanged. Each mutant below was applied to the final fix and the new tests were run; all were caught, and the restored code passes 3/3.decodingfield from recordsPerformance: measured with a bridge-level benchmark that loads the unmodified
bridge/hopper_bridge.pyfrommainand the changed file side by side against the same fake Hopper document (four segments). Measured at089bbf58f;bridge/hopper_bridge.pyis byte-identical atb34bdcdb2. A and B runs are interleaved, 7 repetitions each, caches warmed by one search first; times are best / median. At 16,000 strings with 8,000 matches,mainwas run once because a single search takes about a minute.maina74cbb07a1a764f0e6ae0e822e1370b92f09a5afd1b106b3838700fea1506edbd217c7ac3d5f2c9fd91d617d0f0b8ed7First search on a fresh document (3 repetitions, best / median):
mainUser-visible CLI/MCP output: unchanged; only latency changes.
Remaining proof gaps: real-Hopper verification was not performed (see Compatibility, safety, and release). The fake document implements the segment methods the bridge calls; real Hopper native reads are slower than the fake's, and those reads are unchanged by this PR.
For evidence-bearing changes:
Validation performed
Run on this branch, rebased on
mainatb34bdcdb2, in a Linux arm64 container with Node 24:npm run build:unlockednpx vitest run tests/boundary/providers/hopper/hopperStringSearch.test.tsnpx vitest run tests/boundary/providers/hoppermainatb34bdcdb2: 15 files, 108 tests passed (the difference is the new file)npm run checknpm run verify:test-discoverynpx vitest run --project mcp-boundarynpm run test:fastCompatibility, safety, and release
npm run verify:hopper:linuxfailed on unchangedmain(089bbf58f) before reaching any analysis, because the emulator reports itself as every process's executable and REA's launched-process ownership check correctly refuses it (exit code 74). The change makes no new or different Hopper API calls.Review checklist
type(scope): outcome.