[PR #860] [MERGED] Region writer: encode and compress chunks on helper threads (same bytes) #2865

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

📋 Pull Request Information

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

Base: main ← Head: perf/save-compress-helpers


📝 Commits (2)

  • d1db8c4 Region writer: encode and compress a batch's chunks on helper threads
  • 0bc5f42 save_bench: time the region writer on a saved world's chunks

📊 Changes

2 files changed (+226 additions, -10 deletions)

View changed files

➕ crates/horizon-client/examples/save_bench.rs (+128 -0)
📝 crates/horizon-client/src/save.rs (+98 -10)

📄 Description

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

What

The region writer thread encoded each chunk (NBT tree, paletted sections) and zlib-compressed it, one chunk after another, before writing the batch. A batch of 8 or more chunks is now shared out to scoped helper threads, at most half the hardware threads. Each helper takes the next chunk when it finishes one. The results go back into submission order and are written exactly as before.

  • The files are byte-identical. A chunk's compressed bytes depend only on that chunk. The writes keep the same order, so RegionBitmap-style sector allocation lands every chunk in the same place.
  • Vanilla's IOWorker compresses one chunk at a time on its IO thread. This does the same work, spread across helpers.
  • Batches under 8 chunks stay on the writer thread, because a helper thread costs more to start than it saves there.
  • examples/save_bench.rs (dev-only) reads every chunk of a saved dimension. It times the one-thread encode and compress against SaveDir::save_later + flush, and checks each written chunk byte for byte against the one-thread records.

Why

perf-ticks-8 profiled the game while exploring (macOS sample, Mac mini M4 Pro):

  • The writer thread was CPU-bound on encoding (about 55% of its busy samples, a third of that in palette_ids) and on zlib (about 35%).
  • Leaving a world waits for the writer (SaveDir::flush in save_level). On the server thread, stop_integrated sat in that flush for 3.3 s of a 4.3 s save.

Measured (Mac mini M4 Pro, 14 threads, shared with other sessions, load average 70-120)

save_bench on a world saved by the game. Seed 1234, RD16, 60 s flight (3,433 chunks, 21 MB compressed). cargo run --release -p horizon-client --example save_bench -- .local/horizon-client/26.3 saves/W/dimensions/minecraft/overworld --rounds 7:

wall, median of 7
one thread: encode + compress (the writer before this PR) 8,394 ms (2.44 ms per chunk)
SaveDir with helpers: encode + compress + write + flush 1,422 ms (0.41 ms per chunk)

The bench checks every written chunk: "21016 KiB compressed, identical to the one-thread records".

Quitting the world after the same flight. RD16, seed 1234, a 1,200-block flight at night, then horizon-ctl quit, timed until the process exits. Runs alternate between builds:

main this PR alone this PR + #859
5.95, 5.05, 1.48, 3.55 s 1.65, 0.97 s 1.91, 2.24, 1.18, 1.52 s

The quit time depends on how far behind the writer was, and that depends on the machine's load. The bench above is the cleaner number. Frame times during the flights are within the noise of the shared machine. Total CPU is unchanged: the same work runs on more threads.

Checks

  • cargo fmt --all --check and cargo clippy --workspace --all-targets -- -D warnings: clean (on the integration of this PR with #859, on main 256f7c20).
  • New test records_compressed_on_helpers_match_one_thread_in_order: 53 records, deletions included, on the helper path, compared byte for byte with one-thread ChunkRecord::from_nbt.
  • cargo test -p horizon-client --lib: 868 passed (on the integration).
  • Parity, on the integration of this PR with #859: 17 of 17 pass: paletted_container, bulk_section, chunk_generation, chunk_stage, save_roundtrip, world_reopen, disk, world, server_world_gen, datafix ×6, lan, dedicated_server. save_roundtrip has a vanilla server load the chunks Horizon rewrote.
  • --changed selects nearly every harness because they all link horizon-client. CI parity covers the rest.

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/860 **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/save-compress-helpers` --- ### 📝 Commits (2) - [`d1db8c4`](https://github.com/horizon-rust/horizon/commit/d1db8c4a54635193b77a3600fdd019af905a54fd) Region writer: encode and compress a batch's chunks on helper threads - [`0bc5f42`](https://github.com/horizon-rust/horizon/commit/0bc5f4263c68cd76a4d9df01550a446fe8923067) save_bench: time the region writer on a saved world's chunks ### 📊 Changes **2 files changed** (+226 additions, -10 deletions) <details> <summary>View changed files</summary> ➕ `crates/horizon-client/examples/save_bench.rs` (+128 -0) 📝 `crates/horizon-client/src/save.rs` (+98 -10) </details> ### 📄 Description > 🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself. ## What The region writer thread encoded each chunk (NBT tree, paletted sections) and zlib-compressed it, one chunk after another, before writing the batch. A batch of 8 or more chunks is now shared out to scoped helper threads, at most half the hardware threads. Each helper takes the next chunk when it finishes one. The results go back into submission order and are written exactly as before. - **The files are byte-identical.** A chunk's compressed bytes depend only on that chunk. The writes keep the same order, so `RegionBitmap`-style sector allocation lands every chunk in the same place. - Vanilla's `IOWorker` compresses one chunk at a time on its IO thread. This does the same work, spread across helpers. - Batches under 8 chunks stay on the writer thread, because a helper thread costs more to start than it saves there. - `examples/save_bench.rs` (dev-only) reads every chunk of a saved dimension. It times the one-thread encode and compress against `SaveDir::save_later` + `flush`, and checks each written chunk byte for byte against the one-thread records. ## Why perf-ticks-8 profiled the game while exploring (macOS `sample`, Mac mini M4 Pro): - The writer thread was CPU-bound on encoding (about 55% of its busy samples, a third of that in `palette_ids`) and on zlib (about 35%). - Leaving a world waits for the writer (`SaveDir::flush` in `save_level`). On the server thread, `stop_integrated` sat in that flush for 3.3 s of a 4.3 s save. ## Measured (Mac mini M4 Pro, 14 threads, shared with other sessions, load average 70-120) **`save_bench` on a world saved by the game.** Seed 1234, RD16, 60 s flight (3,433 chunks, 21 MB compressed). `cargo run --release -p horizon-client --example save_bench -- .local/horizon-client/26.3 saves/W/dimensions/minecraft/overworld --rounds 7`: | | wall, median of 7 | |---|---| | one thread: encode + compress (the writer before this PR) | 8,394 ms (2.44 ms per chunk) | | **`SaveDir` with helpers: encode + compress + write + flush** | **1,422 ms (0.41 ms per chunk)** | The bench checks every written chunk: "21016 KiB compressed, identical to the one-thread records". **Quitting the world after the same flight.** RD16, seed 1234, a 1,200-block flight at night, then `horizon-ctl quit`, timed until the process exits. Runs alternate between builds: | main | this PR alone | this PR + #859 | |---|---|---| | 5.95, 5.05, 1.48, 3.55 s | 1.65, 0.97 s | 1.91, 2.24, 1.18, 1.52 s | The quit time depends on how far behind the writer was, and that depends on the machine's load. The bench above is the cleaner number. Frame times during the flights are within the noise of the shared machine. Total CPU is unchanged: the same work runs on more threads. ## Checks - `cargo fmt --all --check` and `cargo clippy --workspace --all-targets -- -D warnings`: clean (on the integration of this PR with #859, on main 256f7c20). - New test `records_compressed_on_helpers_match_one_thread_in_order`: 53 records, deletions included, on the helper path, compared byte for byte with one-thread `ChunkRecord::from_nbt`. - `cargo test -p horizon-client --lib`: 868 passed (on the integration). - Parity, on the integration of this PR with #859: 17 of 17 pass: `paletted_container`, `bulk_section`, `chunk_generation`, `chunk_stage`, `save_roundtrip`, `world_reopen`, `disk`, `world`, `server_world_gen`, `datafix` ×6, `lan`, `dedicated_server`. `save_roundtrip` has a vanilla server load the chunks Horizon rewrote. - `--changed` selects nearly every harness because they all link horizon-client. CI parity covers the rest. ## 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 15:59: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#2865
No description provided.