Repository navigation
Conversation
FIFO mode polled the received-mutation queue every 4ms, so each mutation waited for the next tick. That wait was most of its latency, and the loop woke 250 times per second when idle. FIFO now blocks on Queue.takeAll and accepts everything that queued during the previous batch. Batch mode keeps its fixed window for batchOrder. Both modes take mutations outside the speculative-state lock. Counter benchmark (sequential FIFO): 4.04ms -> 1.56ms average, 247 -> 641 mutations/s.
Persisting a batch (~1.2ms) ran inside the speculative-state lock, so execution waited on Postgres. A single persist fiber now commits accepted batches in order, outside the lock. It groups all batches that queued during the previous commit into one transaction. Callers still get a response only after their batch commits, and a batch reaches submit only after it commits. Simulate is correct only for the newest journals, so submit waits under the lock for the in-flight commit before it reads the accepted batches. A failed commit now fails the waiting callers instead of leaving them pending. Counter benchmark, open loop at 4,000/s (2 runs each): 3,180-3,250 -> 3,577-3,580 mutations/s; p50 516-571ms -> 272-285ms. Closed-loop results do not change: those clients send the next mutation only after the commit.
Comment on lines
+799
to
+801
| if (lastBatchCommitted !== undefined) { | ||
| yield* Deferred.await(lastBatchCommitted); | ||
| } |
There was a problem hiding this comment.
The new wait in submit has no focused regression test. Existing tests do not hold a database commit open while another batch executes, so they would not catch a broken wait that makes settlement use the wrong local state.
Add a test that delays batch N's commit, lets N+1 execute, and checks that submit waits for both commits before simulating them. Also check that a failed commit rejects waiting public callers.
Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/typewriter/src/runtime.ts
Line: 799-801
Comment:
**Commit ordering lacks a test**
The new wait in `submit` has no focused regression test. Existing tests do not hold a database commit open while another batch executes, so they would not catch a broken wait that makes settlement use the wrong local state.
Add a test that delays batch N's commit, lets N+1 execute, and checks that `submit` waits for both commits before simulating them. Also check that a failed commit rejects waiting public callers.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
This branch has not been deployed
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
Persisting a batch (~1.2 ms, mostly the WAL fsync) ran inside the speculative-state lock, so execution waited on Postgres.
submitonly after it commits.Correctness note: submit waits for the in-flight commit
simulaterewinds and then replays the journals it is given. This is correct only when those journals are the newest. Suppose batch N+1 has executed but is not yet committed (so it is not inacceptedBatchesQueue). Then rewinding and replaying batch N writes N's post-values over N+1's slots, and the simulation includes N+1's effects. Sosubmit, under the lock, first waits for the most recent batch's commit. Because commits are ordered, that wait covers all earlier batches. The lock stops new batches from executing during the wait.Results
Counter benchmark, open loop at 4,000/s (2 runs each): 3,180–3,250 → 3,577–3,580 mutations/s; p50 516–571 ms → 272–285 ms.
Closed-loop results do not change, as expected: those clients send the next mutation only after the commit.
Test plan
bun run lint,bun run typecheck,bun run test🤖 Generated with Claude Code
The PR appears safe to merge, with a non-blocking request for focused commit-order tests.
Fix with agent prompt
Summary
Moves accepted-batch persistence into a separate worker so the next batch can execute during a database commit.
Diagram
sequenceDiagram participant Client participant Execute participant Persist participant Submit Client->>Execute: Send mutation Execute->>Persist: Queue accepted batch N Execute->>Execute: Execute batch N+1 Persist->>Persist: Commit queued batches in order Persist-->>Client: Return accepted result Submit->>Submit: Hold state lock and wait for newest commit Persist-->>Submit: Publish batches, then signal commit Submit->>Submit: Simulate committed batches Submit->>Submit: Release lock and send transactionReviews (1) · Last reviewed commit: "perf(runtime): commit accepted batches w..." · Reviewed by Greptile