Conversation
Contributor
Coverage Report
File Coverage
|
||||||||||||||||||||||||||||||||||||||
…Buffer communication Co-authored-by: Asjas <3828967+Asjas@users.noreply.github.com> Agent-Logs-Url: https://github.com/Asjas/scratchyjs/sessions/e50a6765-1b96-42b5-af30-49c4c1d8b529
Copilot
AI
changed the title
[WIP] Implement lock-free ring buffer for high-throughput SharedArrayBuffer communication
feat(renderer): lock-free ring buffer for high-throughput SharedArrayBuffer communication
Mar 23, 2026
Contributor
|
📚 Docs preview deployed https://scratchyjs-docs-pr-35.asjas.workers.dev Updates automatically on every push to this PR. |
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a new SharedRingBuffer primitive to @scratchyjs/renderer to support high-throughput, zero-copy streaming over SharedArrayBuffer between the main thread and Worker Threads, plus docs and exports updates.
Changes:
- Introduces
SharedRingBufferSPSC ring buffer implementation overSharedArrayBuffer. - Adds a comprehensive Vitest suite for ring buffer behavior and re-export checks.
- Updates worker communication docs and package exports to expose the new API.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/renderer/src/ring-buffer.ts | New SharedRingBuffer implementation (SPSC ring buffer over SAB). |
| packages/renderer/src/ring-buffer.test.ts | New test suite covering correctness, wrap-around, and streaming simulation. |
| packages/renderer/src/index.ts | Re-exports SharedRingBuffer from the package root. |
| packages/renderer/src/index.test.ts | Verifies the new root re-export. |
| packages/renderer/package.json | Adds ./ring-buffer subpath export. |
| docs/worker-communication.md | Replaces incorrect pseudocode with usage examples + API reference. |
| IMPLEMENTATION_PLAN.md | Marks the ring buffer item as completed. |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: A-J Roos <asjasroos@pm.me>
…dRingBuffer Co-authored-by: Asjas <3828967+Asjas@users.noreply.github.com> Agent-Logs-Url: https://github.com/Asjas/scratchyjs/sessions/dd87aedf-cc69-4bce-be67-6f464c2104cc
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.
Implements
SharedRingBuffer— a SPSC lock-free ring buffer overSharedArrayBufferfor streaming large payloads (e.g. chunked SSR HTML) between the main thread and Worker Threads with zero serialization overhead.Implementation —
packages/renderer/src/ring-buffer.tsBuffer layout: 8-byte header (
writePosInt32 +readPosInt32) + N-byte data ring.Design: Monotonically increasing pointers (
pointer % capacityfor actual position).writePosis advanced only by the producer;readPosonly by the consumer — no CAS loops required. Split writes/reads across the ring boundary are handled transparently.API surface:
write(chunk): boolean,read(maxBytes): Uint8Array | null,fromSharedBuffer(sab)static factory,availableToRead,availableToWrite,isEmpty,isFull,capacity,byteLength,getSharedBuffer().Atomics.notifyis called onwritePosafter writes and onreadPosafter reads, keeping the API extensible for future blocking-consumer/producer patterns without breaking changes.Docs fix —
docs/worker-communication.mdThe existing pseudocode example had
Atomics.notify(this.readPos, 0)insidewrite()— wrong target; a waiting reader blocks onwritePos, notreadPos. Replaced the entire sketch with accurate usage examples and an API reference table.Exports
SharedRingBufferre-exported from the package root (@scratchyjs/renderer)"./ring-buffer"named export added topackage.jsonTests —
ring-buffer.test.ts(50 tests)Covers: constructor validation,
fromSharedBuffershared-memory semantics,write/readcontract, ring-wrap (split writes and reads across boundary), fill/drain/refill cycles, introspection getters, partial reads, and a streaming HTML chunks simulation.⚡ Quickly spin up Copilot coding agent tasks from anywhere on your macOS or Windows machine with Raycast.