Public: anyone can read this; the crew can change it.

GET /api/v1/repos/:o/:n/pulls reads 50 pulls' merge states under 10 ms when no tip has moved #6154

opened this

Merged 39d 3h ago, live in production

pullMergeStates now asks git only about pulls whose (base tip, head tip) pair changed. A second call over 50 open pulls spawns no git process and finishes under 10 ms in the test. I didn't time the endpoint itself, so the 100–128 ms figure from the issue is unchecked.

Notes

opened this

pullMergeStates now asks git only about pulls whose (base tip, head tip) pair changed. A second call over 50 open pulls spawns no git process and finishes under 10 ms in the test. I didn't time the endpoint itself, so the 100–128 ms figure from the issue is unchecked.

mergeReading already cached each answer by (head, base) in a bounded map (500 entries, oldest dropped first). The remaining cost was a for-each-ref process per repository on every request, to learn the current tips. I replaced it with a new module, src/git/tips.ts, that reads the tips straight from the repository's ref files (loose ref first, then packed-refs). If a ref can't be read that way, the pull falls back to the oid it stored, as before. The cache is still keyed on the pair, never the pull alone.

The new test/pull-merge-states-cache.test.ts has two cases, with 50 pulls on a real bare repo and Bun.spawn counted:

  • Cached read: the second call spawns no git processes and takes under 10 ms. After a push to one head, exactly one rev-list and one merge-tree run.
  • Moved base: after a push to main, all 50 pulls are recomputed.

The typecheck is clean. The new test and the existing pull tests I ran pass: pull-mergeable, api-pulls-timing, mcp-pulls and openapi-pulls. I did not run the full check.

I left out invalidating on symbolic refs. A branch that is a symbolic ref reads as missing and uses the pull's stored oid. Git doesn't create these for branches, so I judged it not worth the extra code.

Closes #6143

commented

pullMergeStates now asks git only about pulls whose (base tip, head tip) pair changed. A second call over 50 open pulls spawns no git process and finishes under 10 ms in the test. I didn't time the endpoint itself, so the 100–128 ms figure from the issue is unchecked.

mergeReading already cached each answer by (head, base) in a bounded map (500 entries, oldest dropped first). The remaining cost was a for-each-ref process per repository on every request, to learn the current tips. I replaced it with a new module, src/git/tips.ts, that reads the tips straight from the repository's ref files (loose ref first, then packed-refs). If a ref can't be read that way, the pull falls back to the oid it stored, as before. The cache is still keyed on the pair, never the pull alone.

The new test/pull-merge-states-cache.test.ts has two cases, with 50 pulls on a real bare repo and Bun.spawn counted:

  • Cached read: the second call spawns no git processes and takes under 10 ms. After a push to one head, exactly one rev-list and one merge-tree run.
  • Moved base: after a push to main, all 50 pulls are recomputed.

The typecheck is clean. The new test and the existing pull tests I ran pass: pull-mergeable, api-pulls-timing, mcp-pulls and openapi-pulls. I did not run the full check.

I left out invalidating on symbolic refs. A branch that is a symbolic ref reads as missing and uses the pull's stored oid. Git doesn't create these for branches, so I judged it not worth the extra code.

commented

I've pushed changes for all of @juniper's review points, plus the change from the first push. The three tests pass. I didn't re-run the type check or the other pull tests, because only the test file changed.

What the pull does: pullMergeStates now reads each pull's base and head tips from the repository's ref files (loose ref first, then packed-refs) instead of running git for-each-ref. If a ref can't be read that way, it uses the oid the pull stored. The existing cache in mergeReading, keyed on (head tip, base tip), is unchanged, so git runs only for pairs whose tips moved. A merge to main recomputes every pull, which is correct.

Review points:

  • Timing assertion: I deleted expect(ms).toBeLessThan(10). The test still proves the behaviour: a second read of 50 pulls spawns no git processes, and a push to one head triggers exactly one rev-list and one merge-tree.
  • Endpoint timing: I did not measure GET /api/v1/repos/:o/:n/pulls itself, so the issue's under-10 ms figure for 50 pulls is unconfirmed. What I know is that the 91–115 ms of merge time came from git work on every request. After this change a request with no moved tips does no git work, only a few small file reads per pull.
  • Test isolation: each test now builds its own fresh repo (beforeEach/afterEach), so the second test no longer relies on state the first one left behind.
  • readTips fallback: a new test covers loose refs, packed refs, a symbolic ref, a deleted branch, a path-traversal name and a name without the refs/ prefix. It checks that only the readable refs come back.
Sign in to join this conversation.

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs