Repository navigation
perf(osfs): reduce stateless Grep scanning allocations - #1625
Merged
Merged
Conversation
Preserve search semantics while iterating over byte lines and rejecting files without a safe required literal. Add differential coverage and reusable benchmarks. Co-Authored-By: Mecatl <[email protected]>
jhrozek
approved these changes
Sep 17, 2026
jhrozek
left a comment
Contributor
There was a problem hiding this comment.
Reviewed via automated multi-angle diff review (line-by-line, removed-behavior audit, cross-file trace, reuse/simplification/efficiency pass) plus manual verification: the regexp.LiteralPrefix() pre-filter is a sound necessary-condition check, the UTF-8/RuneError guard is load-bearing, Match([]byte) vs MatchString(string) are semantically equivalent here, and no aliasing hazard exists in the byte-to-string conversion. Test suite passes including a differential test against pre-optimization behavior. No findings.
Merged
2 tasks
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
Routine, behavior-preserving optimization of local filesystem Grep. No acceptance-plan or ADR is needed: this applies standard-library operations within the existing scanner contract, without new behavior or interface decisions.
regexp.LiteralPrefixonce per query and reject a file when its required literal is absent. Disable this filter for empty, invalid-UTF-8, or U+FFFD-containing prefixes so Go regexp's invalid-byte semantics remain intact.BenchmarkWorkspaceGrepcases.No new dependencies, indexes, caches, watchers, parallel scanning, flags, public API changes, or changed user-visible search semantics. Public documentation is unchanged for that reason.
Performance evidence
Measured the real
Workspace.Grep, with fixture/workspace setup outside the timer, on identical generated test-owned fixtures. Linux amd64, Intel i7-1365U, warm-cache Go benchmarks; ten 300 ms repetitions per version, compared with benchstat. These measurements are workload-specific, not universal performance guarantees or cold-cache claims.Literal-miss allocation bytes: 7.249 MiB/op -> 3.543 MiB/op (51.1% lower). Rare-literal allocation bytes fall 50.8%. Non-prefix, case-fold, alternation, and ignored nested-worktree timings showed no statistically significant difference in this run.
Tradeoff: match-heavy capped cases allocate about 11% more objects because each returned line gets its own string, while allocated bytes fall about 4%. This avoids retaining whole-file strings through returned line substrings. The short-literal regression is explicitly reported rather than excluded.
Reproduce in a development environment:
To compare revisions, run the same benchmark file with the pre-change scanner and the changed scanner. Raw before/after output and benchstat results are retained locally under
.scratch/grep-stateless-production-20260916/in the implementation worktree.Verification
TestGrep|TestGlobAndGrep)task linttask test(including engine standalone hygiene)go run ./cmd/mecademo(offline)git diff --checkNo confirmed review blockers. One non-blocking test advisory remains: the negative-prefilter cancellation unit test populates the private search field directly rather than exercising prefix derivation through the public method. Public Grep behavior and the prefix derivation path are otherwise covered by semantic tests and benchmarks. UX/DevEx review is not applicable: no operator or exported developer-facing surface changes.
An initial review claim that
string([]byte)repairs invalid UTF-8 was rejected after an executable check and independent adjudication: both old and new conversions preserve bytes; regexp decoding is distinct.PANEL: ship_blockers=0 important=0 advisory=1 reviewer_failures=0