[PR #915] [MERGED] Chunk packets: pack paletted containers a long at a time (2.3x faster from_values) #2910

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

📋 Pull Request Information

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

Base: main ← Head: perf/paletted-pack


📝 Commits (1)

  • 00343ad Chunk packets: pack a paletted container a long at a time

📊 Changes

1 file changed (+18 additions, -6 deletions)

View changed files

📝 crates/horizon-protocol/src/chunk.rs (+18 -6)

📄 Description

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

What

Paletted::from_values (horizon-protocol), which turns a section's block states into the wire's PalettedContainer for every chunk packet, now packs its SimpleBitStorage longs a long at a time: values.chunks(64 / bits) folded into one u64 each. Before, it did data[i / per] |= id << ((i % per) * bits) for each of the 4096 values, with a division and a remainder by a runtime divisor every time. The palette pass is unchanged.

The bytes are the same: from_values_matches_the_linear_search already compares from_values with from_values_slow, which keeps the old packing loop, over 1, 2, 16, 17, 200, 300 and 5000 distinct values. That covers every block and biome bit width, both palettes and the global one.

Why

Brief perf-server-10, task 1: the server's big tick spikes are chunk packets encoded on the server thread (Hub::update_chunks → ChunkEncoder::body_in, 25–35 ms per batch walking on the ThinkPad). A 30 s sample of the server while one bot explored (Mac mini) put 59 % of body_in in Paletted::from_values (266 of ~450 samples). Heightmaps were 8 %, light 4 %, and the rest was the section scan around it.

Numbers

A microbenchmark of 200 terrain-like sections (a base block with runs of up to 20 other states, block ids up to 14000, 15-bit global palette). The old packing loop and the new from_values ran in the same process, interleaved, minimum of 30 rounds, M4 Pro:

per section before after
from_values 12.0 µs (7.7 µs of it the packing loop) 5.2 µs (-57 %)

With from_values at 59 % of body_in, that takes about a third off each chunk column's encoding. This PR doesn't measure that server-side on its own: the end-to-end numbers (ThinkPad, idle, one bot) are in the companion PR that moves the encoding off the server thread, measured with both together.

Checks

  • cargo fmt --all --check, cargo clippy --workspace --all-targets -- -D warnings: clean.
  • cargo test -p horizon-protocol: 62 passed.
  • On the integration commit fa1cec37 (this branch merged with perf/chunk-encode-workers and perf/guest-tick-stats on origin/main 87cf8cb9): cargo test -p horizon-client passes (881 lib + 76 bin + integration tests). Parity lan, dedicated_server, server_world_gen, chunk_stage, world_reopen, scoreboard: 6 passed, 0 failed. CI parity covers the rest of --changed.

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/915 **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/paletted-pack` --- ### 📝 Commits (1) - [`00343ad`](https://github.com/horizon-rust/horizon/commit/00343ad2e28218c8c644d46f58467869de654b65) Chunk packets: pack a paletted container a long at a time ### 📊 Changes **1 file changed** (+18 additions, -6 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-protocol/src/chunk.rs` (+18 -6) </details> ### 📄 Description > 🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself. ## What `Paletted::from_values` (horizon-protocol), which turns a section's block states into the wire's `PalettedContainer` for every chunk packet, now packs its `SimpleBitStorage` longs a long at a time: `values.chunks(64 / bits)` folded into one `u64` each. Before, it did `data[i / per] |= id << ((i % per) * bits)` for each of the 4096 values, with a division and a remainder by a runtime divisor every time. The palette pass is unchanged. The bytes are the same: `from_values_matches_the_linear_search` already compares `from_values` with `from_values_slow`, which keeps the old packing loop, over 1, 2, 16, 17, 200, 300 and 5000 distinct values. That covers every block and biome bit width, both palettes and the global one. ## Why Brief perf-server-10, task 1: the server's big tick spikes are chunk packets encoded on the server thread (`Hub::update_chunks` → `ChunkEncoder::body_in`, 25–35 ms per batch walking on the ThinkPad). A 30 s `sample` of the server while one bot explored (Mac mini) put **59 % of `body_in` in `Paletted::from_values`** (266 of ~450 samples). Heightmaps were 8 %, light 4 %, and the rest was the section scan around it. ## Numbers A microbenchmark of 200 terrain-like sections (a base block with runs of up to 20 other states, block ids up to 14000, 15-bit global palette). The old packing loop and the new `from_values` ran in the same process, interleaved, minimum of 30 rounds, M4 Pro: | per section | before | after | |---|---|---| | `from_values` | 12.0 µs (7.7 µs of it the packing loop) | 5.2 µs (-57 %) | With `from_values` at 59 % of `body_in`, that takes about a third off each chunk column's encoding. This PR doesn't measure that server-side on its own: the end-to-end numbers (ThinkPad, idle, one bot) are in the companion PR that moves the encoding off the server thread, measured with both together. ## Checks - `cargo fmt --all --check`, `cargo clippy --workspace --all-targets -- -D warnings`: clean. - `cargo test -p horizon-protocol`: 62 passed. - On the integration commit fa1cec37 (this branch merged with perf/chunk-encode-workers and perf/guest-tick-stats on origin/main 87cf8cb9): `cargo test -p horizon-client` passes (881 lib + 76 bin + integration tests). Parity `lan`, `dedicated_server`, `server_world_gen`, `chunk_stage`, `world_reopen`, `scoreboard`: 6 passed, 0 failed. CI parity covers the rest of `--changed`. ## 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:02 +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#2910
No description provided.