[PR #657] [MERGED] Build a saved chunk's structure starts when it loads, not on the game thread (#489) #2704

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

📋 Pull Request Information

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

Base: main ← Head: fix/structure-starts-on-load


📝 Commits (1)

  • 75ee9a3 Build a saved chunk's structure starts when it loads, not on the game thread

📊 Changes

4 files changed (+26 additions, -3 deletions)

View changed files

📝 crates/horizon-client/src/save.rs (+3 -0)
📝 crates/horizon-client/src/streaming.rs (+7 -1)
📝 crates/horizon-client/src/vanillagen.rs (+13 -0)
📝 crates/horizon-world/src/worldgen/generator_structures.rs (+3 -2)

📄 Description

What

A saved world has none of its structure starts in the generator's cache (Horizon rebuilds them from the seed instead of reading them from the chunk). The first time natural spawning asked which structures hold a position in a chunk that references a village, the game thread built the village start (jigsaw assembly): 60 to 120 ms in a single tick, once on the first tick after the world opened and again a few ticks later for the next start. That is the "~130 ms spike a few ticks after the first" of #489, and returning from the Nether builds a fresh generator and pays it again.

Vanilla reads a chunk's structure starts with the chunk. Horizon now does the equivalent: the streaming worker that loads a saved chunk (Job::Generate → ChunkGenerator::load) builds the starts the chunk references (structure_refs) into the generator's cache (ChunkGenerator::prepare_loaded, implemented by VanillaTerrain). When the game thread asks, they are there.

The cache also keeps the first starts built for a chunk when two threads build them at once (it kept the last), so every user shares one StructureStart and the placement state on its pieces (a temple's placed chests, a mineshaft's spider, a shipwreck's height), as vanilla's single object does.

Same results

The same function builds the starts from the same seed and chunk position, only earlier and on a worker; nothing reads them differently. The cache is never cleared at run time (only by a chunk_dump example, and past 65,536 entries).

Results

First ticks after loading the saved busy world (below), base/new/base/new, thread CPU time:

Same world and settings main this PR main this PR
Slowest tick in the first 300 ticks 192 ms 104 ms 179 ms 131 ms
Second slowest 127 ms 24 ms 123 ms 42 ms
Structure lookups for spawning in those ticks (structures_at) 116 + 84 ms 0 112 + 63 ms 0
Tick CPU time after warm-up, mean / p99 8.08 / 13.8 ms 8.47 / 17.2 ms 8.33 / 13.8 ms 8.92 / 13.7 ms

The spike a few ticks in is gone, and the first tick loses its 60–70 ms of start building. The steady ticks run no new code (the player doesn't move, so no chunk loads during the bench); their means differ by the run-to-run spread of this busy, shared machine, where identical builds measured 6.0 to 9.5 ms in other runs.

What remains of the first tick (100–130 ms): about 35–45 ms of mob ticks and ~60 ms in Game::tick outside tick_world (the player's tick path), not attributed yet.

How it was measured

  • Machine: Mac mini M4 Pro (14 cores, Metal), shared with other agents' builds and harnesses (load average 60–110 during these runs), so absolute times swing from run to run: compare columns of the same table. Tick times are the integrated server tick in thread CPU time (a local, uncommitted probe times Game::tick with CLOCK_THREAD_CPUTIME_ID, so time spent descheduled doesn't count; the same probe sums the existing spans per tick).
  • World: seed 1234, creative, next to the village of the village benchmark scene (-960, 66, 176), night (time set 18000, advance_time false), a fenced pen with 400 summoned mobs (80 villagers, 80 cows, 80 sheep, 60 zombies, 60 chickens, 40 pigs), about 510 mobs in all. Saved once and restored before every run. Render distance 12, offscreen 1280×720, fast profile (release without LTO).
  • Run: load, wait until no chunk is pending, 300 ticks of warm-up, then bench 40 (about 800 ticks). Builds interleaved base/new.

Self-check

Run on one integration commit holding this PR and the four other server-tick PRs of this round (structure starts, collision scans, entity portal checks, sculk scan, pathfinder caches), which touch separate code:

  • cargo fmt --all --check: clean.
  • cargo clippy --workspace --all-targets -- -D warnings: clean.
  • cargo test -p horizon-client --lib: 787 passed, 0 failed (10 ignored). cargo test -p horizon-world -p horizon-core: all passed.
  • Local parity, tools/parity.py --changed origin/main on the integration commit (all five PRs together, 309 harnesses depend on the changed files): 194 passed, 0 failed; the other 115 did not run because the run hit the 2 h background limit. The passes are in the shared pass cache, so a rerun of the same tree only runs the remaining 115.

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/657 **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:** `fix/structure-starts-on-load` --- ### 📝 Commits (1) - [`75ee9a3`](https://github.com/horizon-rust/horizon/commit/75ee9a3299cb76a80325f0481690ef48eec93ccd) Build a saved chunk's structure starts when it loads, not on the game thread ### 📊 Changes **4 files changed** (+26 additions, -3 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/save.rs` (+3 -0) 📝 `crates/horizon-client/src/streaming.rs` (+7 -1) 📝 `crates/horizon-client/src/vanillagen.rs` (+13 -0) 📝 `crates/horizon-world/src/worldgen/generator_structures.rs` (+3 -2) </details> ### 📄 Description ## What A saved world has none of its structure starts in the generator's cache (Horizon rebuilds them from the seed instead of reading them from the chunk). The first time natural spawning asked which structures hold a position in a chunk that references a village, the game thread built the village start (jigsaw assembly): **60 to 120 ms in a single tick**, once on the first tick after the world opened and again a few ticks later for the next start. That is the "~130 ms spike a few ticks after the first" of #489, and returning from the Nether builds a fresh generator and pays it again. Vanilla reads a chunk's structure starts with the chunk. Horizon now does the equivalent: the streaming worker that loads a saved chunk (`Job::Generate` → `ChunkGenerator::load`) builds the starts the chunk references (`structure_refs`) into the generator's cache (`ChunkGenerator::prepare_loaded`, implemented by `VanillaTerrain`). When the game thread asks, they are there. The cache also keeps the **first** starts built for a chunk when two threads build them at once (it kept the last), so every user shares one `StructureStart` and the placement state on its pieces (a temple's placed chests, a mineshaft's spider, a shipwreck's height), as vanilla's single object does. ## Same results The same function builds the starts from the same seed and chunk position, only earlier and on a worker; nothing reads them differently. The cache is never cleared at run time (only by a `chunk_dump` example, and past 65,536 entries). ## Results First ticks after loading the saved busy world (below), base/new/base/new, thread CPU time: | Same world and settings | `main` | this PR | `main` | this PR | | --- | ---: | ---: | ---: | ---: | | Slowest tick in the first 300 ticks | 192 ms | **104 ms** | 179 ms | **131 ms** | | Second slowest | 127 ms | **24 ms** | 123 ms | **42 ms** | | Structure lookups for spawning in those ticks (`structures_at`) | 116 + 84 ms | 0 | 112 + 63 ms | 0 | | Tick CPU time after warm-up, mean / p99 | 8.08 / 13.8 ms | 8.47 / 17.2 ms | 8.33 / 13.8 ms | 8.92 / 13.7 ms | The spike a few ticks in is gone, and the first tick loses its 60–70 ms of start building. The steady ticks run no new code (the player doesn't move, so no chunk loads during the bench); their means differ by the run-to-run spread of this busy, shared machine, where identical builds measured 6.0 to 9.5 ms in other runs. What remains of the first tick (100–130 ms): about 35–45 ms of mob ticks and ~60 ms in `Game::tick` outside `tick_world` (the player's tick path), not attributed yet. ### How it was measured - **Machine:** Mac mini M4 Pro (14 cores, Metal), shared with other agents' builds and harnesses (load average 60–110 during these runs), so absolute times swing from run to run: compare columns of the same table. Tick times are the integrated server tick in **thread CPU time** (a local, uncommitted probe times `Game::tick` with `CLOCK_THREAD_CPUTIME_ID`, so time spent descheduled doesn't count; the same probe sums the existing `spans` per tick). - **World:** seed 1234, creative, next to the village of the `village` benchmark scene (-960, 66, 176), night (`time set 18000`, `advance_time false`), a fenced pen with 400 summoned mobs (80 villagers, 80 cows, 80 sheep, 60 zombies, 60 chickens, 40 pigs), about 510 mobs in all. Saved once and restored before every run. Render distance 12, offscreen 1280×720, `fast` profile (release without LTO). - **Run:** load, wait until no chunk is pending, 300 ticks of warm-up, then `bench 40` (about 800 ticks). Builds interleaved base/new. ### Self-check Run on one integration commit holding this PR and the four other server-tick PRs of this round (structure starts, collision scans, entity portal checks, sculk scan, pathfinder caches), which touch separate code: - `cargo fmt --all --check`: clean. - `cargo clippy --workspace --all-targets -- -D warnings`: clean. - `cargo test -p horizon-client --lib`: 787 passed, 0 failed (10 ignored). `cargo test -p horizon-world -p horizon-core`: all passed. - Local parity, `tools/parity.py --changed origin/main` on the integration commit (all five PRs together, 309 harnesses depend on the changed files): **194 passed, 0 failed**; the other 115 did not run because the run hit the 2 h background limit. The passes are in the shared pass cache, so a rerun of the same tree only runs the remaining 115. 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:58:23 +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#2704
No description provided.