[PR #934] [MERGED] Chunk batches: encode each column once a tick, its new sections on warm-up threads #2925

Closed
opened 2026-11-02 16:00:09 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

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

Base: main ← Head: perf/chunk-encode-workers


📝 Commits (1)

  • 1f2f2a9 Chunk batches: encode each column once a tick, its new sections on warm-up threads

📊 Changes

2 files changed (+415 additions, -148 deletions)

View changed files

📝 crates/horizon-client/src/lan/chunks.rs (+291 -75)
📝 crates/horizon-client/src/lan/hub.rs (+124 -73)

📄 Description

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

What

Chunk packets are still built and sent on the server thread, in the same order and with the same pacing, but each one is cheaper there:

  • Once per column per tick, for everyone. Hub::update_chunks first works out each guest's batch (next_batch: the PlayerChunkSender pacing code, unchanged), then encodes each distinct column once (body_in) and gives every guest that asked for it the same bytes. Before, two guests getting the same column in one tick encoded it twice.
  • New sections on warm-up threads. ChunkEncoder::warm(&columns) runs before the bodies are built. It finds the sections not yet in the encoder's section cache. If there are at least 16, it posts them to a small persistent pool (horizon-chunk-encode-*, one thread less than the cores) and works on the list itself as well. Each section's wire bytes and light layers land in their own OnceLock slot; nothing is shared but an atomic index. The server thread never waits for a helper. Once it has run out of sections to claim, it moves the finished slots into the cache under one lock nobody else takes, and body_in encodes whatever a helper had not finished, as before. So a helper that the OS doesn't schedule costs nothing but its unfinished share.
  • The read-only parts of the encoder (block flags, biome ids, the biome bit width) moved into SectionCoder, an Arc the helpers share. section and section_wire moved with them; their code is unchanged.

The wire format and order are unchanged: a guest's packets are still ChunkBatchStart, its columns in queue order, ChunkBatchFinished, sent in the same tick as before.

Pacing compared with vanilla

I checked next_batch against 26.3's PlayerChunkSender. They match:

  • desiredChunksPerTick starts at 9 and is clamped to 0.01–64; NaN becomes 0.01.
  • maxUnacknowledgedBatches is 1 until the first acknowledgement, then 10.
  • batchQuota = min(quota + desired, max(1, desired)) starts at 0 and goes back to 1 when nothing is left unacknowledged.

The memory-connection branch (single player's own client) doesn't apply to LAN guests. This PR doesn't change any of it, only moves the code into next_batch.

Why

Brief perf-server-10, task 1: move chunk-packet encoding off the server thread. On the ThinkPad, idle with one bot, the server's biggest tick spikes were lan_chunks at 25–35 ms. A profile put 59 % of body_in in Paletted::from_values; #915 makes that 2.3x faster, and this PR takes most of what's left off the server thread.

Numbers

ThinkPad (4 threads, otherwise idle; one bot, tools/server_bench.py, HORIZON_SPIKES=0 so every tick is logged with its spans; base = main, new = main + this PR + #915):

These runs used this PR's first version, whose helpers shared the cache under a lock. The ThinkPad was then taken by other agents' benchmarks; the current version (per-section slots, no shared lock) will be measured there again when it is free and posted as a comment. The Mac numbers below are the current version.

1 bot tick wall p50 / p99 / max server-thread CPU p99 / max MSPT avg / p95 / p99
explore 1, base 3.3 / 21.6 / 44.3 ms 9.4 / 24.5 ms 5.16 / 13.76 / 23.1
explore 1, new 1.8 / 11.1 / 26.8 ms 4.3 / 13.4 ms 2.68 / 6.45 / 11.53
explore 2, base 3.5 / 25.9 / 64.5 ms 18.6 / 41.4 ms 6.04 / 18.04 / 31.77
explore 2, new 3.8 / 17.6 / 53.2 ms 7.4 / 26.3 ms 5.77 / 13.54 / 22.9
walk 1, base 2.1 / 13.9 / 63.1 ms 13.6 / 60.0 ms 2.68 / 5.81 / 17.63
walk 1, new 1.1 / 8.5 / 30.6 ms 6.3 / 27.4 ms 1.68 / 4.62 / 11.33
walk 2, base 1.1 / 17.9 / 46.4 ms 6.4 / 24.2 ms 2.05 / 5.93 / 16.91
walk 2, new 2.0 / 10.8 / 48.1 ms 7.7 / 47.6 ms 2.38 / 5.04 / 12.42

Exploring, the server thread's CPU p99 halves or better and the tick p99 drops by a third to a half. In explore 2 the first version's helpers made the server thread wait on the shared lock (ticks with lan_chunks at 34 ms wall but 2.6 ms CPU). That is why the current version shares nothing but an atomic index and never waits.

Mac mini under load (40 bots, the machine 80–95 % busy with other sessions; base f2a323a2, new = base + this PR; ticks after the first 400):

40 bots server-thread CPU per tick avg / p99 tick wall p50 / p99 tick_lan span sum MSPT avg / p95 / p99
explore 1, base 16.5 / 48.9 ms 24.3 / 240 ms 60.6 s 52.2 / 153 / 240
explore 1, new 11.7 / 40.7 ms (-29 % / -17 %) 19.6 / 289 ms 46.0 s (-24 %) 61.3 / 193 / 304
explore 2, base 14.1 / 41.8 ms 25.0 / 327 ms 68.7 s 85.0 / 244 / 388
explore 2, new 10.5 / 34.9 ms (-26 % / -17 %) 24.9 / 321 ms 55.7 s (-19 %) 78.4 / 239 / 347
walk, base 1.79 / 3.7 ms 2.3 / 33 ms 8.9 s 5.2 / 20 / 52
walk, new 1.79 / 3.7 ms 2.7 / 43 ms 11.9 s 7.5 / 28 / 54

On the loaded Mac, the server thread's own CPU time drops by a quarter while exploring. Wall time there is set by how often the OS preempts the server thread, so its p99 and MSPT move both ways between runs. Walking sends few new sections (most columns are cached or under the 16-section threshold), so its CPU time is unchanged; the walk wall difference is within this machine's run-to-run noise.

Checks

  • Byte-identical bodies: the new test warmed_columns_give_the_same_bodies builds 4x4 varied columns (and a duplicate, sign block entities, full light). It compares body_in from a fresh encoder with a warmed one, then runs warm again with everything cached, and with no columns. The existing fresh() comparison keeps checking cached against uncached encoding.
  • cargo fmt --all --check, cargo clippy --workspace --all-targets -- -D warnings: clean.
  • Integration commit e9730e3d (origin/main b77891df + this branch + #915, #914 and #927): parity lan, dedicated_server, server_world_gen, chunk_stage, world_reopen, scoreboard, server_entities, server_dimension_players, server_player_targets: 9 passed, 0 failed. cargo test -p horizon-client there: 882 lib and 76 bin tests passed. lan_bot failed because of #927's timing change, which #927 now handles in the test; with that fix, every horizon-client test passes on #927's head.
  • The earlier integration commit fa1cec37 (the first version of this branch + #915 + #914): cargo test -p horizon-client passed. Parity lan, dedicated_server, server_world_gen, chunk_stage, world_reopen, scoreboard: 6 passed.

Needs Ludovic

Nothing.

🤖 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/934 **Author:** [@ludovic111](https://github.com/ludovic111) **Created:** 10/18/2026 **Status:** ✅ Merged **Merged:** 10/18/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `perf/chunk-encode-workers` --- ### 📝 Commits (1) - [`1f2f2a9`](https://github.com/horizon-rust/horizon/commit/1f2f2a9f6d8ffbbdb8a67f51d7a67e4693bd68e0) Chunk batches: encode each column once a tick, its new sections on warm-up threads ### 📊 Changes **2 files changed** (+415 additions, -148 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/lan/chunks.rs` (+291 -75) 📝 `crates/horizon-client/src/lan/hub.rs` (+124 -73) </details> ### 📄 Description > 🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself. ## What Chunk packets are still built and sent on the server thread, in the same order and with the same pacing, but each one is cheaper there: - **Once per column per tick, for everyone.** `Hub::update_chunks` first works out each guest's batch (`next_batch`: the `PlayerChunkSender` pacing code, unchanged), then encodes each distinct column once (`body_in`) and gives every guest that asked for it the same bytes. Before, two guests getting the same column in one tick encoded it twice. - **New sections on warm-up threads.** `ChunkEncoder::warm(&columns)` runs before the bodies are built. It finds the sections not yet in the encoder's section cache. If there are at least 16, it posts them to a small persistent pool (`horizon-chunk-encode-*`, one thread less than the cores) and works on the list itself as well. Each section's wire bytes and light layers land in their own `OnceLock` slot; nothing is shared but an atomic index. The server thread never waits for a helper. Once it has run out of sections to claim, it moves the finished slots into the cache under one lock nobody else takes, and `body_in` encodes whatever a helper had not finished, as before. So a helper that the OS doesn't schedule costs nothing but its unfinished share. - The read-only parts of the encoder (block flags, biome ids, the biome bit width) moved into `SectionCoder`, an `Arc` the helpers share. `section` and `section_wire` moved with them; their code is unchanged. The wire format and order are unchanged: a guest's packets are still `ChunkBatchStart`, its columns in queue order, `ChunkBatchFinished`, sent in the same tick as before. ### Pacing compared with vanilla I checked `next_batch` against 26.3's `PlayerChunkSender`. They match: - `desiredChunksPerTick` starts at 9 and is clamped to 0.01–64; NaN becomes 0.01. - `maxUnacknowledgedBatches` is 1 until the first acknowledgement, then 10. - `batchQuota = min(quota + desired, max(1, desired))` starts at 0 and goes back to 1 when nothing is left unacknowledged. The memory-connection branch (single player's own client) doesn't apply to LAN guests. This PR doesn't change any of it, only moves the code into `next_batch`. ## Why Brief perf-server-10, task 1: move chunk-packet encoding off the server thread. On the ThinkPad, idle with one bot, the server's biggest tick spikes were `lan_chunks` at 25–35 ms. A profile put 59 % of `body_in` in `Paletted::from_values`; #915 makes that 2.3x faster, and this PR takes most of what's left off the server thread. ## Numbers **ThinkPad** (4 threads, otherwise idle; one bot, `tools/server_bench.py`, `HORIZON_SPIKES=0` so every tick is logged with its spans; base = main, new = main + this PR + #915): These runs used this PR's first version, whose helpers shared the cache under a lock. The ThinkPad was then taken by other agents' benchmarks; the current version (per-section slots, no shared lock) will be measured there again when it is free and posted as a comment. The Mac numbers below are the current version. | 1 bot | tick wall p50 / p99 / max | server-thread CPU p99 / max | MSPT avg / p95 / p99 | |---|---|---|---| | explore 1, base | 3.3 / 21.6 / 44.3 ms | 9.4 / 24.5 ms | 5.16 / 13.76 / 23.1 | | explore 1, new | 1.8 / 11.1 / 26.8 ms | 4.3 / 13.4 ms | 2.68 / 6.45 / 11.53 | | explore 2, base | 3.5 / 25.9 / 64.5 ms | 18.6 / 41.4 ms | 6.04 / 18.04 / 31.77 | | explore 2, new | 3.8 / 17.6 / 53.2 ms | 7.4 / 26.3 ms | 5.77 / 13.54 / 22.9 | | walk 1, base | 2.1 / 13.9 / 63.1 ms | 13.6 / 60.0 ms | 2.68 / 5.81 / 17.63 | | walk 1, new | 1.1 / 8.5 / 30.6 ms | 6.3 / 27.4 ms | 1.68 / 4.62 / 11.33 | | walk 2, base | 1.1 / 17.9 / 46.4 ms | 6.4 / 24.2 ms | 2.05 / 5.93 / 16.91 | | walk 2, new | 2.0 / 10.8 / 48.1 ms | 7.7 / 47.6 ms | 2.38 / 5.04 / 12.42 | Exploring, the server thread's CPU p99 halves or better and the tick p99 drops by a third to a half. In explore 2 the first version's helpers made the server thread wait on the shared lock (ticks with `lan_chunks` at 34 ms wall but 2.6 ms CPU). That is why the current version shares nothing but an atomic index and never waits. **Mac mini under load** (40 bots, the machine 80–95 % busy with other sessions; base f2a323a2, new = base + this PR; ticks after the first 400): | 40 bots | server-thread CPU per tick avg / p99 | tick wall p50 / p99 | `tick_lan` span sum | MSPT avg / p95 / p99 | |---|---|---|---|---| | explore 1, base | 16.5 / 48.9 ms | 24.3 / 240 ms | 60.6 s | 52.2 / 153 / 240 | | explore 1, new | 11.7 / 40.7 ms (-29 % / -17 %) | 19.6 / 289 ms | 46.0 s (-24 %) | 61.3 / 193 / 304 | | explore 2, base | 14.1 / 41.8 ms | 25.0 / 327 ms | 68.7 s | 85.0 / 244 / 388 | | explore 2, new | 10.5 / 34.9 ms (-26 % / -17 %) | 24.9 / 321 ms | 55.7 s (-19 %) | 78.4 / 239 / 347 | | walk, base | 1.79 / 3.7 ms | 2.3 / 33 ms | 8.9 s | 5.2 / 20 / 52 | | walk, new | 1.79 / 3.7 ms | 2.7 / 43 ms | 11.9 s | 7.5 / 28 / 54 | On the loaded Mac, the server thread's own CPU time drops by a quarter while exploring. Wall time there is set by how often the OS preempts the server thread, so its p99 and MSPT move both ways between runs. Walking sends few new sections (most columns are cached or under the 16-section threshold), so its CPU time is unchanged; the walk wall difference is within this machine's run-to-run noise. ## Checks - Byte-identical bodies: the new test `warmed_columns_give_the_same_bodies` builds 4x4 varied columns (and a duplicate, sign block entities, full light). It compares `body_in` from a fresh encoder with a warmed one, then runs `warm` again with everything cached, and with no columns. The existing `fresh()` comparison keeps checking cached against uncached encoding. - `cargo fmt --all --check`, `cargo clippy --workspace --all-targets -- -D warnings`: clean. - Integration commit e9730e3d (origin/main b77891df + this branch + #915, #914 and #927): parity `lan`, `dedicated_server`, `server_world_gen`, `chunk_stage`, `world_reopen`, `scoreboard`, `server_entities`, `server_dimension_players`, `server_player_targets`: 9 passed, 0 failed. `cargo test -p horizon-client` there: 882 lib and 76 bin tests passed. `lan_bot` failed because of #927's timing change, which #927 now handles in the test; with that fix, every `horizon-client` test passes on #927's head. - The earlier integration commit fa1cec37 (the first version of this branch + #915 + #914): `cargo test -p horizon-client` passed. Parity `lan`, `dedicated_server`, `server_world_gen`, `chunk_stage`, `world_reopen`, `scoreboard`: 6 passed. ## Needs Ludovic Nothing. 🤖 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 16:00:09 +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#2925
No description provided.