Repository navigation
perf(search): the prefilter was matching JSON field names, not commands #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -418,6 +418,86 @@ def prefilter_needles(terms: Iterable[str]) -> list[str]: | |
| return needles | ||
|
|
||
|
|
||
| _COMMAND_KEY = '"command"' | ||
| _COMMAND_PREFIX = '{"command":"' | ||
|
|
||
|
|
||
| def command_span(line: str) -> str: | ||
| """The command text inside a raw JSONL line, without decoding the line. | ||
|
|
||
| The prefilter used to scan the whole line, which includes the field | ||
| *names*. Every record contains ``"command"``, ``"dir"``, ``"exit_code"``, | ||
| ``"duration_ms"``, ``"session"`` and ``"imported"``, so searching for any | ||
| of `command`, `dir`, `exit`, `code`, `port`, `ms`, `ts` or `session` | ||
| matched **every line in the store** and the filter admitted all of them. | ||
| Measured over 20,000 records: `exit` matched 20,000 and `git` matched | ||
| 2,000 — so the queries most likely to be slow were exactly the ones that | ||
| got no speedup at all. | ||
|
|
||
| It was never a correctness bug — ``_matches`` re-checks the parsed | ||
| command, and the command field is the only thing it looks at — which is | ||
| why it went unnoticed. Narrowing the scan to the same field the real | ||
| filter uses makes the two agree. | ||
|
|
||
| Falls back to the whole line for anything it cannot locate: a | ||
| hand-written or truncated record must still be considered, and admitting | ||
| too much is the safe direction. | ||
| """ | ||
| # Fast path: this is how mem itself writes every record, so it is the | ||
| # shape of essentially every line, and it skips the search entirely. | ||
| if line.startswith(_COMMAND_PREFIX): | ||
| index = len(_COMMAND_PREFIX) | ||
| else: | ||
| index = _locate_value(line) | ||
| if index < 0: | ||
| return line | ||
|
|
||
| # Everything below is `str.find`, which runs in C. An earlier version | ||
| # walked the line character by character in Python and cost more than the | ||
| # `json.loads` it was there to avoid — 165ms per 100k records against | ||
| # 137ms for simply parsing everything. A filter slower than the work it | ||
| # skips is worse than no filter. | ||
| cursor = index | ||
| while True: | ||
| end = line.find('"', cursor) | ||
| if end == -1: | ||
| return line # unterminated: scan the whole line rather than none | ||
| # A quote inside a JSON string is escaped, so the closing one is the | ||
| # first preceded by an even number of backslashes. | ||
| backslashes = 0 | ||
| position = end - 1 | ||
| while position >= index and line[position] == "\\": | ||
| backslashes += 1 | ||
| position -= 1 | ||
| if backslashes % 2 == 0: | ||
| return line[index:end] | ||
| cursor = end + 1 | ||
|
|
||
|
|
||
| def _locate_value(line: str) -> int: | ||
| """Index just past the opening quote of the ``command`` value, or -1. | ||
|
|
||
| Only reached for records mem did not write — a hand-edited file, or one | ||
| produced by another tool — so it tolerates whitespace the compact | ||
| encoder never emits. | ||
| """ | ||
| start = line.find(_COMMAND_KEY) | ||
| if start == -1: | ||
| return -1 | ||
| index = start + len(_COMMAND_KEY) | ||
| length = len(line) | ||
| while index < length and line[index] in " \t": | ||
| index += 1 | ||
| if index >= length or line[index] != ":": | ||
| return -1 | ||
| index += 1 | ||
| while index < length and line[index] in " \t": | ||
| index += 1 | ||
| if index >= length or line[index] != '"': | ||
| return -1 | ||
| return index + 1 | ||
|
|
||
|
|
||
| def read_commands( | ||
| repo: str, needles: Sequence[str] | None = None | ||
| ) -> Iterator[CapturedCommand]: | ||
|
|
@@ -439,7 +519,7 @@ def read_commands( | |
| if not line: | ||
| continue | ||
| if needles: | ||
| lowered = line.lower() | ||
| lowered = command_span(line).lower() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This changes only Useful? React with 👍 / 👎. |
||
| if not all(needle in lowered for needle in needles): | ||
| continue | ||
| try: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For hand-edited records or records produced by another tool,
_locate_valueselects 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 ofgitwhileCapturedCommand.from_jsonlyieldsdocker ps, so a search fordockersilently 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 👍 / 👎.