Skip to content

perf(runtime): wake the FIFO batch loop when a mutation arrives - #58

Merged
kyscott18 merged 2 commits into
mainfrom
perf/fifo-wake-on-offer
Oct 13, 2026
Merged

kyscott18 merged 2 commits into
mainfrom
perf/fifo-wake-on-offer

Conversation

@kyscott18

@kyscott18 kyscott18 commented Oct 13, 2026 •

Copy link
Copy Markdown
Collaborator

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.

  • FIFO now blocks on Queue.takeAll and accepts all mutations that queued while the previous batch ran.
  • Batch mode keeps its fixed window, because it needs one to reorder by batchOrder.
  • Both modes take mutations outside the speculative-state lock, so a wait never blocks submit.

Most of the diff is reindentation: acceptBatch now receives the drained mutations. git diff -w shows +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

RetriggerConfidence Score: 5/5

The PR appears safe to merge; the remaining FIFO test gap is non-blocking.

Fix All in Claude CodeFindings

  1. P2 FIFO changes escape tests ▶
Fix with agent prompt
### Issue 1
packages/typewriter/src/runtime.ts:1109-1112
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.

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!

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

FIFO now waits for mutations with Queue.takeAll instead of polling every 4 ms.

  • FIFO accepts queued mutations when they arrive instead of waiting for a timer tick.

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 --> Mode
Loading

Reviews (2) · Last reviewed commit: "refactor(runtime): inline the accept bat..." · Reviewed by Greptile

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.
Comment on lines +1113 to +1116
: Effect.forever(
Queue.takeAll(receivedMutationQueue).pipe(
Effect.flatMap(acceptBatch),
),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 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.

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!

Fix in Claude Code

@kyscott18
kyscott18 merged commit ffca09c into main Oct 13, 2026
8 checks passed
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