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 <noreply@anthropic.com>
This commit is contained in:
@@ -2784,6 +2784,9 @@ impl<G: ChunkGenerator> ChunkGenerator for Persistent<G> {
|
||||
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
|
||||
|
||||
@@ -36,6 +36,9 @@ pub trait ChunkGenerator: Send + Sync + 'static {
|
||||
fn load(&self, _x: i32, _z: i32) -> Option<Chunk> {
|
||||
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);
|
||||
|
||||
@@ -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))),
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user