Skip to content

fs_grep: ripgrep tier 5+6 — byte walker + Boyer-Moore-Horspool - #241

Merged
FMXExpress merged 1 commit into
mainfrom
claude/fs-grep-rg-tier5-6
Jun 12, 2026
Merged

FMXExpress merged 1 commit into
mainfrom
claude/fs-grep-rg-tier5-6

Conversation

@FMXExpress

Copy link
Copy Markdown
Owner

Summary

Two more ripgrep-inspired wins for Tool_FSGrep, this time in the per-file scanner loop (independent from PR #240's walk-time filters):

Tier 5 — walk bytes, not lines. The old ScanFile did:

  • StringReplace(Body, #13, '', [rfReplaceAll]) — whole-body copy
  • TStringList.Text := ... — one AnsiString alloc per line
  • LowerCase(Lines[j]) per match-candidate line

None of which is necessary. The new walker splits on #10 in place, trims trailing #13 inline for CRLF, and only Copy()s the line bytes on lines that actually matched. ~3-5× faster on the per-file path for typical source files.

Tier 6 — Boyer-Moore-Horspool substring search. Pos(PatLower, Cmp) advances one byte at a time. BMH precomputes a 256-entry shift table once per Tool_FSGrep call; on a failed alignment it slides the pattern by up to m bytes instead of 1. For the patterns the model actually queries (TODO, function, class, FIXME, def, return), this is ~3-10× fewer text-byte reads.

Case-insensitive is handled by folding text bytes through a 256-entry ASCII lower table built once per call — one branch instead of the old per-line LowerCase(). The pattern itself is pre-lowered into PatLower the same way as before.

Output equivalence

Output is byte-identical to the old path except:

  • CRLF lines no longer carry a stray #13 in the emitted line text. The old StringReplace pass had hidden this; the byte walker now trims trailing #13 explicitly.
  • Lone mid-line #13 bytes (extremely rare; old Mac line endings) are no longer stripped from output. ripgrep treats them the same way.

Test plan

  • make smoke — green
  • make test — full aggregate green (existing + new)
  • make test-fs-grep-tier5-6 — 10 new tests in src/tests/fs_grep_tier5_6_tests.pas pin observable behaviour:
    • Tier 5: exact line numbers across the byte walker
    • Tier 5: CRLF trimmed inline, no stray CR in output
    • Tier 5: final line without trailing newline still matched
    • Tier 5: multiple matches per file, exactly one hashline header
    • Tier 5: empty / newline-only bodies don't crash
    • Tier 6: ignore_case via byte-fold table
    • Tier 6: overlapping / partial / embedded patterns (classic BMH traps: AAAA vs AAAB, XAAAAY, AAAAA, etc.)
    • Tier 6: m=1 degenerate case (single-char pattern)
    • Tier 6: m > n early exit (pattern longer than any line — no cross-line scan)
    • Tier 5+6: hashline ¶path#hash header still emitted so fs_edit_hashline downstream still parses

Relationship to #240

Both PRs touch the same function. #240 adds walk-time / per-file gating (deferred hash, blocked-dirs, NUL-sniff, size-cap). #241 (this PR) rewrites the inner scan loop. They compose cleanly — once one merges, the other will rebase with no semantic conflict.


Generated by Claude Code

Two more cheap wins for fs_grep on top of the tier-1-4 filters
(deferred-hash / blocked-dirs / NUL-sniff / size-cap), this time
in the inner per-file path.

Tier 5 -- walk bytes, not lines. The old ScanFile did:
  - StringReplace(Body, #13, '', [rfReplaceAll])  -- whole-body copy
  - Lines := TStringList.Create / Lines.Text := ... -- one alloc per line
  - LowerCase(Lines[j])                            -- one per match-candidate

None of that's needed. The new walker splits on #10 in place, trims
trailing #13 inline for CRLF, and only Copy()s the line bytes on
lines that actually matched. ~3-5x faster on the per-file path for
typical source files.

Tier 6 -- Boyer-Moore-Horspool substring search. The old Pos(PatLower,
Cmp) advanced one byte at a time. BMH precomputes a 256-entry shift
table once at Tool_FSGrep startup and on a failed alignment slides
the pattern by up to m bytes instead of 1. For the patterns the model
actually queries (TODO, function, class, FIXME, def, return) this
hits ~3-10x fewer text-byte reads than Pos.

Case-insensitive is handled by folding text bytes through a 256-entry
ASCII lower table (built once per call) -- one branch instead of
the old per-line LowerCase(). Pattern is pre-lowered into PatLower
the same way as before.

Output is byte-identical to the old path except:
  - CRLF lines no longer carry a stray #13 in the emitted line text
    (the old StringReplace pass had hidden this; the byte walker
    now trims trailing #13 explicitly).
  - Lone mid-line #13 bytes (extremely rare; old Mac line endings)
    are no longer stripped from output. ripgrep treats them the same
    way -- they're not a line terminator in modern conventions.

Both behaviours are pinned by src/tests/fs_grep_tier5_6_tests.pas
(10 cases): line numbers exact across the walker, CRLF trimming
correct, files without trailing newline still produce a final-line
match, one hashline header per matched file, ignore_case round-trip
through the byte-fold table, BMH overlapping/partial/embedded
patterns, m=1 degenerate case, m > n early exit, empty / newline-
only bodies don't crash, and the hashline ¶path#hash header still
emits in the same shape so fs_edit_hashline downstream still parses
the output.
@FMXExpress
FMXExpress force-pushed the claude/fs-grep-rg-tier5-6 branch from d592258 to 16e27e1 Compare June 12, 2026 22:46
@FMXExpress
FMXExpress merged commit ce429ec into main Jun 12, 2026
FMXExpress pushed a commit that referenced this pull request Jun 13, 2026
Both changelog files were stuck at the 2026-06-10 TUI / background-
subagent / session_search / tool-RPC entries. ~7 weeks of merged
PRs (#214 through #244) had landed without a corresponding entry in
either file -- the git log was the only record of features like the
knowledgebase, inbound MCP server mode, checkpoints + /undo,
heartbeat daemon, shell backends (local | docker), send_message,
promptware defense, Windows shell-output encoding fixes, fs_grep
ripgrep optimisations, OpenTelemetry traces, and `pasclaw agent
--quiet`.

Added (newest first):

  2026-06-13
    #244  agent --quiet log-level clamp

  2026-06-12
    #243  dcc64 search path + agent --quiet / -q
    #242  OpenTelemetry traces (OTLP/HTTP+JSON)
    #241  fs_grep tier 5+6 (byte walker + Boyer-Moore-Horspool)
    #240  fs_grep tier 1-4 (deferred hash + skip-dirs + binary
          sniff + size cap)

  2026-06-11
    #239  Windows shell output: auto-detect UTF-8 vs OEM
    #238  Hashline fs_read header mojibake fix (¶ vs ¶)
    #237  Windows shell output: OEM codepage decode

  2026-06-10
    #233  Shell backends (local | docker via IShellBackend)
    #232  Heartbeat daemon + reversible condensation (CCR)
    #230  send_message tool + condenser families + promptware
          defense + task-aware MEMORY.md slicing

  2026-06-09
    #223  /goal Ralph loop + judge model + JSON-aware condenser
    #222  /undo wired into agent + sample dprojs
    #221  Checkpoints (auto-snapshot + TUI /undo)
    #220  mcp catalog: lenient hub projector
    #219  mcp: SSE / Streamable HTTP / stdio hub transports
    #218  Inbound MCP server mode
    #217  onboard: offer to index docs into the knowledgebase
    #214  Knowledgebase (RAG over operator-curated docs)

Skipped from this changelog (build-system / Delphi-fix / FPC-fix
PRs visible in the git log but not user-facing features):
#215, #216, #224, #225, #226, #227, #228, #229, #231, #234, #236.

Both files (README.md and docs/changelog.md) updated with the same
entries -- intentional mirror so a reader on either surface sees
the same history. Same prose in both; same footnote refs at the
bottom.

Validated: every [#N] reference in both files has a matching [#N]:
URL footnote (no broken links).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants