Skip to content

perf: scan patch bytes directly for search - #10

Merged
backnotprop merged 1 commit into
mainfrom
perf/search-large
Nov 15, 2026
Merged

backnotprop merged 1 commit into
mainfrom
perf/search-large

Conversation

@backnotprop

Copy link
Copy Markdown
Contributor

Problem

Search on large patches was slow. On the 711 MB Linux v6.0…v7.0 patch (76,872 files), a search from the top took about 9 s to show the first match. An F3 that wrapped around the patch also took about 9 s. The search ran off the input thread, so the UI stayed responsive. It scanned at only about 80 MB/s.

Cause

Domain::scan read every source line through its own reader. For each line, Literal::find resized a 64 KiB buffer with resize(_, 0), twice. In a profile of the search thread, 88% of samples were in __bzero. Every file also allocated a new 64 KiB read window and refilled it.

Fix

  • Files without revealed context are scanned in spool order. memchr::memmem runs over one reusable 1 MiB window (storage::PatchScan), so neighbouring files share reads. A raw occurrence counts only when it lies entirely inside one source record's bytes. A galloping search over the span index finds that record. Signs, headers, paths, hunk function context and \ No newline markers still never match.
  • The spool offset of a record gives its position only when unified rows follow record order. Projection::unified_follows_source checks this. A patch that interleaves -/+ lines inside one change block does not follow it. Such files, and files with revealed context, keep the per-line path. Files that do not bound the search only run this check when they contain a hit.
  • Backward search visits files from the end. memchr's reverse finder has no vector prefilter, so each 16 KiB slice is scanned forward and its hits are checked from the end.
  • The per-line path reads into a buffer that is zeroed once, not once per line.
  • Cancellation is checked at least every 16 KiB. Spool reads bypass the shared page cache, as before.

Search semantics do not change: case-sensitive literal, overlapping matches, source text only, unified order, wrapping once. The 3.6 s refusal while the index loads is unchanged. Searching a partial index needs the Search incomplete reporting from SPEC.md, which is a separate change.

Numbers

Release builds of main (b6f7c8c) and this branch. Apple Silicon Mac, 160×50 PTY, ambient load about 5–7 on 18 cores. Each run checked first that no other perf-video runs were active. Queries: irq_bypass_unregister_consumer (Linux) and getRustPlatform (Bun). Timing runs from the key write to the first synchronized frame that shows the result line.

Search probe: after the index is ready, g, /query Enter, then F3 until the search wraps. 3 runs each:

main this branch
Linux first match 8,941 / 9,118 / 9,772 ms 66 / 62 / 60 ms
Linux F3 that wraps 8,903 / 8,910 / 9,325 ms 58 / 58 / 57 ms
Linux slowest F3 that does not wrap 2,772 / 2,820 / 2,926 ms 23 / 22 / 22 ms
Bun first match (it is near the top) 3.3 / 3.4 / 3.2 ms 3.1 / 3.2 / 3.0 ms
Bun F3 that wraps 617 / 594 / 599 ms 8.0 / 8.1 / 8.0 ms
Peak RSS, Linux open + search 417 / 413 / 411 MiB 410 / 416 / 410 MiB
Peak RSS, Bun open + search 30 / 31 / 31 MiB 31 / 30 / 30 MiB

On both builds, every run showed the same sequence of matches and wrap states.

Single runs on this branch only:

  • A Linux search with no match scans the whole patch in 68 ms.
  • A backward Shift-F3 that wraps takes 58 ms.

Full navigation harness (nav_harness.py … after), 3 runs per build, interleaved, all runs pooled (p50 / p95 / max, ms):

Block Linux main Linux branch Bun main Bun branch
search submit 8,892–9,025 57–236 451–475 8.8–9.5
F3 ×10 8.0 / 8,942 / 9,100 6.2 / 71 / 76 4.9 / 692 / 709 4.9 / 8.3 / 8.3
hold j 30/s 2.8 / 3.8 / 5.4 2.8 / 3.4 / 6.4 3.7 / 4.8 / 10.3 3.6 / 4.6 / 5.8
hold j during no-match search 4.8 / 5.7 / 6.6 4.4 / 4.7 / 7.1 34.5 / 42.5 / 71.4 36.4 / 44.7 / 74.5
PgDn 10/s 3.7 / 5.4 / 8.0 3.7 / 5.5 / 5.7 2.0 / 3.3 / 4.2 2.0 / 2.9 / 3.9
PgUp 10/s 8.1 / 8.8 / 8.8 8.3 / 11.5 / 13.8 3.6 / 5.1 / 6.0 3.7 / 4.6 / 5.4
peak RSS (MiB) 416 / 419 / 413 416 / 416 / 420 51 / 57 / 58 60 / 60 / 59

Notes on the harness numbers:

  • On the branch, the no-match search finishes before the first j, so j is no longer measured while a search runs. On main, Esc cancelled the search in under 0.4 ms.
  • The other blocks are within run-to-run variation, and this change does not touch their code paths.
  • The independent content check found 0 mismatched rows in every run.
  • The F3 block's final screen differs from the old reference on both builds. That reference recorded slow Searching… frames.
  • The Linux search submit and its second F3 ranged from 57 to 236 ms on the branch. Runs 2 and 3 were slower than run 1.

Tests

  • New engine::tests::search_semantics tests compare every search with a brute-force oracle. The oracle lists every occurrence in frozen unified order, and the test checks forward and backward results from every row of every file, at and one byte after each match, and at both ends. Covered cases:

    • context, removed and added lines, interleaved change blocks
    • overlapping hits, case sensitivity, text that starts with +/-
    • headers, paths, hunk function context, binary and rename records, \ No newline markers
    • CRLF, 2,500 small files, hits across 64 KiB and 1 MiB read boundaries
    • revealed context, no match, cancellation

    These tests were written and passed against main first (36 s there, 6 s on this branch).

  • The literal buffer tests now check the buffer's length instead of its capacity.

  • cargo fmt --check, both clippy runs, cargo test --release --locked --no-fail-fast -- --test-threads=1, and the embedded host checks all pass. cargo +1.88.0 check --locked --all-targets and RUSTDOCFLAGS=-D warnings cargo doc --no-deps --document-private-items also pass.

Search read every source line through its own reader and zeroed a 64 KiB
buffer twice per line, so a search ran at about 80 MB/s: 9 s to the first
match on the 711 MB Linux v6.0...v7.0 patch.

Files without revealed context are now scanned in spool order with
memchr::memmem through one reusable 1 MiB window. A raw occurrence counts
only when it lies inside one source record, so signs, headers, paths and
markers still never match. Files whose unified rows do not follow record
order, and files with revealed context, keep the per-line path. The
per-line path no longer zeroes its buffer for each line.

Results are pinned by new tests that compare every search with a
brute-force oracle over the frozen source order.
@backnotprop
backnotprop merged commit e4d9140 into main Nov 15, 2026
3 checks passed
@backnotprop
backnotprop deleted the perf/search-large branch November 15, 2026 20:14
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.

1 participant