Repository navigation
perf: use a contiguous UDP receive buffer - #4004
larseggert wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
🟢 Approval recommended
No unresolved review issues were identified.
Pull request overview
Updates UDP receive buffers to use one contiguous allocation with fixed 64 KiB slots, preserving receive behavior while improving locality.
Changes:
- Replaces per-slot allocations with one boxed buffer.
- Updates slicing and iteration for contiguous storage.
File summaries
| File | Description |
|---|---|
neqo-udp/src/lib.rs |
Implements contiguous receive-buffer allocation and iteration. |
Review details
- Files reviewed: 1/1 changed files
- Comments generated: 0
- Review effort level: Lite (auto)
Note
Copilot is running an experiment and ran this review at Lite.
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
There was a problem hiding this comment.
Focused, well-scoped split-out from #3948. The change is straightforward and I don't see a correctness or safety problem:
RecvBuf's tuple field is private, so swappingVec<Vec<u8>>→Box<[u8]>is not a breaking change for downstream consumers (neqo-binonly usesRecvBuf::default()and passes&mut).DatagramIter's fields are private too, so theChunksMuttype change is contained.- Allocation count on
RecvBuf::default()drops fromNUM_BUFS + 1to 1, andvec![0; n]still hits thealloc_zeroedfast path, so there's no memset regression — strictly better than the oldvec![inner.clone(); NUM_BUFS], which didalloc + memcpyper slot. chunks_mutyields exactlyNUM_BUFSfull-length chunks because the length is an exact multiple of the stride, so theexpect("NUM_BUFS elements")and thebuf[0..meta.len]slice inDatagramIter::nextstay in bounds. The 1-byte-larger slot is harmless:meta.lenis still bounded by the iov length quinn-udp was handed.
Two architectural notes that apply to the PR as a whole rather than any one line:
-
The stride and the slot count are now coupled to the allocation length by convention only. With
Vec<Vec<u8>>the shape was self-describing; nowRECV_BUF_SIZEhas taken on a second job (allocation sizing and partitioning stride) that the type system doesn't enforce. An alternative worth considering is keeping the buffer asBox<[[u8; RECV_BUF_SIZE]; NUM_BUFS]>— still one allocation, but the layout is in the type,iter_mut()replaces bothchunks_mutcalls, and theexpectdisappears entirely. The 1 MiB stack temporary during construction is the catch, so it'd needvec![[0; RECV_BUF_SIZE]; NUM_BUFS].into_boxed_slice().try_into()or similar; may not be worth it, but it does remove the invariant. -
The commit message states the mechanism, not the measured effect. "Contiguous slots retain the same page offset" is true, but on every non-Apple target
NUM_BUFS == 1so there is only one slot and the rounding is a no-op. Worth recording the Apple benchmark delta in the description (and ideally aneqo-binbench reference), since that's the only configuration the change can affect — see the inline comment for the cache-set-aliasing counter-argument that makes a measurement rather than reasoning the right tiebreaker here.
Remaining inline comments are DRY and test-coverage nits, none blocking.
| /// Receive buffer size, per slot in [`RecvBuf`]. | ||
| /// | ||
| /// Fits a maximum size UDP datagram, or, on platforms with segmentation | ||
| /// offloading, multiple smaller datagrams. | ||
| const RECV_BUF_SIZE: usize = u16::MAX as usize; | ||
| /// | ||
| /// Rounded up so contiguous slots keep the same page offset. | ||
| const RECV_BUF_SIZE: usize = u16::MAX as usize + 1; |
There was a problem hiding this comment.
Tip
The "same page offset" rationale only bites when NUM_BUFS > 1, i.e. only on Apple (NUM_BUFS = 16); everywhere else NUM_BUFS == 1 and the + 1 just adds a byte. Also worth sanity-checking the opposite effect: an exact 64 KiB (power-of-two) stride makes every slot alias to the same L1/L2 cache sets, which is the classic recipe for conflict misses when the kernel fills several slots in one recvmmsg/recvmsg_x. Conventional tuning adds padding to break such alignment rather than enforce it.
Since this is a perf-only change, could you include the Apple benchmark numbers (the ones that motivated NUM_BUFS = 16) in the PR description? A one-line note here on what the win actually was would also keep the constant from drifting back on the next reader's hunch.
Minor doc nit while you're here — spelling out the resulting size makes the intent self-evident:
| /// Receive buffer size, per slot in [`RecvBuf`]. | |
| /// | |
| /// Fits a maximum size UDP datagram, or, on platforms with segmentation | |
| /// offloading, multiple smaller datagrams. | |
| const RECV_BUF_SIZE: usize = u16::MAX as usize; | |
| /// | |
| /// Rounded up so contiguous slots keep the same page offset. | |
| const RECV_BUF_SIZE: usize = u16::MAX as usize + 1; | |
| /// Receive buffer size, per slot in [`RecvBuf`]. | |
| /// | |
| /// Fits a maximum size UDP datagram, or, on platforms with segmentation | |
| /// offloading, multiple smaller datagrams. | |
| /// | |
| /// Rounded up to 64 KiB (a multiple of the page size) so that contiguous slots | |
| /// keep the same page offset. | |
| const RECV_BUF_SIZE: usize = u16::MAX as usize + 1; |
| /// A UDP receive buffer backed by one contiguous allocation. | ||
| pub struct RecvBuf(Box<[u8]>); | ||
|
|
||
| impl Default for RecvBuf { | ||
| fn default() -> Self { | ||
| Self(vec![vec![0; RECV_BUF_SIZE]; NUM_BUFS]) | ||
| Self(vec![0; RECV_BUF_SIZE * NUM_BUFS].into_boxed_slice()) | ||
| } | ||
| } |
There was a problem hiding this comment.
Note
The RECV_BUF_SIZE * NUM_BUFS layout invariant is now implicit and re-derived at two call sites via chunks_mut(RECV_BUF_SIZE) (L156 and L182). If the stride and the allocation ever diverge, the failure mode is the expect("NUM_BUFS elements") panic rather than a compile error. Pulling the split into one inherent method keeps it single-sourced:
| /// A UDP receive buffer backed by one contiguous allocation. | |
| pub struct RecvBuf(Box<[u8]>); | |
| impl Default for RecvBuf { | |
| fn default() -> Self { | |
| Self(vec![vec![0; RECV_BUF_SIZE]; NUM_BUFS]) | |
| Self(vec![0; RECV_BUF_SIZE * NUM_BUFS].into_boxed_slice()) | |
| } | |
| } | |
| /// A UDP receive buffer backed by one contiguous allocation. | |
| pub struct RecvBuf(Box<[u8]>); | |
| impl RecvBuf { | |
| /// The [`NUM_BUFS`] receive slots, each [`RECV_BUF_SIZE`] bytes long. | |
| fn slots(&mut self) -> ChunksMut<'_, u8> { | |
| self.0.chunks_mut(RECV_BUF_SIZE) | |
| } | |
| } | |
| impl Default for RecvBuf { | |
| fn default() -> Self { | |
| Self(vec![0; RECV_BUF_SIZE * NUM_BUFS].into_boxed_slice()) | |
| } | |
| } |
Both call sites then become recv_buf.slots().
| remaining_buffers: metas | ||
| .into_iter() | ||
| .zip(recv_buf.0.chunks_mut(RECV_BUF_SIZE)) | ||
| .take(n), |
There was a problem hiding this comment.
Note
Nothing currently pins the partitioning invariant, and many_datagrams_through_gso_gro only exercises it on Linux/Windows. A platform-independent unit test is cheap and would catch a stride/allocation mismatch at test time rather than via the expect panic in recv_inner:
| remaining_buffers: metas | |
| .into_iter() | |
| .zip(recv_buf.0.chunks_mut(RECV_BUF_SIZE)) | |
| .take(n), | |
| .zip(recv_buf.0.chunks_mut(RECV_BUF_SIZE)) | |
| .take(n), | |
| local_address, | |
| }) | |
| } | |
| #[cfg(test)] | |
| mod layout_tests { | |
| use super::{NUM_BUFS, RECV_BUF_SIZE, RecvBuf}; | |
| #[test] | |
| fn default_splits_into_num_bufs_full_slots() { | |
| let mut buf = RecvBuf::default(); | |
| let slots: Vec<_> = buf.0.chunks_mut(RECV_BUF_SIZE).collect(); | |
| assert_eq!(slots.len(), NUM_BUFS); | |
| assert!(slots.iter().all(|s| s.len() == RECV_BUF_SIZE)); | |
| } | |
| } |
(Or just fold the assertions into the existing tests module — it already has use super::*.)
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4004 +/- ##
==========================================
- Coverage 96.98% 96.82% -0.16%
==========================================
Files 113 118 +5
Lines 39643 39992 +349
Branches 39643 39992 +349
==========================================
+ Hits 38446 38722 +276
- Misses 1181 1246 +65
- Partials 16 24 +8
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
Performance profiles for profiler.firefox.comBenchmarks (14)
|
|
Not worth it. |
Failed Interop TestsQUIC Interop Runner, client vs. server, differences relative to
All resultsSucceeded Interop TestsQUIC Interop Runner, client vs. server neqo-pr as client
neqo-pr as server
Unsupported Interop TestsQUIC Interop Runner, client vs. server neqo-pr as client
neqo-pr as server
|
Benchmark resultsNo significant performance differences relative to cac2b74. All resultstransfer/1-conn/1-100mb-resp (aka. Download)/mtu-1504: Change within noise threshold. time: [134.62 ms 134.79 ms 134.97 ms]
thrpt: [740.88 MiB/s 741.88 MiB/s 742.83 MiB/s]
change:
time: [-0.4367% -0.2367% -0.0429%] (p = 0.02 < 0.05)
thrpt: [+0.0429% +0.2372% +0.4386%]
Change within noise threshold.
Found 2 outliers among 100 measurements (2.00%)
2 (2.00%) high mildtransfer/1-conn/10_000-parallel-1b-resp (aka. RPS)/mtu-1504: No change in performance detected. time: [213.93 ms 214.22 ms 214.53 ms]
thrpt: [46.614 Kelem/s 46.681 Kelem/s 46.745 Kelem/s]
change:
time: [-0.2933% -0.0951% +0.0888%] (p = 0.36 > 0.05)
thrpt: [-0.0887% +0.0952% +0.2942%]
No change in performance detected.
Found 1 outliers among 100 measurements (1.00%)
1 (1.00%) high severetransfer/1-conn/1-1b-resp (aka. HPS)/mtu-1504: No change in performance detected. time: [7.3710 ms 7.3752 ms 7.3798 ms]
thrpt: [135.50 B/s 135.59 B/s 135.67 B/s]
change:
time: [-0.0844% -0.0007% +0.0757%] (p = 0.99 > 0.05)
thrpt: [-0.0756% +0.0007% +0.0845%]
No change in performance detected.
Found 5 outliers among 100 measurements (5.00%)
4 (4.00%) high mild
1 (1.00%) high severetransfer/1-conn/1-100mb-req (aka. Upload)/mtu-1504: Change within noise threshold. time: [138.24 ms 138.39 ms 138.60 ms]
thrpt: [721.49 MiB/s 722.57 MiB/s 723.38 MiB/s]
change:
time: [+0.5597% +0.7280% +0.9210%] (p = 0.00 < 0.05)
thrpt: [-0.9126% -0.7228% -0.5566%]
Change within noise threshold.
Found 3 outliers among 100 measurements (3.00%)
2 (2.00%) low mild
1 (1.00%) high severestreams/walltime/1-streams/each-1000-bytes: Change within noise threshold. time: [543.43 µs 545.24 µs 547.38 µs]
thrpt: [1.7423 MiB/s 1.7491 MiB/s 1.7549 MiB/s]
change:
time: [-1.3383% -0.7947% -0.2193%] (p = 0.00 < 0.05)
thrpt: [+0.2198% +0.8010% +1.3565%]
Change within noise threshold.
Found 11 outliers among 100 measurements (11.00%)
2 (2.00%) high mild
9 (9.00%) high severestreams/walltime/1000-streams/each-1-bytes: No change in performance detected. time: [10.289 ms 10.303 ms 10.317 ms]
thrpt: [94.656 KiB/s 94.786 KiB/s 94.913 KiB/s]
change:
time: [-0.3303% -0.0388% +0.2161%] (p = 0.79 > 0.05)
thrpt: [-0.2156% +0.0389% +0.3314%]
No change in performance detected.streams/walltime/1000-streams/each-1000-bytes: Change within noise threshold. time: [30.650 ms 30.686 ms 30.721 ms]
thrpt: [31.043 MiB/s 31.079 MiB/s 31.114 MiB/s]
change:
time: [-0.5355% -0.3666% -0.1995%] (p = 0.00 < 0.05)
thrpt: [+0.1999% +0.3680% +0.5384%]
Change within noise threshold.streams-flow-controlled/walltime/1-streams/each-4194304-bytes: Change within noise threshold. time: [24.832 ms 24.862 ms 24.894 ms]
thrpt: [160.68 MiB/s 160.89 MiB/s 161.08 MiB/s]
change:
time: [-0.4372% -0.2672% -0.0965%] (p = 0.00 < 0.05)
thrpt: [+0.0966% +0.2680% +0.4392%]
Change within noise threshold.
Found 2 outliers among 100 measurements (2.00%)
2 (2.00%) high mildstreams-flow-controlled/walltime/10-streams/each-1048576-bytes: No change in performance detected. time: [68.040 ms 68.136 ms 68.234 ms]
thrpt: [146.55 MiB/s 146.76 MiB/s 146.97 MiB/s]
change:
time: [-0.1207% +0.0464% +0.2383%] (p = 0.60 > 0.05)
thrpt: [-0.2377% -0.0464% +0.1208%]
No change in performance detected.transfer/walltime/pacing-false/varying-seeds: Change within noise threshold. time: [17.881 ms 17.904 ms 17.939 ms]
thrpt: [222.98 MiB/s 223.42 MiB/s 223.70 MiB/s]
change:
time: [+0.6756% +0.8379% +1.0643%] (p = 0.00 < 0.05)
thrpt: [-1.0531% -0.8309% -0.6711%]
Change within noise threshold.
Found 3 outliers among 100 measurements (3.00%)
2 (2.00%) high mild
1 (1.00%) high severetransfer/walltime/pacing-true/varying-seeds: Change within noise threshold. time: [18.131 ms 18.142 ms 18.152 ms]
thrpt: [220.36 MiB/s 220.49 MiB/s 220.61 MiB/s]
change:
time: [+0.5845% +0.7310% +0.8450%] (p = 0.00 < 0.05)
thrpt: [-0.8379% -0.7257% -0.5811%]
Change within noise threshold.
Found 5 outliers among 100 measurements (5.00%)
3 (3.00%) low mild
2 (2.00%) high mildtransfer/walltime/pacing-false/same-seed: Change within noise threshold. time: [17.903 ms 17.916 ms 17.929 ms]
thrpt: [223.10 MiB/s 223.26 MiB/s 223.43 MiB/s]
change:
time: [+1.7698% +1.8717% +1.9683%] (p = 0.00 < 0.05)
thrpt: [-1.9304% -1.8374% -1.7390%]
Change within noise threshold.
Found 4 outliers among 100 measurements (4.00%)
2 (2.00%) low mild
2 (2.00%) high mildtransfer/walltime/pacing-true/same-seed: No change in performance detected. time: [18.089 ms 18.099 ms 18.108 ms]
thrpt: [220.90 MiB/s 221.01 MiB/s 221.12 MiB/s]
change:
time: [-0.1575% +0.0409% +0.1822%] (p = 0.70 > 0.05)
thrpt: [-0.1819% -0.0409% +0.1577%]
No change in performance detected.Instructions per cycleCriterion reported no significant timing changes. All benchmarks
Download data for |
Client/server transfer resultsPerformance differences relative to cac2b74. Transfer of 33554432 bytes over loopback, min. 100 runs. All unit-less numbers are in milliseconds.
Table above only shows statistically significant changes. See all results below. All resultsTransfer of 33554432 bytes over loopback, min. 100 runs. All unit-less numbers are in milliseconds.
Download data for |
Splits the receive-buffer allocation change out of #3948 so that PR remains focused on the UDP send path.
This stores receive slots in one boxed allocation and uses a 65,536-byte stride so contiguous slots retain the same page offset.