Repository navigation
perf(search): the prefilter was matching JSON field names, not commands - #26
Conversation
The raw-line prefilter scanned the whole encoded record, and every record
contains the field *names* `command`, `dir`, `exit_code`, `duration_ms`,
`session` and `imported`. So searching for `exit` — or `dir`, `command`,
`port`, `code`, `ms`, `ts` — matched **every line in the store**, the filter
admitted all of them, and the queries most likely to be slow were the ones
that got no speedup at all.
Measured over 20,000 records before the fix:
exit -> 20000/20000 lines matched (no filtering whatsoever)
dir -> 20000/20000
command -> 20000/20000
git -> 2000/20000
Never a correctness bug — `_matches` re-checks the parsed command, and the
command field is the only thing it looks at — which is exactly why nobody
noticed. Narrowing the scan to the same field the real filter uses makes the
two agree. Found while building the concept map, where `port` queries hit it
through `"imported":false`.
The first attempt was worse than the bug. Walking the line character by
character in Python to find the field cost **165ms per 100k records against
137ms for simply parsing all of them** — a filter slower than the work it
skips. The version here is `str.find` in C, with a fast path for the shape
mem itself writes and a Python loop only for the rare escaped quote:
exit -> 0 parsed 62ms (was: 100000 parsed, 137ms)
git -> 10096 parsed 76ms
no filter -> 100000 parsed 140ms
A record it cannot recognise — hand-edited, truncated, written by another
tool — is scanned whole. Admitting too much is the safe direction; excluding
a real match is not.
Both failure modes are now pinned: a test that a field name no longer matches
every record, and a `perf` test that a field-name query is not slower than no
filter at all.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 5b8443b297
ℹ️ About Codex in GitHub
Your team has set up Codex to 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 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| produced by another tool — so it tolerates whitespace the compact | ||
| encoder never emits. | ||
| """ | ||
| start = line.find(_COMMAND_KEY) |
There was a problem hiding this comment.
Restrict extraction to the top-level command key
For hand-edited records or records produced by another tool, _locate_value selects the first "command" key at any nesting level rather than the top-level field that Pydantic will deserialize. For example, {"metadata":{"command":"git"},"command":"docker ps","ts":1,"dir":"/x"} yields a span of git while CapturedCommand.from_jsonl yields docker ps, so a search for docker silently skips the valid record. Locate the top-level key or fall back to scanning the entire line when that cannot be guaranteed.
Useful? React with 👍 / 👎.
| continue | ||
| if needles: | ||
| lowered = line.lower() | ||
| lowered = command_span(line).lower() |
There was a problem hiding this comment.
Apply command-only filtering to the interactive finder
This changes only storage.read_commands, but the inspected Ctrl+R path calls tui.rank, whose candidate_lines still executes lowered = line.lower() over the complete JSON record (src/mem/tui.py:187-202). Consequently queries such as exit, command, or port still admit and parse every history entry on every keystroke in the latency-sensitive interactive finder, leaving that user-facing search path unaffected by the optimization.
Useful? React with 👍 / 👎.
The raw-line prefilter scanned the whole encoded record, and every record contains the field names
command,dir,exit_code,duration_ms,session,imported.So a search for
exit— ordir,command,port,code,ms,ts— matched every line in the store. The filter admitted all of them and saved nothing, precisely for the queries most likely to be slow. Measured over 20,000 records:It was never a correctness bug —
_matchesre-checks the parsed command, and the command field is the only thing it looks at — which is exactly why nobody noticed. Narrowing the scan to the same field the real filter uses makes the two agree.Found while building the concept map, where
portqueries hit it through"imported":false.The first attempt was worse than the bug
Walking the line character by character in Python to locate the field cost 165 ms per 100k records — against 137 ms for simply parsing all of them. A filter slower than the work it skips is worse than no filter, and the benchmark caught it before it shipped.
The version here is
str.find, which runs in C, with a fast path for the shape mem itself writes and a Python loop only for the rare escaped quote:exitgitSafety
A record it cannot recognise — hand-edited, truncated, written by another tool — is scanned whole. Admitting too much is the safe direction; excluding a real match is not. The span is the encoded text, which is what the needles are built to match: decoding is the expensive step this path exists to skip.
Tests
Both failure modes are pinned, because each of them is silent:
perftest that a field-name query is not slower than no filter at all — the regression that nearly shipped.Plus the escaped-quote, non-ASCII, tab, unterminated, missing-field and wrong-type cases, each asserting the span equals
json.dumps(command)[1:-1]rather than a hand-written expectation.1309 passed, 90% branch coverage.