Conversation
Broadcasting the same message with compression enabled compressed it once per connection, which dominates CPU cost when fanning out to many peers. PreparedMessage compresses the payload once and shares it across connections that compress without context takeover while other connections use the regular write path.
kvizyx
force-pushed
the
kvizyx/prepared-message
branch
from
October 2, 2026 21:17
9d1be01 to
385007a
Compare
This branch has not been deployed
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.
Closes #546
Adds
PreparedMessageandConn.WritePreparedfor writing the same message to many connections, such as when broadcasting. On connections that compress without context takeover, the message is compressed once and the result is shared instead of being compressed per connection.Other connections use the regular write path: with context takeover each connection compresses against its own sliding window, so the result cannot be shared, and without compression there is nothing expensive to share. Only the compressed payload is cached so it works for both server and client connections, and clients still mask every write with a new key. On Wasm,
WritePreparedis equivalent toWrite.Without context takeover,
WriteandWritePreparednow also send a message uncompressed if compression does not make it smaller, such as already compressed data, as RFC 7692 allows. With context takeover the message stays compressed to keep the peer's sliding window in sync (picked up this approach from uWebSockets).Also adds a broadcast example in
internal/examples/broadcast.Benchmarks
BenchmarkBroadcastwrites a 1 KiB message to 16 connections per op:The extra allocations happen once per broadcast, not per connection: 2 for
NewPreparedMessage(the struct and its copy of the message) + 4 for compressing the message once without context takeover.AI: Part of this changes were written with AI. I reviewed all of the code :)