[PR #7873] Chunk encoding: deduplicate shared sections and fast-path uniform palettes #7875

Open
opened 2026-11-03 07:25:50 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon/pull/7873
Author: @rdbtCVS
Created: 11/2/2026
Status: 🔄 Open

Base: main ← Head: perf/server-chunk-encoding


📝 Commits (1)

  • dfcc1b9 perf: avoid repeated work in chunk section encoding

📊 Changes

3 files changed (+258 additions, -3 deletions)

View changed files

📝 crates/horizon-client-lib/Cargo.toml (+4 -0)
➕ crates/horizon-client/examples/chunk_encoding_bench.rs (+168 -0)
📝 crates/horizon-client/src/lan/chunks.rs (+86 -3)

📄 Description

🤖 Opened by rdbtCVS's AI agent (Codex, gpt-6.1-sol), not by rdbtCVS personally.

What and why

Chunk packets for many separated players repeatedly encode shared air sections and process single-value palettes as though they contained many values. This PR removes three independent sources of work:

  1. ChunkEncoder::job schedules each distinct Arc<Section> once. A shared section can occur in many columns, and a column can occur twice; both still produce the same packets. The existing worker-dispatch cutoff is preserved.
  2. occupied reads a single-value block palette directly instead of looking it up in the section cache. Mixed palettes keep their cached path, and the existing one-lock-per-column policy remains.
  3. SectionCoder::biomes directly builds the single-value network palette, avoiding expansion to 64 values and reconstruction of the same palette. Mixed palettes and registry-ID fallback keep their existing path.

Fixes #7852. Independent of #7719 and #7836; no gameplay tick order, RNG draws, mod callback scheduling, worker counts or packet ordering change.

Benchmark

HORIZON_WORKER_THREADS=12 cargo run --profile fast -p horizon-client-lib --example chunk_encoding_bench -- .local/horizon-client/26.3

Linux x86_64, 6 physical / 12 logical CPUs, rustc 1.99.0, fast profile. The same benchmark example and example registration were built against baseline f1d6576b3, a dedup-only variant, and the final implementation. Final A/B order was baseline/final/final/baseline, with seven retained samples per run after warm-up: 14 samples per case. Repeated with one worker as a serial control. Both byte counts and packet checksums matched in every case and configuration.

Fixtures contain 100 separated areas 2,400 blocks apart. Each area sends a 1×1, 2×2 or 4×4 batch, with its neighbouring columns resident. Mixed fixtures have eight independently allocated non-uniform block sections per column and shared air above; the air fixture shares its uniform sections. They use the production encoder and real prepared state/heightmap tables. The benchmark excludes fixture/encoder setup and sockets/compression; it times warm-up plus encoding for cold caches, or encoding after caches are populated for warm caches. Result bodies are dropped inside the measurement. It reports main-thread CPU time and wall time separately; worker CPU is not included in main-thread CPU time. These are component measurements, not server MSPT or TPS.

All table times are milliseconds, before → after, with twelve reported cores (eleven encoding helpers):

Areas/players Columns per area Mixed block sections per column Phase Main-thread CPU Wall time
1 1 8 cold 0.1052 → 0.1065 0.1061 → 0.1092
1 1 8 warm 0.0024 → 0.0022 0.0025 → 0.0023
100 1 8 biomes only 0.5530 → 0.0577 0.5656 → 0.0586
100 1 8 cold 2.4962 → 2.2828 2.5233 → 2.7231
100 1 8 warm 0.3238 → 0.3515 0.3395 → 0.3581
100 4 8 cold 9.4275 → 8.9261 9.8138 → 9.3462
100 4 8 warm 1.6512 → 1.6660 1.7092 → 1.7501
100 16 8 cold 41.7495 → 39.2343 43.8528 → 40.4071
100 16 8 warm 10.3549 → 9.7564 10.6254 → 10.1097
100 16 0 cold 10.4449 → 5.7873 10.5970 → 5.8881
100 16 0 warm 2.7217 → 2.2017 2.7412 → 2.2143

Small-batch controls do not all improve: 100 isolated warm columns cost 0.324 → 0.351 ms CPU. The demonstrated gains are in uniform-biome encoding and larger/shared-section sends.

Evidence for each change

  • Deduplication alone: the 1,600-column cold air fixture fell from 10.97 to 6.38 ms of main-thread CPU, and 11.21 to 6.72 ms wall time, in the staged baseline/dedup/final/final/dedup/baseline runs. Mixed-column cold wall time also improved, although CPU allocation between the main thread and helpers varied.
  • Uniform occupancy: warm packet loops already have encoded sections and heightmaps, so this shortcut is the change exercised by their occupancy reads. The final A/B warm air case falls from 2.72 to 2.20 ms CPU; the 1,600-column mixed warm case also improves. The existing per-column lock is retained because a lazy-lock variant was less consistent on mixed columns.
  • Uniform biomes: the separate biome-only phase calls neither warm-up nor occupancy. It falls from 0.553 to 0.058 ms CPU for 100 columns, about 9.6× faster.

Batch-wide neighbouring-mask memoisation was also benchmarked and discarded: it added overhead on mixed-column cases. The simpler local single-value shortcut is retained instead. Existing encoding helpers already parallelise cold work; the serial-control runs exercise the no-helper path, so these changes do not depend on adding another pool.

Validation

  • All 19 chunk-encoder unit tests passed, including new single/mixed palette and unknown-ID cases, edit invalidation, shared-section warm-up, tall/Nether columns, proto neighbours, and exact packet-body comparisons.
  • Dedicated-server vanilla parity: 602 transcript lines identical; Horizon-client connection, spawn, chunks and chat checks passed.
  • Server-entity vanilla parity: 34 entities identical.
  • Real dedicated-server load smoke test: 100 survival bots, 1,200-block spacing, view/simulation distance 10, normal terrain starting from a copy of a synthetic test world; all 100 remained connected. This is a correctness/load check, not a controlled whole-server speedup claim. Simulation remains a separate bottleneck.
  • Port ledger checked against origin/main: no new gaps.

Kind

  • perf: faster, or less memory

Vanilla reference

LevelChunkSection.write, PalettedContainer.write, PalettedContainerRO.write (inspected through the port ledger and tools/rea_vanilla.py). Single-value palettes still use the same network IDs and canonical palette fields; mixed palettes keep their original path. Section identity affects only duplicate warm-up work, not any packet contents.

Other projects' code

None.

Self-check

pr_ready (tools/pr_ready.py --quick on archlinux, against origin/main at f1d6576b3, 9 min 57 s)

Check Result
Scope ✅ 3 files, +258 −3
Cargo.lock ✅ unchanged
Public tree ✅
Python tests ✅ 614 passed (1 min 5 s)
rustfmt ✅ (9 s)
clippy ✅ (1 min 3 s)
tests: horizon-client-lib ✅ 1477 passed (6 min 23 s)
doc tests ✅ 0 passed (1 s)
content root tests ➖ content root untouched
mod API up to date ➖ native hooks untouched
tokens.css up to date ➖ theme untouched
launcher rustfmt ➖ launcher untouched
launcher clippy ➖ launcher untouched
launcher tests ➖ launcher untouched
Parity ➖ 236 affected, to run in CI (--quick): ambient_sound, anvil_turtle_egg, armor_stand_frost, armor_stand_turtle_egg, arrow, arrow_wind_charge, audio_attributes, auto_jump, block_entity_anim, block_entity_render, block_entity_tickers, block_particles and 224 more
Label player-facing: feature if players get something new, no label for a fix (release-now if urgent)

🤖 Generated with Codex.


🔄 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/7873 **Author:** [@rdbtCVS](https://github.com/rdbtCVS) **Created:** 11/2/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `perf/server-chunk-encoding` --- ### 📝 Commits (1) - [`dfcc1b9`](https://github.com/horizon-rust/horizon/commit/dfcc1b98f11b7e8420e7cb2d4985d44fe1c1a052) perf: avoid repeated work in chunk section encoding ### 📊 Changes **3 files changed** (+258 additions, -3 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client-lib/Cargo.toml` (+4 -0) ➕ `crates/horizon-client/examples/chunk_encoding_bench.rs` (+168 -0) 📝 `crates/horizon-client/src/lan/chunks.rs` (+86 -3) </details> ### 📄 Description > 🤖 Opened by rdbtCVS's AI agent (Codex, gpt-6.1-sol), not by rdbtCVS personally. ## What and why Chunk packets for many separated players repeatedly encode shared air sections and process single-value palettes as though they contained many values. This PR removes three independent sources of work: 1. `ChunkEncoder::job` schedules each distinct `Arc<Section>` once. A shared section can occur in many columns, and a column can occur twice; both still produce the same packets. The existing worker-dispatch cutoff is preserved. 2. `occupied` reads a single-value block palette directly instead of looking it up in the section cache. Mixed palettes keep their cached path, and the existing one-lock-per-column policy remains. 3. `SectionCoder::biomes` directly builds the single-value network palette, avoiding expansion to 64 values and reconstruction of the same palette. Mixed palettes and registry-ID fallback keep their existing path. Fixes #7852. Independent of #7719 and #7836; no gameplay tick order, RNG draws, mod callback scheduling, worker counts or packet ordering change. ## Benchmark ``` HORIZON_WORKER_THREADS=12 cargo run --profile fast -p horizon-client-lib --example chunk_encoding_bench -- .local/horizon-client/26.3 ``` Linux x86_64, 6 physical / 12 logical CPUs, rustc 1.99.0, `fast` profile. The same benchmark example and example registration were built against baseline `f1d6576b3`, a dedup-only variant, and the final implementation. Final A/B order was baseline/final/final/baseline, with seven retained samples per run after warm-up: 14 samples per case. Repeated with one worker as a serial control. Both byte counts and packet checksums matched in every case and configuration. Fixtures contain 100 separated areas 2,400 blocks apart. Each area sends a 1×1, 2×2 or 4×4 batch, with its neighbouring columns resident. Mixed fixtures have eight independently allocated non-uniform block sections per column and shared air above; the air fixture shares its uniform sections. They use the production encoder and real prepared state/heightmap tables. The benchmark excludes fixture/encoder setup and sockets/compression; it times warm-up plus encoding for cold caches, or encoding after caches are populated for warm caches. Result bodies are dropped inside the measurement. It reports main-thread CPU time and wall time separately; worker CPU is not included in main-thread CPU time. These are component measurements, not server MSPT or TPS. All table times are milliseconds, before → after, with twelve reported cores (eleven encoding helpers): | Areas/players | Columns per area | Mixed block sections per column | Phase | Main-thread CPU | Wall time | |---|---|---|---|---|---| | 1 | 1 | 8 | cold | 0.1052 → 0.1065 | 0.1061 → 0.1092 | | 1 | 1 | 8 | warm | 0.0024 → 0.0022 | 0.0025 → 0.0023 | | 100 | 1 | 8 | biomes only | 0.5530 → 0.0577 | 0.5656 → 0.0586 | | 100 | 1 | 8 | cold | 2.4962 → 2.2828 | 2.5233 → 2.7231 | | 100 | 1 | 8 | warm | 0.3238 → 0.3515 | 0.3395 → 0.3581 | | 100 | 4 | 8 | cold | 9.4275 → 8.9261 | 9.8138 → 9.3462 | | 100 | 4 | 8 | warm | 1.6512 → 1.6660 | 1.7092 → 1.7501 | | 100 | 16 | 8 | cold | 41.7495 → 39.2343 | 43.8528 → 40.4071 | | 100 | 16 | 8 | warm | 10.3549 → 9.7564 | 10.6254 → 10.1097 | | 100 | 16 | 0 | cold | 10.4449 → 5.7873 | 10.5970 → 5.8881 | | 100 | 16 | 0 | warm | 2.7217 → 2.2017 | 2.7412 → 2.2143 | Small-batch controls do not all improve: 100 isolated warm columns cost 0.324 → 0.351 ms CPU. The demonstrated gains are in uniform-biome encoding and larger/shared-section sends. ### Evidence for each change - **Deduplication alone:** the 1,600-column cold air fixture fell from 10.97 to 6.38 ms of main-thread CPU, and 11.21 to 6.72 ms wall time, in the staged baseline/dedup/final/final/dedup/baseline runs. Mixed-column cold wall time also improved, although CPU allocation between the main thread and helpers varied. - **Uniform occupancy:** warm packet loops already have encoded sections and heightmaps, so this shortcut is the change exercised by their occupancy reads. The final A/B warm air case falls from 2.72 to 2.20 ms CPU; the 1,600-column mixed warm case also improves. The existing per-column lock is retained because a lazy-lock variant was less consistent on mixed columns. - **Uniform biomes:** the separate biome-only phase calls neither warm-up nor occupancy. It falls from 0.553 to 0.058 ms CPU for 100 columns, about 9.6× faster. Batch-wide neighbouring-mask memoisation was also benchmarked and discarded: it added overhead on mixed-column cases. The simpler local single-value shortcut is retained instead. Existing encoding helpers already parallelise cold work; the serial-control runs exercise the no-helper path, so these changes do not depend on adding another pool. ## Validation - All 19 chunk-encoder unit tests passed, including new single/mixed palette and unknown-ID cases, edit invalidation, shared-section warm-up, tall/Nether columns, proto neighbours, and exact packet-body comparisons. - Dedicated-server vanilla parity: 602 transcript lines identical; Horizon-client connection, spawn, chunks and chat checks passed. - Server-entity vanilla parity: 34 entities identical. - Real dedicated-server load smoke test: 100 survival bots, 1,200-block spacing, view/simulation distance 10, normal terrain starting from a copy of a synthetic test world; all 100 remained connected. This is a correctness/load check, not a controlled whole-server speedup claim. Simulation remains a separate bottleneck. - Port ledger checked against `origin/main`: no new gaps. ## Kind - [x] perf: faster, or less memory ## Vanilla reference `LevelChunkSection.write`, `PalettedContainer.write`, `PalettedContainerRO.write` (inspected through the port ledger and `tools/rea_vanilla.py`). Single-value palettes still use the same network IDs and canonical palette fields; mixed palettes keep their original path. Section identity affects only duplicate warm-up work, not any packet contents. ## Other projects' code None. ## Self-check **pr_ready** (`tools/pr_ready.py --quick` on archlinux, against origin/main at f1d6576b3, 9 min 57 s) | Check | Result | | --- | --- | | Scope | ✅ 3 files, +258 −3 | | Cargo.lock | ✅ unchanged | | Public tree | ✅ | | Python tests | ✅ 614 passed (1 min 5 s) | | rustfmt | ✅ (9 s) | | clippy | ✅ (1 min 3 s) | | tests: horizon-client-lib | ✅ 1477 passed (6 min 23 s) | | doc tests | ✅ 0 passed (1 s) | | content root tests | ➖ content root untouched | | mod API up to date | ➖ native hooks untouched | | tokens.css up to date | ➖ theme untouched | | launcher rustfmt | ➖ launcher untouched | | launcher clippy | ➖ launcher untouched | | launcher tests | ➖ launcher untouched | | Parity | ➖ 236 affected, to run in CI (--quick): ambient_sound, anvil_turtle_egg, armor_stand_frost, armor_stand_turtle_egg, arrow, arrow_wind_charge, audio_attributes, auto_jump, block_entity_anim, block_entity_render, block_entity_tickers, block_particles and 224 more | | Label | player-facing: `feature` if players get something new, no label for a fix (`release-now` if urgent) | 🤖 Generated with [Codex](https://openai.com/codex/). --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
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#7875
No description provided.