Skip to content

crypto/tls: write the records of one write together - #2

Merged
Nemu-x merged 2 commits into
devfrom
perf/coalesce-records
Oct 18, 2026
Merged

Nemu-x merged 2 commits into
devfrom
perf/coalesce-records

Conversation

@Nemu-x

@Nemu-x Nemu-x commented Oct 18, 2026

Copy link
Copy Markdown
Collaborator

writeRecordLocked sent every record of a write with a write of its own to the connection. A server's download writes 32–64 KiB at a time, i.e. 2–4 records, and the network stack is paid per write: on a loaded server most of the download time went to write(2).

The records of one write are now encrypted in place one behind the other and written together, up to eight full-sized records (about 128 KiB) per write. Nothing is held back: everything is written before the call returns, so latency is unchanged. A write of one record is still one write of that record; record sizes (including dynamic record sizing) are unchanged; handshake buffering (c.buffering) still applies, since the group goes through c.write. If encryption cannot work in place (the REALITY handshake padding in utls can grow a record past the reserve), the record is appended instead.

Tests: TestWriteCoalescesRecords (TLS 1.3, AEAD, CBC, stream ciphers, with and without dynamic record sizing: data intact, at most one write per ~8 records, no write over the limit), TestWriteSmallRecordsUnchanged.

Used by Citrus (getmikan/Citrus perf/download-batching), with 64 KiB download reads. 4-core Docker bench, Citrus server, medians of 3, MB/s (CPU s/GB):

before after
Trojan dl32 2737 (0.68) 3936 (0.49)
VLESS REALITY dl32 2802 (0.67) 4235 (0.46)

A write of more than one record sent every record with its own write to
the connection, and the network stack is paid per write. The records of
one write are now encrypted in place one behind the other and written
together, up to eight full-sized records (about 128 KiB) at a time.
Nothing is held back: all of it is written before the call returns.
@Nemu-x
Nemu-x merged commit 420f58a into dev Oct 18, 2026
4 checks passed
@Nemu-x
Nemu-x deleted the perf/coalesce-records branch October 18, 2026 04:59
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.

1 participant