[PR #623] [MERGED] Dedicated server: encode each chunk section once, not once per player #2677

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

📋 Pull Request Information

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

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


📝 Commits (3)

  • 943dd21 Dedicated server: encode each chunk section once, not once per player
  • edffb85 LAN chunks: compute a column's light once, and keep sections' light layers
  • 3d21289 Merge main into perf/server-chunk-encoding

📊 Changes

2 files changed (+392 additions, -62 deletions)

View changed files

📝 crates/horizon-client/src/lan/chunks.rs (+308 -62)
📝 crates/horizon-protocol/src/chunk.rs (+84 -0)

📄 Description

This fixes most of the gap at 10 to 100 players between Horizon's dedicated server and vanilla's. Chunk packets took nearly all of the tick: each send re-encoded the column for that one player.

The profile
macOS sample on the server with 50 walking bots: about 85 % of the main thread went to ChunkEncoder::encode_in. The parts were:

  • Paletted::from_values: linear palette searches for every block;
  • unpack_u16 over all 9 × 24 sections around the column, to find which light layers exist;
  • light layers packed a nibble at a time;
  • heightmaps primed again.

Walking players re-receive the chunks they leave and come back to, about 10 chunks per second per player on both servers. So at 50 bots, Horizon encoded about 450 columns a second and could not keep up. It ran 280 ms ticks where vanilla ran 33 ms on the benchmark ThinkPad (the benchmark PR has the table).

The change

  • Section cache. ChunkEncoder keeps what each resident section gives the wire, by the section's address: its bytes, whether it holds block entity blocks, and whether it is all air.
    • Sections are immutable behind Arc, and every edit goes through Arc::make_mut. With a Weak held, make_mut moves the edited section to a new address and keeps the old one from being reused. So the same address means the same, unchanged section.
    • Dead entries are swept when the map grows.
  • Heightmap cache. Heightmaps are kept per column the same way.
  • Unchanged. Block entity tags are still read for every packet, and the light layers are still built per packet, now in one pass.
  • Smaller fixes, same output:
    • Paletted::from_values uses an index table (same palette order and bits);
    • the all-air check looks at the palette first and stops at the first block;
    • uniform sections skip unpacking.

Measured with tick query on a busy Mac mini, 50 walking bots:

  • about 400 ms ticks with the faster palette and air checks alone;
  • 14 ms with the caches.

The ThinkPad benchmark is being rerun with this change.

Checks

  • New tests:
    • from_values matches the old linear search;
    • cached sections follow edits;
    • light layers pack the same.
  • LAN tests and lan_bot pass; clippy passes.
  • tools/parity.py dedicated_server passes. It compares the chunk bytes with vanilla's server: 541 transcript lines identical.

🤖 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/623 **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/server-chunk-encoding` --- ### 📝 Commits (3) - [`943dd21`](https://github.com/horizon-rust/horizon/commit/943dd210d7369ba75456d4df7b76e6fd76f41126) Dedicated server: encode each chunk section once, not once per player - [`edffb85`](https://github.com/horizon-rust/horizon/commit/edffb857e116389b399822def3a768d997b15221) LAN chunks: compute a column's light once, and keep sections' light layers - [`3d21289`](https://github.com/horizon-rust/horizon/commit/3d21289df7715f4b978ed355adda1d416b813d3d) Merge main into perf/server-chunk-encoding ### 📊 Changes **2 files changed** (+392 additions, -62 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/lan/chunks.rs` (+308 -62) 📝 `crates/horizon-protocol/src/chunk.rs` (+84 -0) </details> ### 📄 Description This fixes most of the gap at 10 to 100 players between Horizon's dedicated server and vanilla's. Chunk packets took nearly all of the tick: each send re-encoded the column for that one player. **The profile** macOS `sample` on the server with 50 walking bots: about 85 % of the main thread went to `ChunkEncoder::encode_in`. The parts were: - `Paletted::from_values`: linear palette searches for every block; - `unpack_u16` over all 9 × 24 sections around the column, to find which light layers exist; - light layers packed a nibble at a time; - heightmaps primed again. Walking players re-receive the chunks they leave and come back to, about 10 chunks per second per player on both servers. So at 50 bots, Horizon encoded about 450 columns a second and could not keep up. It ran 280 ms ticks where vanilla ran 33 ms on the benchmark ThinkPad (the benchmark PR has the table). **The change** - **Section cache.** `ChunkEncoder` keeps what each resident section gives the wire, by the section's address: its bytes, whether it holds block entity blocks, and whether it is all air. - Sections are immutable behind `Arc`, and every edit goes through `Arc::make_mut`. With a `Weak` held, `make_mut` moves the edited section to a new address and keeps the old one from being reused. So the same address means the same, unchanged section. - Dead entries are swept when the map grows. - **Heightmap cache.** Heightmaps are kept per column the same way. - **Unchanged.** Block entity tags are still read for every packet, and the light layers are still built per packet, now in one pass. - **Smaller fixes, same output:** - `Paletted::from_values` uses an index table (same palette order and bits); - the all-air check looks at the palette first and stops at the first block; - uniform sections skip unpacking. **Measured** with `tick query` on a busy Mac mini, 50 walking bots: - about 400 ms ticks with the faster palette and air checks alone; - 14 ms with the caches. The ThinkPad benchmark is being rerun with this change. **Checks** - New tests: - `from_values` matches the old linear search; - cached sections follow edits; - light layers pack the same. - LAN tests and `lan_bot` pass; clippy passes. - `tools/parity.py dedicated_server` passes. It compares the chunk bytes with vanilla's server: 541 transcript lines identical. 🤖 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:10 +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#2677
No description provided.