# Racing git status, and winning

Sage checks whether an agent's memory is still true before every session. I set it a 300 ms budget on a 100,000-file repo, then went further and made the whole check faster than git status itself, benchmarked with hyperfine. Here is how, with every number on screen.

<FramedImage src="https://assets.crossxr.me/blog/blog17.png" alt="Racing git status, and winning" />

<BuiltWithRust benchmarks="/blog/racing-git-status/benchmarks.html" />

A few days ago I wrote about [why your agent's memory is lying to it](/blog/your-agents-memory-is-lying), and about the half of the problem I decided to build, which is memory you can verify. That work now has a name, Sage, and this post is about the first piece of it, written in Rust from the ground up, that had to be fast enough to disappear. Sage ties every belief an agent holds about a codebase, things like "tests run with `pnpm test:unit`" or "routes are only registered in `routes.ts`", to the files, symbols, searches and commands it came from. When a session starts, Sage checks those anchors against the repo as it is right now, before the agent is handed anything at all.

That check sits in front of every single session, which means people feel it every time, and anything people feel every time eventually gets switched off. So I gave it a hard budget of under 300 ms on a warm 100,000-file repo. I hit that budget, then kept going, and the check now runs in <MemoMark tone="green">47 ms end to end, 1.7 times faster than `git status` on the same repo</MemoMark>, while answering a bigger question than `git status` does. I am genuinely proud of this one, partly because of the number, and partly because almost every speed-up came from choosing a better shape for the data rather than from tuning code.

<RaceBoard />

## Why it had to beat git

Think about how your own memory works when you walk into your kitchen. You do not stop at the door to re-verify where the fridge is, open every cupboard to confirm the cups are still there, and only then reach for one. Your brain checks what it remembers against what your eyes are seeing continuously and almost for free, and you only notice the check when it fails, the moment something has been moved. Recognition at that level is astonishingly fast, and when [Thorpe and colleagues](https://www.nature.com/articles/381520a0) flashed photos at people in 1996, their brain activity had already told photos with animals apart from photos without them within about 150 milliseconds. That speed is the whole point. <MemoMark tone="green">Memory you have to slowly re-verify before you can use it is not memory at all, it is research.</MemoMark>

Agents today live on the wrong side of that line. A coding agent that wants to trust a note about your repo has to re-read the code to confirm it, which is exactly the slow, deliberate work memory was supposed to save. In the [memory post](/blog/your-agents-memory-is-lying) I described Sage the way the brain is built, with a fast path that answers on every turn and a slow cycle that consolidates in the background, and the fast path only works if checking a belief is closer to a reflex than to a thought. If the check is slow, it becomes one more step the agent waits on, and the cheapest fix for a slow step is always to skip it, which drags the agent straight back to re-exploring the codebase from scratch.

That is why `git status` and `git diff` were the bar and not just a nice benchmark to win. They are the commands every developer and every agent already runs without thinking, the things that feel instant, and they are the floor of what "free" means inside a repo. Sage asks a harder question than either of them, whether everything a belief was built on is unchanged since the commit it was formed at, and if answering it cost more than `git status`, it would always feel like overhead sitting in front of the real work. Beating them means the check can sit in front of every session, and eventually every turn, without anyone ever feeling it, which is the only way verified memory gets to behave like a mind instead of like a search.

## Measure before touching anything

I started by building a synthetic repo of 1,000 folders holding 100 small TypeScript files each, with a second commit that moves 100 files, deletes another 100 and edits one. Before changing a line of Sage, I timed every git call the check was making.

| git call (100k files, Windows laptop)      | Time                 |
| ------------------------------------------ | -------------------- |
| Starting git at all (`rev-parse`)          | \~80 ms              |
| `git ls-tree -r` (list one commit's files) | \~365 ms, done twice |
| `git status`                               | \~135 ms             |
| `git diff` against the working tree        | \~131 ms             |

The whole check took somewhere between 0.7 and 1.1 seconds, and the two tree listings alone were most of it. These first timings went through a Git Bash loop, which adds its own overhead to every command, so they were good for ranking the costs and nothing more, and the final comparison later in this post uses hyperfine instead.

## A cache of things that can never change

The obvious move was a cache, and the obvious risk with a cache is serving stale data. For a tool whose whole job is catching stale information, a wrong cache entry is the worst bug it could possibly have, because it would quietly keep a stale belief marked as fresh. The design I settled on sidesteps invalidation entirely by <MemoMark>only caching facts that are immutable, keyed by the thing that determines them</MemoMark>.

A commit's file list is keyed by the commit id, since a commit id always names the same tree, and the diff between two commits is keyed by both ids. Once an id has been verified as a commit, that fact is cached too. Tool versions are the interesting case, because `node --version` can cost up to a second on Windows, so the version is keyed by the binary's path, size and modification time, and an upgrade replaces the file and changes the key on its own. Since nothing in the cache can go stale, it never needs invalidating. Two extra guards came from the same correctness-first instinct. Every entry carries a checksum, so a corrupted entry counts as a miss and never as data, and version-manager shims from volta, asdf and mise are never cached at all, because one shim file runs whichever version the project pins and its identity says nothing about which version that is. Warm runs dropped to about 190 to 240 ms, and on paper the target was met.

## The lazy index that only moved the cost

One optimisation looked great and was not. A snapshot of the repo kept a reverse index from content to paths, used to find files that had moved, and building it for 100,000 files cost about 80 ms, so I made it lazy and built it only when needed. Setup time dropped, but when I re-ran the full benchmark, the time to check all anchors went from 34 ms to between 130 and 184 ms, because the first deleted file triggered the full index build in the middle of the check. The cost had moved, not gone.

The fix came from asking a better question. You only ever look up where some content went for a file that vanished, and it can only have gone to a path that is new since the old commit, so the checker now indexes just those few new paths. I like this one a lot, because the right data structure was obvious the moment the question was precise.

## Can I beat git status?

Once the budget was met I wanted more, and set the check a new bar of being faster than `git status` and `git diff`. The first thing I had to accept was a reality check. As long as Sage calls `git status`, it can never be faster than `git status`, so to win it has to do less work than git, not the same work faster. Before writing anything, I measured the floor, meaning how fast 100,000 files can be listed at all on this machine.

| Directory walk              | Time         |
| --------------------------- | ------------ |
| One thread                  | \~310 ms     |
| `ignore` crate, 12 threads  | 169–188 ms   |
| Bare `read_dir`, 12 threads | **35–42 ms** |

Two things stood out. Listing directories here is latency-bound, so a single thread spends most of its time waiting and having many listings in flight helps enormously. And the bare walk was four times faster than `git status`, which meant there was real room to win.

That led to a native status engine. It finds `.git` without starting git, which saves a whole process launch, and then reads `.git/index` directly. The index lists every tracked file with its blob id, size and modification time, and if a file's size and time still match, it has not changed, which is the same rule git itself uses. The working tree is walked in parallel, and ignore rules are only checked for files git does not track, because tracked files cannot be ignored and so for most of a repo there is nothing to match. Anything the engine does not handle exactly, such as staged changes, merge conflicts, a split or sparse index, a changed symlink or a SHA-256 repo, falls back to real `git status`, which costs speed but never correctness. Every behaviour is pinned down by differential tests that build a working-tree state with edits, deletions, untracked files, nested `.gitignore` rules with negations, force-added files in ignored folders and touched-but-unchanged files, then take the snapshot both natively and through `git status` and require the two to be identical.

## The index is already sorted

The first native version was slower than calling git. The walk took 100 to 130 ms when the bare walk took 35, and instead of guessing at the cause, I bisected it. I rebuilt the scan from the bare walker one feature at a time, timing each step, and found that index lookups, stat comparisons and directory checks each cost almost nothing. The expensive parts were all around the walk. Two hash tables were being built over all 100,000 index entries before the walk could even start, each directory's full listing was collected into a list before being processed, at about 600 bytes per entry on Windows, and idle threads were spinning on the work-queue lock.

The insight that fixed it is that <MemoMark tone="blue">git's index is already sorted by path, so everything under a directory is one contiguous run</MemoMark>, and no hash table is needed at all. Each directory finds its run with a binary search inside its parent's run, each file is looked up inside that small run, and the question of whether a folder contains tracked files becomes whether its run is non-empty. The case-insensitive matching Windows needs moved to after the walk, where it compares the handful of untracked files against the handful of missing ones instead of lower-casing 100,000 paths up front, and idle threads now sleep on a condition variable instead of spinning. The scan went from 102 ms to 24 ms, the same as the bare walk.

## The honest part, in-process numbers flatter you

With all of that in place, Sage reported a full warm check of about 60 ms, against `git status` at about 110 ms on the same machine at the same moment, and I was ready to call it. Then I timed Sage's binary from the outside, exactly the way git was being timed, and got 150 ms. My in-process number had left out two costs that git's number included. The first was starting the process at all, and the second was freeing memory at exit, which took about 45 ms because two snapshots held around 200,000 separately allocated strings, while git deliberately does not free at exit.

Like for like, Sage was roughly level with `git status`, not twice as fast. I caught this before publishing anything, and I had already corrected an earlier before-and-after claim of my own the same way. That is the rule I hold myself to on benchmarks, because <MemoMark tone="red">a number I cannot defend is worse than no number at all</MemoMark>.

## A snapshot is a sorted table, not a hash map

The fix was one more data-structure change. A snapshot answers "what blob is at this path?" for every file, and it used to be a `HashMap<String, String>` with 100,000 entries, which meant 200,000 heap allocations to build it and 200,000 more to free it. Both of its sources are already sorted by path, since git's index is kept in path order and `git ls-tree -r` lists a tree in the same order, so the snapshot now keeps them that way in three flat buffers, with every path back to back in one string, every blob id back to back in another, and one end offset per entry.

|                            | Hash map                        | Sorted table            |
| -------------------------- | ------------------------------- | ----------------------- |
| Allocations for 100k files | \~200,000                       | 3                       |
| Lookup                     | hash and compare                | binary search, 17 steps |
| Compare two snapshots      | look up every path in the other | one merge pass          |
| Time to free               | \~45 ms                         | **0.5 ms**              |

A working-tree snapshot is that table plus a small sorted overlay of the dirty files. With nothing staged, the index is HEAD's file list, so the index's table moves into the snapshot as it is and is never copied, and comparing the commit a belief was formed at with the disk right now becomes a merge of two sorted lists, about 4 ms for 100,000 files.

Profiling the new version showed two smaller costs, both found by timing each phase separately. The index parser allocated a hex string per entry and then copied it into the table, so writing the hex straight into the table cut reading the index from 16 ms to 10 ms. And every directory made an extra system call to ask whether it had a `.gitignore`, when the directory listing already says so. Tracked files never need ignore rules, so they are settled while the listing streams past, and only untracked files and subdirectories wait until the end of the listing, when it is known whether a `.gitignore` was in it. That took the scan from 25 ms to 20 ms.

## Measuring it properly

For the final numbers I used [hyperfine](https://github.com/sharkdp/hyperfine) with `-N`, so every command is started directly with no shell in between, and git and Sage pay exactly the same start-up cost. That also corrected one of my own earlier claims. I had written that starting any process cost 45 to 55 ms on this machine, but that figure came from the Git Bash loop and included bash's own overhead, and measured with hyperfine an empty process starts in about 6 ms while `git rev-parse HEAD` takes 19 ms. I also caught an unfair setup in the other direction. The benchmark was being given the tag `t0`, and resolving a tag starts git, about 18 ms, before any checking happens, but Sage's ledger stores commit ids and never tags, so the fair run passes the id.

The repo is generated by a script, and because tree ids depend only on content, I rebuilt it from scratch and checked that both trees came out byte-identical to the one I measured.

<ProofShot src="https://assets.crossxr.me/blog/racing-git-status/01-setup.png" alt="Terminal showing the machine details and the benchmark repo: 99,900 files, two commits, matching tree ids and a clean working tree" width="1387" height="854" number="01" title="the track" accent="track" tags="Ryzen 5 5600H · Windows 11 · git 2.46">
  An AMD Ryzen 5 5600H laptop with 12 threads, on Windows 11 with git 2.46. The
  repo holds 99,900 tracked files, and the change from `t0` to `HEAD` is 100
  deletions, 100 renames and one edit. Both tree ids match what `make_repo.py`
  produces.
</ProofShot>

Then the race itself, timed from outside the process with 5 warm-up runs and 50 measured runs per command.

<ProofShot src="https://assets.crossxr.me/blog/racing-git-status/02-race.png" alt="hyperfine output comparing git status, git diff, git diff t0 and the Sage check" width="1387" height="762" number="02" title="the race" accent="race" tags="hyperfine -N · 5 warm-up · 50 runs">
  `git status` averages 80.8 ms, `git diff` 52.7 ms and `git diff t0` 123.6 ms.
  The Sage check, from `t0` to the working tree with untracked files included,
  averages 47.0 ms, 41.9 percent below `git status`.
</ProofShot>

| Measured 13 Oct 2026, 100k files, `hyperfine -N`, 50 runs            | Mean        |
| -------------------------------------------------------------------- | ----------- |
| `git status`                                                         | 80.8 ms     |
| `git diff` (index against disk, no untracked files)                  | 52.7 ms     |
| `git diff t0` (what Sage's check actually answers)                   | 123.6 ms    |
| **Sage warm check** (`t0` to working tree, untracked files included) | **47.0 ms** |

Sage is 1.7 times faster than `git status` and 2.6 times faster than `git diff t0`, and it even beats plain `git diff`, which does strictly less work, since it never looks for untracked files and never compares against an older commit. Inside the process, the check takes about 35 to 40 ms and freeing everything takes half a millisecond, while the same check routed through the `git status` fallback takes about 85 ms.

<ProofShot src="https://assets.crossxr.me/blog/racing-git-status/03-inside.png" alt="Per-run timings of the check inside the process, natively and through git status" width="1387" height="578" number="03" title="under the hood" accent="hood" tags="in-process · open + check + free">
  Natively, each run is 36 to 41 ms, of which freeing memory is 0.5 ms. Forcing
  the same check through `git status` with `SAGE_GIT_STATUS=1` takes 84 to 87
  ms, on the same 99,900 files with 101 changed.
</ProofShot>

Speed is only worth anything if the answers are the same. Every file at `t0` is treated as an anchor and checked against the working tree three ways, natively, through `git status` and with the cache switched off, and all three agree on every one of the 202 results that are not Same.

<ProofShot src="https://assets.crossxr.me/blog/racing-git-status/04-correct.png" alt="Anchor results computed natively, through git status and without the cache, all identical" width="1387" height="440" number="04" title="same answers" accent="answers" tags="native · git status · no cache">
  100 Gone, 1 Modified, 100 Relocated and 99,799 Same in all three runs, and a
  line-by-line diff of the per-file results comes back identical.
</ProofShot>

The differential tests compare the native snapshot with `git status` on edits, deletions, untracked files, nested ignore rules with negations, force-added files and case-only renames, and the whole workspace passes alongside a clean clippy run.

<ProofShot src="https://assets.crossxr.me/blog/racing-git-status/05-tests.png" alt="Differential tests, clippy and the full workspace test suite, all passing" width="1387" height="693" number="05" title="all green" accent="green" tags="17 differential · clippy clean · 155 total">
  17 targeted tests check that native status matches git, `cargo clippy` with
  warnings as errors is clean, and the full workspace passes 127, 5 and 23
  tests.
</ProofShot>

## Where it stands

|                       | Before this work                            | Now                               |
| --------------------- | ------------------------------------------- | --------------------------------- |
| Warm check            | \~690 ms (in-process, earlier the same day) | **47 ms** (end to end, hyperfine) |
| Freeing the snapshots | \~45 ms                                     | 0.5 ms                            |

Timed the same way as git, the check is now 1.7 times faster than `git status` and 2.6 times faster than `git diff t0`. The budget was 300 ms, and the check takes <MemoMark tone="yellow">about a sixth of that, including starting the process</MemoMark>. In one day it went from the slowest thing in front of a session to something faster than the command every developer already runs without thinking.

One honest caveat belongs here too. All of this is one Windows laptop and a synthetic repo of tiny files, and Linux and macOS will look different, since git is very fast there, while real repos are messier than any generated one. The whole run is scripted, from the repo generator to the benchmark steps to the screenshots above, so every figure here can be reproduced on a new machine, and the next step is to point the same race at real repos and see how much of that lead survives.

None of this felt certain while it was happening. There were whole stretches where the native engine was slower than the git it was meant to replace, and moments when the only thing I knew for sure was the next measurement I wanted to take. I keep coming back to the same thought whenever I build like this. <MemoMark tone="green">I do things scared, I do things alone, I do things when I'm not sure where they'll lead, and maybe that's what strength is, not knowing but still choosing to go, to build.</MemoMark> Sage is the most ambitious thing I have built that way, and beating `git status` is the first time it has paid me back in a number.


Last updated on October 13, 2026