Repository navigation
perf(runtime): wake the FIFO batch loop when a mutation arrives - #58
Merged
Merged
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.
1 task done
Comment on lines
+1113
to
+1116
| : Effect.forever( | ||
| Queue.takeAll(receivedMutationQueue).pipe( | ||
| Effect.flatMap(acceptBatch), | ||
| ), |
There was a problem hiding this comment.
Both helpers in runtime.test.ts select batch mode, so the existing suite never exercises the new Queue.takeAll branch. The suite can pass even if FIFO acceptance or submission breaks. Add a FIFO test that waits while idle, accepts consecutive mutations in arrival order, and confirms that submit can still run while the queue is empty.
Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/typewriter/src/runtime.ts
Line: 1113-1116
Comment:
**FIFO changes escape tests**
Both helpers in `runtime.test.ts` select batch mode, so the existing suite never exercises the new `Queue.takeAll` branch. The suite can pass even if FIFO acceptance or submission breaks. Add a FIFO test that waits while idle, accepts consecutive mutations in arrival order, and confirms that `submit` can still run while the queue is empty.
---
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 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
In FIFO mode, the runtime polled the received-mutation queue every 4 ms, so each mutation waited for the next tick. That wait was most of the latency for a single client. The loop also woke 250 times per second when idle.
Queue.takeAlland accepts all mutations that queued while the previous batch ran.batchOrder.submit.Most of the diff is reindentation:
acceptBatchnow receives the drained mutations.git diff -wshows +29/−14.Results
Counter benchmark, 1 client: 4.04 ms → 1.56 ms average; 247 → 641 mutations/s.
Test plan
bun run lint,bun run typecheck,bun run test🤖 Generated with Claude Code

Confidence Score: 5/5
The PR appears safe to merge; the remaining FIFO test gap is non-blocking.Fix with agent prompt
Summary
FIFO now waits for mutations with
Queue.takeAllinstead of polling every 4 ms.Diagram
%%{init: {'theme': 'neutral'}}%% flowchart TD Mode{Sequencing mode} Mode -->|FIFO| Wait["Queue.takeAll waits for mutations"] Mode -->|Batch| Window["Wait for fixed batch window"] Window --> Drain["Queue.clear drains mutations"] Wait --> Accept["acceptBatch takes speculative-state lock"] Drain --> Accept Accept --> Persist["Accept and persist mutations"] Persist --> Ready["Queue batch for submit"] Ready --> ModeReviews (2) · Last reviewed commit: "refactor(runtime): inline the accept bat..." · Reviewed by Greptile