[PR #872] [MERGED] Structure starts: a sharded cache, so the server thread stops waiting on workers #2874

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

📋 Pull Request Information

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

Base: main ← Head: perf/structure-start-shards


📝 Commits (1)

  • 43223c3 Structure starts: a sharded cache, so the server thread stops waiting on workers

📊 Changes

1 file changed (+79 additions, -13 deletions)

View changed files

📝 crates/horizon-world/src/worldgen/generator_structures.rs (+79 -13)

📄 Description

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

What

The generator's structure-start cache (StructureState.starts) was one Mutex<HashMap>. It is now 64 RwLock<HashMap> shards keyed by a hash of the chunk position, and a lookup read-locks one shard.

  • Each generated chunk reads the 289 start chunks around it for its references (createReferences).
  • The server thread reads the starts of the chunks its mobs and spawn attempts are in (structures_reaching, through VanillaTerrain::starts_at).

With a single lock, the server thread kept waiting on the workers. Vanilla keeps the starts on each chunk (StructureManager.getStartForStructure returns chunk.getStartForStructure(structure)), so its lookups share no lock either.

Unchanged:

  • The first starts built for a chunk are still the ones everyone keeps (insert_first), so placement state on their pieces stays shared.
  • forget_starts clears every shard.
  • The 65,536-entry limit applies per shard, at 1,024 entries each.

Why

perf-ticks-8 counted about 180 game-thread profile samples waiting on this lock while workers generated (brief perf-ticks-9, task 2).

Measured

Mac mini M4 Pro, shared and busy (load average 100–130). Fast profile with line tables. Each run starts a fresh world (seed 1234, creative, render distance 12), then horizon-ctl fly -500 140 60 2500 140 60 60. During the flight, macOS sample records 40 s of every thread. The runs alternated base, new, base, new. Base is main e508a5ce.

Game thread (inclusive samples, 1 ms each):

run game thread samples tick structures_reaching of which lock waits
base 1 22,351 1,508 260 194
new 1 22,701 528 15 1
base 2 24,213 866 127 57
new 2 24,997 739 50 0

The rest of structures_reaching in the new runs is starts built on a cache miss, not waiting. Worker threads' lock waits under starts_at went from 1,456 and 1,924 samples to 1,235 and 674. Most of what remains there is the ring searches' condvar and template caches, not this map. Frame rate during the flight is unchanged within noise: 20.6 / 19.3 fps base, 21.2 / 16.4 fps new, on a machine this loaded.

Checks

  • cargo fmt --all --check and cargo clippy --workspace --all-targets -- -D warnings: clean.
  • cargo test -p horizon-world --lib: 103 passed. That includes the new start_cache_keeps_first_and_spreads_neighbours: first starts kept, a chunk's 17×17 reference window spread over the shards (each shard gets 1–19 of the 289), and clear.
  • Parity, local (on e7201fa8, the same change before the rebase onto 4f9df738): structure_density, worldgen, chunk_stage and chunk_generation: 4 of 4 pass.
  • CI parity covers the rest of --changed.

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/872 **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/structure-start-shards` --- ### 📝 Commits (1) - [`43223c3`](https://github.com/horizon-rust/horizon/commit/43223c34e208e546a47395a2b85b09126d80d681) Structure starts: a sharded cache, so the server thread stops waiting on workers ### 📊 Changes **1 file changed** (+79 additions, -13 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-world/src/worldgen/generator_structures.rs` (+79 -13) </details> ### 📄 Description > 🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself. ## What The generator's structure-start cache (`StructureState.starts`) was one `Mutex<HashMap>`. It is now 64 `RwLock<HashMap>` shards keyed by a hash of the chunk position, and a lookup read-locks one shard. - Each generated chunk reads the 289 start chunks around it for its references (`createReferences`). - The server thread reads the starts of the chunks its mobs and spawn attempts are in (`structures_reaching`, through `VanillaTerrain::starts_at`). With a single lock, the server thread kept waiting on the workers. Vanilla keeps the starts on each chunk (`StructureManager.getStartForStructure` returns `chunk.getStartForStructure(structure)`), so its lookups share no lock either. Unchanged: - The first starts built for a chunk are still the ones everyone keeps (`insert_first`), so placement state on their pieces stays shared. - `forget_starts` clears every shard. - The 65,536-entry limit applies per shard, at 1,024 entries each. ## Why perf-ticks-8 counted about 180 game-thread profile samples waiting on this lock while workers generated (brief perf-ticks-9, task 2). ## Measured Mac mini M4 Pro, shared and busy (load average 100–130). Fast profile with line tables. Each run starts a fresh world (seed 1234, creative, render distance 12), then `horizon-ctl fly -500 140 60 2500 140 60 60`. During the flight, macOS `sample` records 40 s of every thread. The runs alternated base, new, base, new. Base is main e508a5ce. Game thread (inclusive samples, 1 ms each): | run | game thread samples | `tick` | `structures_reaching` | of which lock waits | |---|---|---|---|---| | base 1 | 22,351 | 1,508 | 260 | **194** | | new 1 | 22,701 | 528 | 15 | **1** | | base 2 | 24,213 | 866 | 127 | **57** | | new 2 | 24,997 | 739 | 50 | **0** | The rest of `structures_reaching` in the new runs is starts built on a cache miss, not waiting. Worker threads' lock waits under `starts_at` went from 1,456 and 1,924 samples to 1,235 and 674. Most of what remains there is the ring searches' condvar and template caches, not this map. Frame rate during the flight is unchanged within noise: 20.6 / 19.3 fps base, 21.2 / 16.4 fps new, on a machine this loaded. ## Checks - `cargo fmt --all --check` and `cargo clippy --workspace --all-targets -- -D warnings`: clean. - `cargo test -p horizon-world --lib`: 103 passed. That includes the new `start_cache_keeps_first_and_spreads_neighbours`: first starts kept, a chunk's 17×17 reference window spread over the shards (each shard gets 1–19 of the 289), and clear. - Parity, local (on e7201fa8, the same change before the rebase onto 4f9df738): `structure_density`, `worldgen`, `chunk_stage` and `chunk_generation`: 4 of 4 pass. - CI parity covers the rest of `--changed`. ## 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:46 +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#2874
No description provided.