Repository navigation
Conversation
dispatchBufferSize was exactly 4 MiB, the maximum typical NBD data payload. Every request also carries a 28-byte header, so a max-size (4 MiB) write did not fit the buffer whole: the first Read filled 4 MiB (header + 4 MiB-28 of payload) and a second Read was needed for the trailing 28 payload bytes. The code carried a TODO acknowledging exactly this. Widen dispatchBufferSize by one header (4*1024*1024 + nbdRequestHeaderSize) so [28-byte header + 4 MiB payload] lands in a single Read. Introduce the named nbdRequestHeaderSize constant for the header width and use it in place of the bare 28 literals in the dispatch loop, and drop the resolved TODO and the now- inaccurate deadlock comment in the write path. Measured on this repo with a counting socket feeding one 4 MiB write (header + full payload buffered), reads that returned payload bytes: before (buffer 4194304): 2 Reads after (buffer 4194332): 1 Read One syscall saved per max-size write; for I/O-heavy sandboxes doing large sequential writes this is thousands of requests. Memory cost is 28 bytes per sandbox connection buffer. Rebased onto current main at the maintainers invitation on e2b-dev#3373 (the Sept NBD dispatch refactor left the buffer sizing untouched). Includes the requested before/after measurement as a pinned test (TestDispatchMaxSizeWriteSingleRead). Closes e2b-dev#3355.
AdaAibaby
requested review from
ValentaTomas,
dobrac and
jakubno
as code owners
October 22, 2026 07:22
Contributor
|
This still does not say how frequent the two system call scenarios would be and what the cost of two system calls is and how it impacts the overall performance. Is the a numbers that backs this? Most of the time, we work with larger values than 4MB. This is only a problem if we deal with very small blocks which isn't a general case. |
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.
Rebased follow-up to #3373, which @ryan-sana closed with: "please rebase onto current main and include a before/after measurement." This is that rebase, with the measurement.
Problem (still present on current main)
dispatchBufferSizeis exactly 4 MiB — the maximum typical NBD data payload. But every request also carries a 28-byte header, so a max-size (4 MiB) write does not fit the buffer whole:The September NBD dispatch refactor (
ad11877aa,0c21aa227) reshaped how commands are passed and added async.Pool, but left the buffer sizing untouched — theTODOacknowledging this is still verbatim onmain:Fix
nbdRequestHeaderSize = 28and use it for the bare28literals in the dispatch loop (header completeness check, header slice, the threerp += 28).dispatchBufferSizeto4*1024*1024 + nbdRequestHeaderSizeso[28B header + 4 MiB payload]is taken in a singleRead.Before/after measurement (as requested)
Measured in-repo with a counting socket that feeds one 4 MiB write (header + full payload already buffered, as a busy kernel socket delivers it), counting the
Readcalls that returned payload bytes:Readcalls for one 4 MiB writemain)One syscall saved per max-size write. For I/O-heavy sandboxes doing large sequential block writes this is thousands of requests. Memory cost: 28 bytes per sandbox connection buffer.
Test
TestDispatchMaxSizeWriteSingleReadpins the behaviour: a 4 MiB write (header + full payload as one chunk) is consumed in exactly oneRead, and the payload reaches the backend with the correct offset and full data integrity. ExistingTestDispatchWriteZeroesReadLoopStallstill passes;go buildandgo vetare clean.Closes #3355. Supersedes #3373.