Repository navigation
Conversation
Consume each frame before deserializing it so that an invalid frame neither wedges nor desynchronizes the channel, and deliver the messages decoded from the same read before throwing its error. Accumulate large frames that span reads into a BackingStore that host objects borrow, and small ones into a bounded reused buffer. Copy in bulk and avoid 32-bit overflow in the length check. Signed-off-by: Daniel Lemire <daniel@lemire.me>
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.
This is a follow-up to nodejs#64072. It targets that PR's branch, so it can be folded into it.
Error handling
In nodejs#64072, the framer only advances its state after a frame deserializes successfully. A frame that fails to deserialize therefore:
read()throws again and the channel is stuck.Now each frame is consumed before it is deserialized. The first error is held in the framer, and the remaining frames in the read are still decoded.
parseChannelMessages()delivers the decoded messages and then throws the held error. A small generator handles this, and it is only created on the error path. Termination exceptions are rethrown immediately. The oldbuffering()method becomesstatus()(a bitmask) plustakeError(), so the normal path still makes only one extra binding call.Memory and copies for frames that span reads
BackingStore, sized from its length prefix, instead of astd::vector<char>. When the frame is complete, the store becomes anArrayBuffer. ArrayBufferView host objects borrow from it, the same as for frames within one read, instead of being copied a second time. The memory is released by the GC with those views, instead of staying in the vector's capacity for the life of the channel.memcpyinstead of onepush_backper byte.ERR_MEMORY_ALLOCATION_FAILEDand discards the frame's bytes, so the stream stays in sync.Other changes
available - 4 >= size, so it cannot overflow whensize_tis 32 bits.MemoryInforeports the buffered payload size instead of visiting every byte.Tests
test/parallel/test-child-process-advanced-serialization-spanning-frames.js: messages from 1 byte to 1 MiB carrying Buffer, Float64Array and Uint16Array data, with their contents checked.parallel/test-child-process-*,parallel/test-cluster-*,sequential/test-child-process-*andsequential/test-cluster-*pass (Linux x86-64).Benchmarks
Linux x86-64 (128 cores).
benchmark/child_process/child-process-ipc-roundtrip(msgs/s), comparing nodejs#64072 with this PR on top of it, 10 runs each:Round trips of
{ b: Buffer.alloc(len), f: new Float64Array(len / 16) }withserialization: 'advanced'(msgs/s, mean of 3 runs). This compares the currentmain(the JS framer), nodejs#64072, and nodejs#64072 with this PR on top:On these large messages, nodejs#64072 alone is 3–5× slower than
mainbecause it copies one byte at a time and copies large frames twice. This PR restoresmain's throughput or improves on it.To keep small frames that span reads cheap (one
BackingStoreandArrayBufferper frame cost about 8% at 16 KiB), payloads up to 64 KiB are accumulated into a buffer the framer reuses. Its size is bounded, so it never retains more than 64 KiB.