From 75ee9a3299cb76a80325f0481690ef48eec93ccd Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 17 Oct 2026 05:35:24 +0200 Subject: [PATCH] Build a saved chunk's structure starts when it loads, not on the game thread A loaded world had none of its structure starts in the generator's cache. The first time natural spawning (or anything else) asked which structures hold a position in a chunk referencing a village, the game thread built the village start: 60 to 120 ms in one tick, once right after the world opened and again a few ticks later for the next start (#489). Vanilla reads the starts with the chunk. The streaming worker that loads a saved chunk now builds the starts it references into the same cache, so the game thread finds them there. The starts are the same: the same function builds them from the same seed and chunk, only earlier and on a worker. The cache now keeps the first starts built for a chunk when two threads build them at once, so every user shares one start and its pieces' placement state, as vanilla's single StructureStart does. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/save.rs | 3 +++ crates/horizon-client/src/streaming.rs | 8 +++++++- crates/horizon-client/src/vanillagen.rs | 13 +++++++++++++ .../src/worldgen/generator_structures.rs | 5 +++-- 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/crates/horizon-client/src/save.rs b/crates/horizon-client/src/save.rs index a8553e276..79e0ec396 100644 --- a/crates/horizon-client/src/save.rs +++ b/crates/horizon-client/src/save.rs @@ -2784,6 +2784,9 @@ impl ChunkGenerator for Persistent { chunk.entities = self.save.load_entities(x, z); Some(chunk) } + fn prepare_loaded(&self, worker: &mut G::Worker, chunk: &Chunk) { + self.inner.prepare_loaded(worker, chunk); + } fn proto(&self, worker: &mut G::Worker, x: i32, z: i32) -> crate::streaming::Proto { let mut proto = self.inner.proto(worker, x, z); if let Some(saved) = self diff --git a/crates/horizon-client/src/streaming.rs b/crates/horizon-client/src/streaming.rs index 994316324..1754f1550 100644 --- a/crates/horizon-client/src/streaming.rs +++ b/crates/horizon-client/src/streaming.rs @@ -36,6 +36,9 @@ pub trait ChunkGenerator: Send + Sync + 'static { fn load(&self, _x: i32, _z: i32) -> Option { None } + /// What a loaded chunk needs from the generator before the game uses it, + /// on the worker that loaded it. + fn prepare_loaded(&self, _worker: &mut Self::Worker, _chunk: &Chunk) {} fn proto(&self, worker: &mut Self::Worker, x: i32, z: i32) -> Proto; fn decorates(&self) -> bool { false @@ -369,7 +372,10 @@ impl Streaming { }; let done = match job { Job::Generate(x, z) => match generator.load(x, z) { - Some(chunk) => Done::Generated(chunk), + Some(chunk) => { + generator.prepare_loaded(&mut worker, &chunk); + Done::Generated(chunk) + } None => { let proto = generator.proto(&mut worker, x, z); let decorated = generator.is_decorated(&proto); diff --git a/crates/horizon-client/src/vanillagen.rs b/crates/horizon-client/src/vanillagen.rs index fc18e8404..6f0c4edaf 100644 --- a/crates/horizon-client/src/vanillagen.rs +++ b/crates/horizon-client/src/vanillagen.rs @@ -265,6 +265,19 @@ impl ChunkGenerator for VanillaTerrain { fn worker(&self) -> GeneratorWorker { self.generator.worker() } + /// The structure starts a saved chunk references, as vanilla reads them + /// with the chunk: built here, on the loading worker, into the + /// generator's cache, rather than on the game thread the first time + /// spawning or a mob asks which structures hold a position (a village + /// start took 70 to 110 ms there, one tick each, after a world loaded). + fn prepare_loaded(&self, worker: &mut GeneratorWorker, chunk: &Chunk) { + let mut starts: Vec<(i32, i32)> = chunk.structure_refs.iter().map(|(_, at)| *at).collect(); + starts.sort_unstable(); + starts.dedup(); + for (x, z) in starts { + worker.structure_starts(ChunkPos::new(x, z)); + } + } fn proto(&self, worker: &mut GeneratorWorker, x: i32, z: i32) -> Proto { match worker.generate_proto(ChunkPos::new(x, z)) { Ok(chunk) => Box::new(VanillaProto::Ready(Box::new(chunk))), diff --git a/crates/horizon-world/src/worldgen/generator_structures.rs b/crates/horizon-world/src/worldgen/generator_structures.rs index f5487dc33..e78ce0772 100644 --- a/crates/horizon-world/src/worldgen/generator_structures.rs +++ b/crates/horizon-world/src/worldgen/generator_structures.rs @@ -229,8 +229,9 @@ impl StructureState { if cache.len() >= START_CACHE_LIMIT { cache.clear(); } - cache.insert((chunk.x, chunk.z), starts.clone()); - starts + // Another thread may have built the same starts meanwhile: everyone + // keeps the first, so placement state on its pieces is shared. + cache.entry((chunk.x, chunk.z)).or_insert(starts).clone() } /// ChunkGenerator.findNearestMapStructure: the nearest start of one of /// `names` from block `from` (rings: every ring chunk; random spreads: