minecraft: cut per-batch decode allocations ~1500x with pooled decompression - #504
Open
HashimTheArab wants to merge 1 commit into
Open
minecraft: cut per-batch decode allocations ~1500x with pooled decompression#504HashimTheArab wants to merge 1 commit into
HashimTheArab wants to merge 1 commit into
Conversation
Decoding a compressed batch allocated the full decompressed payload and a packet collection on every read. Decoder.DecodeFunc now decompresses into a pooled buffer and passes borrowed packet slices to a callback, copying only the packets that outlive the batch. The dialer and listener read loops use it, so a queued packet no longer pins the entire decompressed batch's backing array.
Contributor
Author
|
ive been running this for a couple of days now and its running great, no issues |
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.
Reading a compressed batch used to allocate the full decompressed payload plus a packet collection on every read. The read loops now decompress into a pooled buffer and allocate ~50 bytes per batch instead of ~74 KB, and a single queued packet no longer pins the entire decompressed batch's backing array.
Benchmark (64 packets × 1 KiB in one flate batch, Apple M3 Pro, three runs):
flateCompression.Decompress(old)Decoder.DecodeFunc(new)Changes
Decoder.DecodeFunc, which calls a callback for each packet in a batch with slices borrowed from a pooled decompression buffer. The batch is validated in full before the first callback, so a malformed batch dispatches no packets, preserving the all-or-nothing behavior ofDecode.DecodeFunc. Packets are copied only at the two points where they can outlive the batch (theConn.packetschannel anddeferPacket), viapacketData.ensureOwned.appendDecompressioninterface, so batches decompress into an existing pooled buffer. Buffers above 1 MiB are dropped instead of returned to the pool.Decoder.Decodekeeps its API and behavior. Its returned packets are now backed by one caller-owned allocation, where they previously aliased the decoder's internal read buffer and were only valid until the nextDecodecall.PacketFuncnow receives a copy of the payload, so it can retain it safely.Compression-limit handling is untouched; this is orthogonal to #503.
Validation
go build ./...,go vet ./...,go test -race -count=1 ./...