[PR #246] [MERGED] perf: group block entities once per save batch #2434

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

📋 Pull Request Information

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

Base: main ← Head: perf/save-batch-entity-grouping-20261014


📝 Commits (1)

  • 34628c1 perf: group block entities once per save batch

📊 Changes

5 files changed (+503 additions, -34 deletions)

View changed files

➕ crates/horizon-client/examples/save_batch_bench.rs (+260 -0)
📝 crates/horizon-client/src/game.rs (+18 -34)
📝 crates/horizon-client/src/save.rs (+124 -0)
➕ docs/save-batch-entities.csv (+11 -0)
➕ docs/save-batch-entities.md (+90 -0)

📄 Description

Saving several modified chunks currently rescans every loaded block entity and custom name for each chunk. Prepare the dimension-change, full-save and dirty-unload batches in one pass over each live map, grouping borrowed entries for requested chunks and cloning each chunk's inventories in sequence.

The empty-batch path skips both maps; a single chunk retains the original direct filters. Preserve arithmetic negative-coordinate mapping, input and per-map iteration order, duplicate inputs, custom names and saved NBT extras. World maps remain owned by the world. Modified-flag drains, tick/extras attachment, unload cleanup and save error handling retain their existing sequence.

Validation:

  • CARGO_INCREMENTAL=0 CARGO_TARGET_DIR=/home/bob/Documents/horizon/target cargo test --profile fast -p horizon-client --lib save::tests: 11 passed, including new coordinate-boundary, ordering, stale-content and snapshot-ownership coverage.
  • cargo check --profile fast -p horizon-client --bin horizon-client --example save_batch_bench: passed; final cargo build --profile fast -p horizon-client --example save_batch_bench and cargo clippy --profile fast -p horizon-client --all-targets -- -D warnings: passed.
  • cargo fmt --all -- --check, git diff --check, and python3 tools/check_public_tree.py: passed. Every Cargo invocation used CARGO_INCREMENTAL=0 and the shared target directory.
  • Benchmark validates exact full chunk NBT bytes for every fixture/batch, including block entities, custom names, source overlays, saved entity order and an unmodelled block entity. A shared-target stale horizon-theme artifact initially caused missing-constant errors in clippy; touching this worktree's dependency source forced its correct rebuild, and clippy passed without a content change to that dependency.

Benchmark: AMD Ryzen 5 5600X / Linux 7.2.8-zen1-2-zen / rustc 1.99.0 / optimized fast profile, LTO off, 16 codegen units, incremental off, logical CPU 2. Five warmups, medians of 21 alternating samples, adaptive repetitions, identical inputs and black_box outputs. Retained measurements ran after other Rust builds finished; a contended trial was discarded.

Fixture Saved chunks Before (ms) After (ms) Speedup
village 1 0.043732 0.043813 0.998x
village 16 0.695763 0.578064 1.204x
village 64 8.185510 7.467929 1.096x
village 256 38.434320 36.447195 1.055x
storage 1 0.172842 0.170303 1.015x
storage 16 3.688055 3.242919 1.137x
storage 64 35.799641 33.446834 1.070x
storage 256 155.262657 147.501719 1.053x

These are CPU times for the actual production grouping/clone helper, including output destruction. The synthetic village has 3,072 loaded block entities and 768 names; storage has 12,288 and 3,072. Inventories are populated and include custom item components. Single-chunk time is effectively unchanged; multi-chunk runs are 1.053–1.204x faster. Exact production NBT encoding is validated outside the timed section. Compression, disk I/O, writer queueing and Game::attach_save_data are excluded; this does not establish overall save, startup or FPS gains. Prepared production assets were not available, so fixtures use a minimal registry/biome and simplified terrain instead of captured saves.

The additional temporary memory is an O(chunk-count) index and input references, plus O(selected entities + names) borrowed pairs (16 bytes per pair on this 64-bit machine, plus vector spare capacity), and the output chunk vector before Arc wrapping. Unsaved contents are not cloned; no persistent cache is added.

Reproduce with CARGO_INCREMENTAL=0 CARGO_TARGET_DIR=/home/bob/Documents/horizon/target cargo build --profile fast -p horizon-client --example save_batch_bench, then taskset -c 2 /home/bob/Documents/horizon/target/fast/examples/save_batch_bench. Add --nbt to time the combined snapshot/serialization CPU stage as well. See docs/save-batch-entities.md, the raw CSV, and the committed example for full inputs and scope.


🔄 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/246 **Author:** [@rdbtCVS](https://github.com/rdbtCVS) **Created:** 10/15/2026 **Status:** ✅ Merged **Merged:** 10/15/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `perf/save-batch-entity-grouping-20261014` --- ### 📝 Commits (1) - [`34628c1`](https://github.com/horizon-rust/horizon/commit/34628c1471375e174d0faa97e9b2a61737ecec0c) perf: group block entities once per save batch ### 📊 Changes **5 files changed** (+503 additions, -34 deletions) <details> <summary>View changed files</summary> ➕ `crates/horizon-client/examples/save_batch_bench.rs` (+260 -0) 📝 `crates/horizon-client/src/game.rs` (+18 -34) 📝 `crates/horizon-client/src/save.rs` (+124 -0) ➕ `docs/save-batch-entities.csv` (+11 -0) ➕ `docs/save-batch-entities.md` (+90 -0) </details> ### 📄 Description Saving several modified chunks currently rescans every loaded block entity and custom name for each chunk. Prepare the dimension-change, full-save and dirty-unload batches in one pass over each live map, grouping borrowed entries for requested chunks and cloning each chunk's inventories in sequence. The empty-batch path skips both maps; a single chunk retains the original direct filters. Preserve arithmetic negative-coordinate mapping, input and per-map iteration order, duplicate inputs, custom names and saved NBT extras. World maps remain owned by the world. Modified-flag drains, tick/extras attachment, unload cleanup and save error handling retain their existing sequence. Validation: - `CARGO_INCREMENTAL=0 CARGO_TARGET_DIR=/home/bob/Documents/horizon/target cargo test --profile fast -p horizon-client --lib save::tests`: 11 passed, including new coordinate-boundary, ordering, stale-content and snapshot-ownership coverage. - `cargo check --profile fast -p horizon-client --bin horizon-client --example save_batch_bench`: passed; final `cargo build --profile fast -p horizon-client --example save_batch_bench` and `cargo clippy --profile fast -p horizon-client --all-targets -- -D warnings`: passed. - `cargo fmt --all -- --check`, `git diff --check`, and `python3 tools/check_public_tree.py`: passed. Every Cargo invocation used `CARGO_INCREMENTAL=0` and the shared target directory. - Benchmark validates exact full chunk NBT bytes for every fixture/batch, including block entities, custom names, source overlays, saved entity order and an unmodelled block entity. A shared-target stale `horizon-theme` artifact initially caused missing-constant errors in clippy; touching this worktree's dependency source forced its correct rebuild, and clippy passed without a content change to that dependency. Benchmark: AMD Ryzen 5 5600X / Linux 7.2.8-zen1-2-zen / rustc 1.99.0 / optimized `fast` profile, LTO off, 16 codegen units, incremental off, logical CPU 2. Five warmups, medians of 21 alternating samples, adaptive repetitions, identical inputs and `black_box` outputs. Retained measurements ran after other Rust builds finished; a contended trial was discarded. | Fixture | Saved chunks | Before (ms) | After (ms) | Speedup | | --- | ---: | ---: | ---: | ---: | | village | 1 | 0.043732 | 0.043813 | 0.998x | | village | 16 | 0.695763 | 0.578064 | 1.204x | | village | 64 | 8.185510 | 7.467929 | 1.096x | | village | 256 | 38.434320 | 36.447195 | 1.055x | | storage | 1 | 0.172842 | 0.170303 | 1.015x | | storage | 16 | 3.688055 | 3.242919 | 1.137x | | storage | 64 | 35.799641 | 33.446834 | 1.070x | | storage | 256 | 155.262657 | 147.501719 | 1.053x | These are CPU times for the actual production grouping/clone helper, including output destruction. The synthetic village has 3,072 loaded block entities and 768 names; storage has 12,288 and 3,072. Inventories are populated and include custom item components. Single-chunk time is effectively unchanged; multi-chunk runs are 1.053–1.204x faster. Exact production NBT encoding is validated outside the timed section. Compression, disk I/O, writer queueing and `Game::attach_save_data` are excluded; this does not establish overall save, startup or FPS gains. Prepared production assets were not available, so fixtures use a minimal registry/biome and simplified terrain instead of captured saves. The additional temporary memory is an O(chunk-count) index and input references, plus O(selected entities + names) borrowed pairs (16 bytes per pair on this 64-bit machine, plus vector spare capacity), and the output chunk vector before Arc wrapping. Unsaved contents are not cloned; no persistent cache is added. Reproduce with `CARGO_INCREMENTAL=0 CARGO_TARGET_DIR=/home/bob/Documents/horizon/target cargo build --profile fast -p horizon-client --example save_batch_bench`, then `taskset -c 2 /home/bob/Documents/horizon/target/fast/examples/save_batch_bench`. Add `--nbt` to time the combined snapshot/serialization CPU stage as well. See `docs/save-batch-entities.md`, the raw CSV, and the committed example for full inputs and scope. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 15:56:05 +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#2434
No description provided.