[PR #827] [MERGED] Region files: write each saved chunk in place, as vanilla's RegionFile #2837

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

📋 Pull Request Information

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

Base: main ← Head: perf/region-in-place


📝 Commits (1)

  • 2932704 Region files: write each saved chunk in place, as vanilla's RegionFile

📊 Changes

3 files changed (+357 additions, -89 deletions)

View changed files

📝 crates/horizon-client/src/save.rs (+79 -84)
📝 crates/horizon-world/src/region.rs (+160 -5)
📝 crates/horizon-world/tests/world_io.rs (+118 -0)

📄 Description

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

What

Saving a chunk no longer rewrites its whole region file.

Before: for each region a batch touched, the writer thread did the following:

  • read every chunk the file held;
  • wrote them all, with the new ones, to a new file in .staging;
  • flushed that file and renamed it over the old one.

A full region is about 1024 chunks and several MB. Saving one chunk of it cost all of that.

Now: it writes the way vanilla's RegionFile.write and clear do.

  • The saved chunk goes to the first free run of sectors that holds it (RegionBitmap.allocate).
  • An oversized chunk (256 sectors or more) goes to its c.X.Z.mcc file, with a one-sector stub in the region.
  • The header entry then points to the chunk, and only after that are its old sectors freed.
  • The other chunks are not touched. Each region is flushed once per batch.
  • A cleared chunk keeps the file, as in vanilla (it was deleted when its last chunk went).

Other changes:

  • One open file per region. Readers and the writer share it through the region cache, behind one lock. A read never sees a header older than the sectors it points to.
  • Compression before the lock. Chunks are compressed before their region is locked, so readers don't wait on compression.
  • A chunk queued again is written once. If a chunk was queued again before the writer reached it, the older batch skips it and only the newest is written (IOWorker.pendingWrites).
  • horizon_world::region::RegionFile gains open_writable, write, clear and sync. RegionWriter (export to a new file) is unchanged.

The files stay vanilla's format. Chunks are placed where vanilla's first-fit bitmap places them, and each chunk's last sector is zero-padded.

Measured (Mac mini M4 Pro, shared with other sessions)

Saving into a full region. cargo test --release -p horizon-world --test world_io benchmark -- --ignored --nocapture:

  • region of 1024 chunks of 6000 bytes (two sectors each, about a compressed overworld chunk);
  • 100 saves of one chunk, each flushed to disk;
  • results:
per save
whole region rewritten (main's way) 80.3 ms
in place 7.1 ms (mostly the flush)

Travel. The same travel as #824: server thread, RD 12, seed 1234, 450 blocks in 15 s, macOS sample.

  • The writer thread stays saturated both ways.
  • Its time is mostly compressing chunks: zlib at vanilla's level, 3.5k to 5.9k samples of ChunkRecord::from_nbt per run. That work is the same as vanilla's, and this PR doesn't change it.
  • The rest of write_region_records, which is the reading and rewriting of other chunks, went from 992 / 879 / 401 samples (main) to 510 / 315 (this).
  • The gain grows as regions fill up. A fresh world's regions are nearly empty during a 15 s run.

Checks

  • cargo fmt --all --check and cargo clippy --workspace --all-targets -- -D warnings: clean.
  • New test in_place_writes_reuse_freed_sectors_and_keep_other_chunks. It covers first-fit placement, a chunk that grows and moves, freed sectors being reused, an oversized chunk going to its .mcc file and back inline, clearing, and reopening.
  • cargo test -p horizon-world: all pass. cargo test -p horizon-client --lib: 861 passed (on the integration below).
  • Parity, on an integration of this PR with #824, #828 and #829 on main 5b62d771: the 19 harnesses that cover these changes pass (light, lan, dedicated_server, save_roundtrip, world_reopen, disk, datafix ×6, world, server_world_gen, block_entity_tickers, block_entity_anim, block_entity_render, block_entity_storage, chunk_stage). --changed selects about 300 harnesses because nearly all of them link horizon-client; the first 29 of those passed too before I stopped the run on the busy Mac mini, and CI parity covers the rest.
    • save_roundtrip has Horizon rewrite every chunk of a vanilla world in place. The vanilla server then loads the result: no errors, no upgrades, and the same data get output.

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/827 **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/region-in-place` --- ### 📝 Commits (1) - [`2932704`](https://github.com/horizon-rust/horizon/commit/29327041567b12ea441352d6803232e181331090) Region files: write each saved chunk in place, as vanilla's RegionFile ### 📊 Changes **3 files changed** (+357 additions, -89 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/save.rs` (+79 -84) 📝 `crates/horizon-world/src/region.rs` (+160 -5) 📝 `crates/horizon-world/tests/world_io.rs` (+118 -0) </details> ### 📄 Description > 🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself. ## What Saving a chunk no longer rewrites its whole region file. **Before:** for each region a batch touched, the writer thread did the following: - read every chunk the file held; - wrote them all, with the new ones, to a new file in `.staging`; - flushed that file and renamed it over the old one. A full region is about 1024 chunks and several MB. Saving one chunk of it cost all of that. **Now:** it writes the way vanilla's `RegionFile.write` and `clear` do. - The saved chunk goes to the first free run of sectors that holds it (`RegionBitmap.allocate`). - An oversized chunk (256 sectors or more) goes to its `c.X.Z.mcc` file, with a one-sector stub in the region. - The header entry then points to the chunk, and only after that are its old sectors freed. - The other chunks are not touched. Each region is flushed once per batch. - A cleared chunk keeps the file, as in vanilla (it was deleted when its last chunk went). Other changes: - **One open file per region.** Readers and the writer share it through the region cache, behind one lock. A read never sees a header older than the sectors it points to. - **Compression before the lock.** Chunks are compressed before their region is locked, so readers don't wait on compression. - **A chunk queued again is written once.** If a chunk was queued again before the writer reached it, the older batch skips it and only the newest is written (`IOWorker.pendingWrites`). - **`horizon_world::region::RegionFile`** gains `open_writable`, `write`, `clear` and `sync`. `RegionWriter` (export to a new file) is unchanged. The files stay vanilla's format. Chunks are placed where vanilla's first-fit bitmap places them, and each chunk's last sector is zero-padded. ## Measured (Mac mini M4 Pro, shared with other sessions) **Saving into a full region.** `cargo test --release -p horizon-world --test world_io benchmark -- --ignored --nocapture`: - region of 1024 chunks of 6000 bytes (two sectors each, about a compressed overworld chunk); - 100 saves of one chunk, each flushed to disk; - results: | | per save | |---|---| | whole region rewritten (main's way) | 80.3 ms | | **in place** | **7.1 ms** (mostly the flush) | **Travel.** The same travel as #824: server thread, RD 12, seed 1234, 450 blocks in 15 s, macOS `sample`. - The writer thread stays saturated both ways. - Its time is mostly compressing chunks: zlib at vanilla's level, 3.5k to 5.9k samples of `ChunkRecord::from_nbt` per run. That work is the same as vanilla's, and this PR doesn't change it. - The rest of `write_region_records`, which is the reading and rewriting of other chunks, went from 992 / 879 / 401 samples (main) to 510 / 315 (this). - The gain grows as regions fill up. A fresh world's regions are nearly empty during a 15 s run. ## Checks - `cargo fmt --all --check` and `cargo clippy --workspace --all-targets -- -D warnings`: clean. - New test `in_place_writes_reuse_freed_sectors_and_keep_other_chunks`. It covers first-fit placement, a chunk that grows and moves, freed sectors being reused, an oversized chunk going to its `.mcc` file and back inline, clearing, and reopening. - `cargo test -p horizon-world`: all pass. `cargo test -p horizon-client --lib`: 861 passed (on the integration below). - Parity, on an integration of this PR with #824, #828 and #829 on main 5b62d771: the 19 harnesses that cover these changes pass (`light`, `lan`, `dedicated_server`, `save_roundtrip`, `world_reopen`, `disk`, `datafix` ×6, `world`, `server_world_gen`, `block_entity_tickers`, `block_entity_anim`, `block_entity_render`, `block_entity_storage`, `chunk_stage`). `--changed` selects about 300 harnesses because nearly all of them link horizon-client; the first 29 of those passed too before I stopped the run on the busy Mac mini, and CI parity covers the rest. - `save_roundtrip` has Horizon rewrite every chunk of a vanilla world in place. The vanilla server then loads the result: no errors, no upgrades, and the same `data get` output. ## 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:28 +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#2837
No description provided.