fix(index): bound searchPrefix allocation by max_results#295
Merged
Conversation
Prefix expansion in WordIndex.searchPrefix appended every deduplicated hit for every matching key with no upper bound. The Tier 0.5 search flow in explore.zig only enforced max_results AFTER searchPrefix returned, so a broad 3-char prefix on a large index could allocate a huge hit set before truncation — causing latency spikes / OOM despite callers asking for a small limit. Fix: thread max_results through searchPrefix. Pre-size the result list and dedup map to the cap, append with appendAssumeCapacity, and short-circuit both the inner hits loop and the outer key iteration once the cap is reached. Addresses Codex P2 review feedback on PR #293. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
8f2fc19 to
b70548d
Compare
Benchmark Regression ReportThreshold: 10.00%
|
💡 Codex ReviewLines 602 to 603 in 8f2fc19 In ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
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
max_results: usizeparameter toWordIndex.searchPrefix(src/index.zig:271) so the prefix-key scan stops once the cap is reached, instead of collecting every deduplicated hit and relying on the caller to truncate.appendAssumeCapacity, and break out of both the innerhitsloop and outerkeyIteratoronceresult.items.len >= max_results.Explorer.searchContent(src/explore.zig:1314), to pass the user-requestedmax_results.Addresses Codex's P2 review on #293: on large indexes a broad 3-char prefix could allocate a huge hit set before the post-call truncation in
searchContent, causing latency spikes / OOM despite a small caller limit.Test plan
zig build test— 388/388 pass (387 baseline + 1 newword-index: searchPrefix respects max_results cap).fooBarprefix and assertssearchPrefix("foobar", a, 5)returns at most 5 hits.searchPrefixtests updated to pass an explicit cap (32) and continue to pass.🤖 Generated with Claude Code