[PR #300] [MERGED] Autosave stops waiting for entity writes: no more 2 s freeze (#224) #2469

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

📋 Pull Request Information

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

Base: main ← Head: fix/autosave-spike


📝 Commits (1)

  • b18261c Autosave doesn't wait for the entity writes

📊 Changes

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

View changed files

📝 crates/horizon-client/src/game.rs (+1 -1)
📝 crates/horizon-client/src/game/persist.rs (+9 -2)

📄 Description

Part of #224 (the deterministic autosave freeze; one remaining hitch while exploring is described below and left open on the issue).

What

The 5-minute autosave no longer freezes the game until the region files are written.

Why

#284 made the autosave queue its chunks on the region writer instead of waiting. Right after that, though, save_game_with saved entities with SaveDir::save_entities, which waits. The region writer is one FIFO thread, so waiting for the entity batch meant waiting for every chunk queued before it. Vanilla's autosave (ServerLevel.save with flush = false) calls PersistentEntitySectionManager.autoSave(): it stores the entity sections and never flushes. Only saveAll() (save-all, quit) waits for the IO worker.

How

Game::save_entities(wait): the autosave (save_game_with(false)) queues the entities with save_entities_later. The explicit save, save-all, quitting and dimension changes still wait (save_game). Loads still see the queued entities before they reach disk (SaveDir pending writes), as they already do for chunks.

Measurements (Mac mini M4 Pro, render distance 16, about 900 loaded columns)

Same world (seed 1234, creative, camera held still 30 blocks above spawn, frames recorded with horizon-ctl fly … raw). Offscreen, because the session was locked, and on a busy machine (other sessions building), so the steady p99 is 45–140 ms.

autosave window: worst frame other windows: worst frame
main 1860 ms (CPU 1854 ms) 44–115 ms
this branch 137 ms 107–195 ms

With temporary per-step timing in save_game_with (not in this PR), the autosave's save_entities took 150–290 ms on main (up to the full 1.86 s when a large chunk batch was queued ahead of it) and 0.9–3.5 ms here. Everything else in the autosave takes under 5 ms. The explicit save (horizon-ctl save) waits by design and gives 180–255 ms frames on both.

Still open on #224: if decoration wrote proto chunks since the last save (while exploring), Streaming::save converts them for saving on the main thread (VanillaTerrain::saved_proto → column(), a full block-by-block copy of each proto). Teleporting into new terrain just before the autosave: 296 protos took 172–637 ms. Moving the NBT encoding of those protos to the writer thread didn't change that time (the copy is the cost, not the encoding), so it isn't in this PR. The fix is a cheaper copy (whole sections at once, like vanilla's SerializableChunkData.copyOf) or doing the copy off the main thread.

How tested

cargo fmt --all -- --check                                   OK
cargo clippy --workspace --all-targets -- -D warnings        OK
cargo test -p horizon-client --lib                           627 passed
cargo test -p horizon-client --test generation_saves         2 passed
parity: save_roundtrip                                       FAIL, same as main: the only difference is the copper golem's
                                                             PersistenceRequired (#289, fixed by #292)

Nothing needs Ludovic.

🤖 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/300 **Author:** [@ludovic111](https://github.com/ludovic111) **Created:** 10/15/2026 **Status:** ✅ Merged **Merged:** 10/15/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `fix/autosave-spike` --- ### 📝 Commits (1) - [`b18261c`](https://github.com/horizon-rust/horizon/commit/b18261cc799a5686349515f17ac23457fa488537) Autosave doesn't wait for the entity writes ### 📊 Changes **2 files changed** (+10 additions, -3 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/game.rs` (+1 -1) 📝 `crates/horizon-client/src/game/persist.rs` (+9 -2) </details> ### 📄 Description Part of #224 (the deterministic autosave freeze; one remaining hitch while exploring is described below and left open on the issue). ## What The 5-minute autosave no longer freezes the game until the region files are written. ## Why #284 made the autosave queue its chunks on the region writer instead of waiting. Right after that, though, `save_game_with` saved entities with `SaveDir::save_entities`, which **waits**. The region writer is one FIFO thread, so waiting for the entity batch meant waiting for every chunk queued before it. Vanilla's autosave (`ServerLevel.save` with `flush = false`) calls `PersistentEntitySectionManager.autoSave()`: it stores the entity sections and never flushes. Only `saveAll()` (save-all, quit) waits for the IO worker. ## How `Game::save_entities(wait)`: the autosave (`save_game_with(false)`) queues the entities with `save_entities_later`. The explicit save, `save-all`, quitting and dimension changes still wait (`save_game`). Loads still see the queued entities before they reach disk (`SaveDir` pending writes), as they already do for chunks. ## Measurements (Mac mini M4 Pro, render distance 16, about 900 loaded columns) Same world (seed 1234, creative, camera held still 30 blocks above spawn, frames recorded with `horizon-ctl fly … raw`). Offscreen, because the session was locked, and on a busy machine (other sessions building), so the steady p99 is 45–140 ms. | | autosave window: worst frame | other windows: worst frame | |---|---|---| | main | **1860 ms** (CPU 1854 ms) | 44–115 ms | | this branch | **137 ms** | 107–195 ms | With temporary per-step timing in `save_game_with` (not in this PR), the autosave's `save_entities` took 150–290 ms on main (up to the full 1.86 s when a large chunk batch was queued ahead of it) and 0.9–3.5 ms here. Everything else in the autosave takes under 5 ms. The explicit save (`horizon-ctl save`) waits by design and gives 180–255 ms frames on both. **Still open on #224:** if decoration wrote proto chunks since the last save (while exploring), `Streaming::save` converts them for saving on the main thread (`VanillaTerrain::saved_proto` → `column()`, a full block-by-block copy of each proto). Teleporting into new terrain just before the autosave: 296 protos took 172–637 ms. Moving the NBT encoding of those protos to the writer thread didn't change that time (the copy is the cost, not the encoding), so it isn't in this PR. The fix is a cheaper copy (whole sections at once, like vanilla's `SerializableChunkData.copyOf`) or doing the copy off the main thread. ## How tested ``` cargo fmt --all -- --check OK cargo clippy --workspace --all-targets -- -D warnings OK cargo test -p horizon-client --lib 627 passed cargo test -p horizon-client --test generation_saves 2 passed parity: save_roundtrip FAIL, same as main: the only difference is the copper golem's PersistenceRequired (#289, fixed by #292) ``` Nothing needs Ludovic. 🤖 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:56:25 +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#2469
No description provided.