[PR #739] [MERGED] LAN: write chunk packets straight into one buffer #2772

Closed
opened 2026-11-02 15:58:55 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon/pull/739
Author: @ludovic111
Created: 10/17/2026
Status: ✅ Merged
Merged: 10/17/2026
Merged by: @ludovic111

Base: main ← Head: perf/chunk-packet-direct


📝 Commits (1)

  • 43f598f LAN: write chunk packets straight into one buffer

📊 Changes

2 files changed (+291 additions, -91 deletions)

View changed files

📝 crates/horizon-client/src/lan/chunks.rs (+284 -82)
📝 crates/horizon-client/src/lan/hub.rs (+7 -9)

📄 Description

🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself.

What: every chunk sent to a player was built as a LevelChunkWithLight first. That meant growing its section buffer by repeated reallocation, cloning each 2048-byte light layer into the packet (filling fresh layers for uniformly lit sections) and cloning the column's heightmaps. Then Clientbound::encode copied all of it again into the body. ChunkEncoder::body_in now writes the body that encode would give straight into one buffer of its final size:

  • the cached section bytes and light layers are copied once;
  • uniform layers are filled in place;
  • the heightmaps are shared (Arc) instead of cloned;
  • the block entity tags are written first, so the size is known before the light.

The hub sends that body for new chunks and for resend_chunk. encode_in had no callers left and is removed; encode (no neighbours) stays for the examples and tests. The bytes on the wire are the same. The new test the_body_is_the_packet_encoded checks body against Clientbound::LevelChunkWithLight(..).encode() for a column with every kind of light section (uniform dark, uniform lit, varying sky and block light, none), block entities including a sign's update tag, five neighbour masks, and a level without sky light.

Measured (dedicated server, 50 walking bots, seed 12345, view distance 10, Mac mini M4 Pro). Encoding and queueing each batch of chunks on the server thread, from the probe; base and this PR ran at the same time:

base r1 this PR r1 base r2 this PR r2
per chunk, median 15.9 µs 11.6 µs 14.9 µs 11.4 µs
per chunk, 25th percentile 12.2 µs 8.9 µs 12.0 µs 9.2 µs
per tick, mean 804 µs 559 µs 1755 µs 1116 µs
chunks in the window 22476 22367 22136 22207

That is 23 to 27 % less server-thread time per chunk sent.

Inside the old path (sample, base), encode_in was 6.5 to 10.2 % of the server thread, plus part of Clientbound::encode. Within encode_in, reallocation (finish_grow) took 21.5 % and memmove 28 %. In body_in, reallocation is gone and the remaining copy is the section bytes.

MSPT, base vs this PR at the same time: 8.72 / 9.46 ms, 13.96 / 15.90 ms, 5.96 / 5.43 ms, 14.07 / 11.53 ms: within this machine's noise (see below).

How it was measured

  • Mac mini (M4 Pro, 14 threads, 64 GB), shared with other agents' builds and parity runs: ~/fleet/load showed the CPU 72 to 92 % busy with a load average of 56 to 170 through the runs.
  • tools/server_bench.py --servers horizon --behaviour walk --bots 50 --warmup 30 --seconds 45 --gamerule locator_bar=false: seed 12345, view and simulation distance 10, bots on the same machine, fast profile, base = main 7bbbf6e9. Every run sent the bots the same chunks (490 to 505 a second, none sent twice).
  • Paired runs: the base and the new build ran at the same time (two servers, 50 bots each), so both saw the same load; they swapped worktrees between rounds.
  • Probe: a local patch, not part of this PR, timed the step with Instant on the server thread and logged one line per event. The numbers cover the 900 ticks of the measured window. The steps are much shorter than a scheduler time slice, so their medians are close to CPU time even on a loaded machine.
  • Share of the server thread: macOS sample of the engine for 20 s inside the measured window of a separate run per build. It counts wall time, so time spent waiting for a core counts too.
  • MSPT (/tick query average) didn't separate the builds on this machine. The server thread used only about 3 ms of CPU a tick (ps -M); with load averages up to 170 on 14 threads, most of a tick's wall time went to waiting for a core, and that spread is larger than these changes.

Checks

  • cargo fmt --all --check: ok
  • cargo clippy --workspace --all-targets -- -D warnings: ok
  • cargo test -p horizon-client --lib: 824 passed, 0 failed, 11 ignored
  • Local parity (tools/parity.py --changed origin/main, one run on #737, #738, #739 and #748 stacked together, plus the chunk-occupancy commit on perf/chunk-occupied-columns): 87 passed, 0 failed (every harness that depends on the 9 changed files).

Nothing here needs Ludovic.

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/horizon-rust/horizon/pull/739 **Author:** [@ludovic111](https://github.com/ludovic111) **Created:** 10/17/2026 **Status:** ✅ Merged **Merged:** 10/17/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `perf/chunk-packet-direct` --- ### 📝 Commits (1) - [`43f598f`](https://github.com/horizon-rust/horizon/commit/43f598fd4e60afe7cca319c0e193cbb9d1910ec8) LAN: write chunk packets straight into one buffer ### 📊 Changes **2 files changed** (+291 additions, -91 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/lan/chunks.rs` (+284 -82) 📝 `crates/horizon-client/src/lan/hub.rs` (+7 -9) </details> ### 📄 Description > 🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself. **What:** every chunk sent to a player was built as a `LevelChunkWithLight` first. That meant growing its section buffer by repeated reallocation, cloning each 2048-byte light layer into the packet (filling fresh layers for uniformly lit sections) and cloning the column's heightmaps. Then `Clientbound::encode` copied all of it again into the body. `ChunkEncoder::body_in` now writes the body that `encode` would give straight into one buffer of its final size: - the cached section bytes and light layers are copied once; - uniform layers are filled in place; - the heightmaps are shared (`Arc`) instead of cloned; - the block entity tags are written first, so the size is known before the light. The hub sends that body for new chunks and for `resend_chunk`. `encode_in` had no callers left and is removed; `encode` (no neighbours) stays for the examples and tests. The bytes on the wire are the same. The new test `the_body_is_the_packet_encoded` checks `body` against `Clientbound::LevelChunkWithLight(..).encode()` for a column with every kind of light section (uniform dark, uniform lit, varying sky and block light, none), block entities including a sign's update tag, five neighbour masks, and a level without sky light. **Measured** (dedicated server, 50 walking bots, seed 12345, view distance 10, Mac mini M4 Pro). Encoding and queueing each batch of chunks on the server thread, from the probe; base and this PR ran at the same time: | | base r1 | this PR r1 | base r2 | this PR r2 | |---|---:|---:|---:|---:| | per chunk, median | 15.9 µs | 11.6 µs | 14.9 µs | 11.4 µs | | per chunk, 25th percentile | 12.2 µs | 8.9 µs | 12.0 µs | 9.2 µs | | per tick, mean | 804 µs | 559 µs | 1755 µs | 1116 µs | | chunks in the window | 22476 | 22367 | 22136 | 22207 | That is 23 to 27 % less server-thread time per chunk sent. Inside the old path (`sample`, base), `encode_in` was 6.5 to 10.2 % of the server thread, plus part of `Clientbound::encode`. Within `encode_in`, reallocation (`finish_grow`) took 21.5 % and memmove 28 %. In `body_in`, reallocation is gone and the remaining copy is the section bytes. MSPT, base vs this PR at the same time: 8.72 / 9.46 ms, 13.96 / 15.90 ms, 5.96 / 5.43 ms, 14.07 / 11.53 ms: within this machine's noise (see below). **How it was measured** - Mac mini (M4 Pro, 14 threads, 64 GB), shared with other agents' builds and parity runs: `~/fleet/load` showed the CPU 72 to 92 % busy with a load average of 56 to 170 through the runs. - `tools/server_bench.py --servers horizon --behaviour walk --bots 50 --warmup 30 --seconds 45 --gamerule locator_bar=false`: seed 12345, view and simulation distance 10, bots on the same machine, `fast` profile, base = main 7bbbf6e9. Every run sent the bots the same chunks (490 to 505 a second, none sent twice). - **Paired runs:** the base and the new build ran at the same time (two servers, 50 bots each), so both saw the same load; they swapped worktrees between rounds. - **Probe:** a local patch, not part of this PR, timed the step with `Instant` on the server thread and logged one line per event. The numbers cover the 900 ticks of the measured window. The steps are much shorter than a scheduler time slice, so their medians are close to CPU time even on a loaded machine. - **Share of the server thread:** macOS `sample` of the engine for 20 s inside the measured window of a separate run per build. It counts wall time, so time spent waiting for a core counts too. - MSPT (`/tick query` average) didn't separate the builds on this machine. The server thread used only about 3 ms of CPU a tick (`ps -M`); with load averages up to 170 on 14 threads, most of a tick's wall time went to waiting for a core, and that spread is larger than these changes. **Checks** - `cargo fmt --all --check`: ok - `cargo clippy --workspace --all-targets -- -D warnings`: ok - `cargo test -p horizon-client --lib`: 824 passed, 0 failed, 11 ignored - Local parity (`tools/parity.py --changed origin/main`, one run on #737, #738, #739 and #748 stacked together, plus the chunk-occupancy commit on `perf/chunk-occupied-columns`): 87 passed, 0 failed (every harness that depends on the 9 changed files). Nothing here needs Ludovic. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 15:58:55 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
horizon-rust/horizon#2772
No description provided.