Skip to content

packet writing optimizations - #63

Open
HashimTheArab wants to merge 3 commits into
lunarfrom
packet-writing-optimizations-67
Open

packet writing optimizations#63
HashimTheArab wants to merge 3 commits into
lunarfrom
packet-writing-optimizations-67

Conversation

@HashimTheArab

@HashimTheArab HashimTheArab commented May 27, 2026

Copy link
Copy Markdown
Owner

Note

Medium Risk
Changes core send/flush/encode paths and buffer ownership; behavior should match prior logic but regressions could affect ordering, memory, or wire format under edge cases.

Overview
This PR speeds up outbound packet encoding on Conn and in the batch encoder by cutting allocations and improving buffer lifecycle management.

Conn now queues sends with a packetQueue that tracks pooled bytes.Buffer instances alongside packet slices, releases them after flush/encode, and skips returning oversized buffers to the pool (1 MiB cap). Encoding no longer copies every packet to a new []byte; marshaling can reuse a resettable protocol writer, and WritePacketDirect uses a dedicated queue plus directMu instead of a stack buffer. Flush swaps full/spare queues and calls release() on the batch being encoded.

Compression/encoding: Flate can stream into an existing buffer via compressTo, which Encoder prefers (like Snappy’s append path). Conn benchmarks and encoder/compression benchmarks plus a compressTo test document the behavior.

Reviewed by Cursor Bugbot for commit 3d9fc70. Bugbot is set up for automated code reviews on this repo. Configure here.

Related PR

Closes Sandertv#341.

  Changed:

  - minecraft/conn.go:532: encodePacketsTo now queues owned pooled buffers instead of copying each encoded packet.
  - minecraft/conn.go:516: WritePacket now uses the optimized pooled queue path.
  - minecraft/conn.go:641: WritePacketDirect uses a reusable per-conn queue and is now 0 alloc in the benchmark.
  - minecraft/protocol/packet/encoder.go:132: encoder can use a writer-based compression fast path.
  - minecraft/protocol/packet/compression.go:109: flate now compresses directly into the encoder’s pooled output buffer.
  - Added focused tests/benchmarks in conn_bench_test.go, encoder_bench_test.go, and compression_test.go.

  Measured locally:

  - BenchmarkConnWritePacketDirect: now ~80-90 ns/op, 0 B/op, 0 allocs/op (baseline was ~160-169 ns/op, 720 B/op, 4 allocs/op).
  - BenchmarkConnWritePacketDirectBatch: now ~798-883 ns/op, 0 B/op, 0 allocs/op (baseline was ~1890-1965 ns/op, 10664 B/op, 51 allocs/op).
  - BenchmarkEncoderEncodeFlate: now 0 allocs/op (baseline was 1 alloc/op).

  Validation:

  - go test ./minecraft/protocol/packet passed.
  - go test ./minecraft -run 'Test|^$' passed.
  - go test -run '^$' ./... passed compile/no-test run.
  - go test excluding minecraft/room and minecraft/service/signaling passed; full go test ./... hung in those existing network-oriented packages and was stopped.

  I left Reader.ByteSlice unchanged because zero-copy there can alias Decoder.buf on uncompressed reads, which would change slice lifetime semantics.
@coderabbitai

coderabbitai Bot commented May 27, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 88b91155-143e-42e7-8303-974d767aa017

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch packet-writing-optimizations-67

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@cursor

cursor Bot commented Aug 12, 2026

Copy link
Copy Markdown

Bugbot is not enabled for your account, so this pull request was not reviewed.

Enable Bugbot in the Cursor dashboard to get automatic reviews on future PRs.

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