Skip to content

net: disable Nagle's algorithm on client connections - #2996

Merged
sylvestre merged 1 commit into
mozilla:mainfrom
sylvestre:pr/tcp-nodelay
Nov 21, 2026
Merged

sylvestre merged 1 commit into
mozilla:mainfrom
sylvestre:pr/tcp-nodelay

Conversation

@sylvestre

Copy link
Copy Markdown
Collaborator

A message between a client and the daemon is written as a length prefix and then its body. When the body spans several TCP segments, Nagle's algorithm holds its last one until the daemon ACKs the previous ones, which Linux delays by up to 40ms when the daemon has no reply to send yet.

Server mode barely notices, but in client-side mode every cache write carries a whole entry: on a cold FFmpeg build at -j28, a cache write took 33ms on average (median 43ms) instead of 0.2ms in server mode, 69 seconds of the compiles' time in total.

Set TCP_NODELAY on both ends of the connection. The cold FFmpeg build goes from 47.6s to 45.3s in client-side mode, the same as server mode (44.9s), median of 3.

@codecov-commenter

codecov-commenter commented Nov 7, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.96970% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 76.52%. Comparing base (6f6e390) to head (a351a6a).

Files with missing lines Patch % Lines
src/net.rs 91.66% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2996      +/-   ##
==========================================
- Coverage   79.00%   76.52%   -2.48%     
==========================================
  Files          75       75              
  Lines       46249    46211      -38     
==========================================
- Hits        36538    35364    -1174     
- Misses       9711    10847    +1136     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ahochheiden ahochheiden left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

With Nagle off, write_length_prefixed_bincode sends the length and body as two packets for anything over the 8 KiB BufWriter:

sccache/src/util.rs

Lines 676 to 682 in 9091e1b

{
let bytes = bincode::serialize(&data)?;
let mut len = [0; 4];
BigEndian::write_u32(&mut len, bytes.len() as u32);
writer.write_all(&len)?;
writer.write_all(&bytes)?;
writer.flush()?;

One write instead:

    let mut bytes = vec![0; 4];
    bincode::serialize_into(&mut bytes, &data)?;
    BigEndian::write_u32(&mut bytes[..4], (bytes.len() - 4) as u32);
    writer.write_all(&bytes)?;
    writer.flush()?;

A message is written as a length prefix and then its body. When the body
spans several segments, Nagle's algorithm holds its last one until the
daemon ACKs the previous ones, which Linux delays by up to 40ms when the
daemon has no reply to send yet. Server mode barely noticed, but in
client-side mode every cache write carries a whole entry: a write took
33ms on average instead of 0.2ms, and a cold FFmpeg build at -j28 spent
69 seconds of its compiles waiting on them.

Set TCP_NODELAY on both ends of the connection, and only log a failure
to set it: in the accept loop, an error would stop the daemon. The cold
build goes from 47.6s to 45.3s in client-side mode, the same as server
mode (44.9s).
@sylvestre
sylvestre marked this pull request as ready for review November 20, 2026 13:15
@sylvestre
sylvestre merged commit f847172 into mozilla:main Nov 21, 2026
51 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.

3 participants