[PR #2057] [MERGED] Server chunk packets decode on their own thread, not in the frame #3971

Closed
opened 2026-11-03 04:13:41 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

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

Base: main ← Head: perf/remote-chunks-off-render


📝 Commits (1)

  • 427b48a Server chunk packets decode on their own thread, not in the frame

📊 Changes

4 files changed (+449 additions, -200 deletions)

View changed files

📝 crates/horizon-client/src/game.rs (+1 -0)
📝 crates/horizon-client/src/game/remote.rs (+87 -198)
➕ crates/horizon-client/src/game/remote_chunks.rs (+355 -0)
📝 crates/horizon-client/src/streaming.rs (+6 -2)

📄 Description

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

Refs #1890 (stutter while flying fast in creative).

Every server chunk packet used to be decoded on the render thread: single player on its server thread, LAN guests and servers all do this. Decoding means sections, palettes, biome remapping and light, about 0.1 ms per column on an M4 Pro and several times that on slower CPUs. A frame got 1 ms for chunk packets. Once the backlog had waited 50 ms (BACKLOG_LIMIT), one frame decoded everything waiting.

What changes

  • Decoder thread (game/remote_chunks.rs): poll_remote hands each LevelChunkWithLight to a "Chunk decoder" thread as soon as it arrives. The frame that reaches the packet in order only inserts the finished column and its block entities. Packet order is kept: one thread, FIFO in and out, one result per submitted packet.
    • A column still decoding holds back what follows it, overdue or not, so a frame never waits for the decoder.
    • The thread asks for the render thread's priority (favour_current), since frames wait on it.
  • Format checks: the conversion and its inputs (dimension height and sky, the server's biome registry mapped to ours) moved unchanged into ChunkFormat::column and ChunkFormat::light. Remote gets a new Arc<ChunkFormat> whenever the registries or the dimension change. A column decoded with an older format is decoded again in the frame, so a respawn or dimension change can't apply a stale column.
  • Fallback: if the thread can't start, packets decode in the frame as before.
  • Forgetting columns: Streaming::forget removed a column's pending sections by scanning every pending section (meshing/waiting). When the storage moves or shrinks (teleport, view-distance change), thousands of columns are forgotten in one frame, so this was quadratic. It now removes the 24 keys directly. drop_remote_chunks_out_of_range drops the block entities of all gone columns in one pass.

Most of the diff is the conversion moving out of remote.rs (5,671 lines) into the new file; the new logic is ~150 lines.

Measured. Mac mini M4 Pro, --offscreen, RD 32, single player on the server thread. Flying 60 s at 22 blocks/s from spawn (horizon-ctl fly). Alternated runs, fast profile. The machine was loaded by CI (load 30-70), so frame times are paced by the shared GPU; CPU per frame is the render thread's own work.

CPU per frame (ms) main this PR
fresh world (generating while flying): p95 3.69 / 3.71 / 3.67 3.08 / 3.06 / 2.98
fresh world: p99 4.93 / 4.96 / 5.00 3.95 / 3.99 / 3.92
fresh world: update phase in the slowest 1% 3.2 / 3.6 / 3.4 1.3 / 1.6 / 1.7
explored world (same path flown back): p95 3.42 / 3.47 / 3.40 2.84 / 2.71 / 2.77
explored world: p99 5.32 / 5.35 / 5.42 4.00 / 3.93 / 3.99
explored world: update phase in the slowest 1% 3.9 / 3.8 / 3.9 1.9 / 1.8 / 2.0
max (all runs) 6.9-12.3 6.6-11.0 (renderer frames, not chunks)

With HORIZON_SPIKES=6 (temporary spans, not committed), main's slow frames during the flight were chunk decoding at 2.5-4.3 ms per frame (overdue drains). With this PR, none of the slow frames are chunk decoding.

A storage that moves far away (a teleport with RD 32 drops all 3,725 columns in one frame) took 56 ms; now it takes 36-45 ms. What's left is freeing the columns (17 ms) and the renderer's per-section removal (18 ms), which is a separate change.

I could not reproduce the reporter's 47.6 ms frame on the M4, so this says "Refs", not "Fixes". Their Ryzen decodes each column more slowly, which makes the overdue drains this removes longer.

Checks

  • tools/lan_e2e.py, host in-frame: chest, light, respawn, drops pass. With --server-thread: chest, light, respawn, portals, levels, distance pass. These cover dimension changes and view-distance changes.
  • New unit test game::remote_chunks::tests::the_decoder_returns_columns_in_order: 20 packets come back in order, identical to decoding in the frame, with biomes remapped.
  • pr_ready: see below.

No label: players see smoother frames but nothing new.

pr_ready (tools/pr_ready.py on ludovics-mac-mini, against origin/main at ee6dfd2b6, 47 min 13 s)

Check Result
Scope ⚠️ 4 files, +449 −200; 649 lines of code: over 500, split it if it can ship in parts
Cargo.lock ✅ unchanged
Public tree ✅
Python tests ➖ no Python change
rustfmt ✅ (5 s)
clippy ✅ (37 s)
tests: horizon-client ✅ 1295 passed (3 min 47 s)
doc tests ✅ 0 passed (4 s)
tokens.css up to date ➖ theme untouched
launcher rustfmt ➖ launcher untouched
launcher clippy ➖ launcher untouched
launcher tests ➖ launcher untouched
Parity ✅ 78 affected, 78 passed (42 min 18 s)
Label player-facing: feature if players get something new, no label for a fix (release-now if urgent)

The scope line counts the moved decode and light code: remote_chunks.rs takes over remote_chunk and wire_light from remote.rs, so the new logic is about half of it.

🤖 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/2057 **Author:** [@ludovic111](https://github.com/ludovic111) **Created:** 10/26/2026 **Status:** ✅ Merged **Merged:** 10/26/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `perf/remote-chunks-off-render` --- ### 📝 Commits (1) - [`427b48a`](https://github.com/horizon-rust/horizon/commit/427b48aa4210eb218f82a7036a931f53c4bf6a56) Server chunk packets decode on their own thread, not in the frame ### 📊 Changes **4 files changed** (+449 additions, -200 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/game.rs` (+1 -0) 📝 `crates/horizon-client/src/game/remote.rs` (+87 -198) ➕ `crates/horizon-client/src/game/remote_chunks.rs` (+355 -0) 📝 `crates/horizon-client/src/streaming.rs` (+6 -2) </details> ### 📄 Description > 🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself. Refs #1890 (stutter while flying fast in creative). Every server chunk packet used to be decoded on the render thread: single player on its server thread, LAN guests and servers all do this. Decoding means sections, palettes, biome remapping and light, about 0.1 ms per column on an M4 Pro and several times that on slower CPUs. A frame got 1 ms for chunk packets. Once the backlog had waited 50 ms (`BACKLOG_LIMIT`), one frame decoded everything waiting. **What changes** - **Decoder thread** (`game/remote_chunks.rs`): `poll_remote` hands each `LevelChunkWithLight` to a "Chunk decoder" thread as soon as it arrives. The frame that reaches the packet in order only inserts the finished column and its block entities. Packet order is kept: one thread, FIFO in and out, one result per submitted packet. - A column still decoding holds back what follows it, overdue or not, so a frame never waits for the decoder. - The thread asks for the render thread's priority (`favour_current`), since frames wait on it. - **Format checks**: the conversion and its inputs (dimension height and sky, the server's biome registry mapped to ours) moved unchanged into `ChunkFormat::column` and `ChunkFormat::light`. `Remote` gets a new `Arc<ChunkFormat>` whenever the registries or the dimension change. A column decoded with an older format is decoded again in the frame, so a respawn or dimension change can't apply a stale column. - **Fallback**: if the thread can't start, packets decode in the frame as before. - **Forgetting columns**: `Streaming::forget` removed a column's pending sections by scanning every pending section (`meshing`/`waiting`). When the storage moves or shrinks (teleport, view-distance change), thousands of columns are forgotten in one frame, so this was quadratic. It now removes the 24 keys directly. `drop_remote_chunks_out_of_range` drops the block entities of all gone columns in one pass. Most of the diff is the conversion moving out of `remote.rs` (5,671 lines) into the new file; the new logic is ~150 lines. **Measured.** Mac mini M4 Pro, `--offscreen`, RD 32, single player on the server thread. Flying 60 s at 22 blocks/s from spawn (`horizon-ctl fly`). Alternated runs, fast profile. The machine was loaded by CI (load 30-70), so frame times are paced by the shared GPU; CPU per frame is the render thread's own work. | CPU per frame (ms) | main | this PR | |---|---|---| | fresh world (generating while flying): p95 | 3.69 / 3.71 / 3.67 | 3.08 / 3.06 / 2.98 | | fresh world: p99 | 4.93 / 4.96 / 5.00 | 3.95 / 3.99 / 3.92 | | fresh world: `update` phase in the slowest 1% | 3.2 / 3.6 / 3.4 | 1.3 / 1.6 / 1.7 | | explored world (same path flown back): p95 | 3.42 / 3.47 / 3.40 | 2.84 / 2.71 / 2.77 | | explored world: p99 | 5.32 / 5.35 / 5.42 | 4.00 / 3.93 / 3.99 | | explored world: `update` phase in the slowest 1% | 3.9 / 3.8 / 3.9 | 1.9 / 1.8 / 2.0 | | max (all runs) | 6.9-12.3 | 6.6-11.0 (renderer frames, not chunks) | With `HORIZON_SPIKES=6` (temporary spans, not committed), main's slow frames during the flight were chunk decoding at 2.5-4.3 ms per frame (overdue drains). With this PR, none of the slow frames are chunk decoding. A storage that moves far away (a teleport with RD 32 drops all 3,725 columns in one frame) took 56 ms; now it takes 36-45 ms. What's left is freeing the columns (17 ms) and the renderer's per-section removal (18 ms), which is a separate change. I could not reproduce the reporter's 47.6 ms frame on the M4, so this says "Refs", not "Fixes". Their Ryzen decodes each column more slowly, which makes the overdue drains this removes longer. ### Checks - `tools/lan_e2e.py`, host in-frame: chest, light, respawn, drops pass. With `--server-thread`: chest, light, respawn, portals, levels, distance pass. These cover dimension changes and view-distance changes. - New unit test `game::remote_chunks::tests::the_decoder_returns_columns_in_order`: 20 packets come back in order, identical to decoding in the frame, with biomes remapped. - pr_ready: see below. No label: players see smoother frames but nothing new. **pr_ready** (`tools/pr_ready.py` on ludovics-mac-mini, against origin/main at ee6dfd2b6, 47 min 13 s) | Check | Result | | --- | --- | | Scope | ⚠️ 4 files, +449 −200; 649 lines of code: over 500, split it if it can ship in parts | | Cargo.lock | ✅ unchanged | | Public tree | ✅ | | Python tests | ➖ no Python change | | rustfmt | ✅ (5 s) | | clippy | ✅ (37 s) | | tests: horizon-client | ✅ 1295 passed (3 min 47 s) | | doc tests | ✅ 0 passed (4 s) | | tokens.css up to date | ➖ theme untouched | | launcher rustfmt | ➖ launcher untouched | | launcher clippy | ➖ launcher untouched | | launcher tests | ➖ launcher untouched | | Parity | ✅ 78 affected, 78 passed (42 min 18 s) | | Label | player-facing: `feature` if players get something new, no label for a fix (`release-now` if urgent) | The scope line counts the moved decode and light code: remote_chunks.rs takes over remote_chunk and wire_light from remote.rs, so the new logic is about half of it. 🤖 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-03 04:13:41 +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#3971
No description provided.