Reuse natural spawn candidate buffers

Every spawning pass built a new 289-per-player candidate Vec and a new
dedup set. Keep both in a scratch struct on the game and clear them between
passes, so their capacity survives. The first pass and the thunder pass use
the same buffers.

Co-Authored-By: OpenCode <noreply@opencode.ai>
This commit is contained in:
Hector Eccles
2026-10-27 21:55:47 -04:00
parent 56f968e11c
commit a48fcf0d16
3 changed files with 38 additions and 12 deletions
+3
View File
@@ -1779,6 +1779,8 @@ struct Game<'a> {
creaking: creaking::CreakingState, creaking: creaking::CreakingState,
/// Monster spawners' display mobs (`game/spawner.rs`). /// Monster spawners' display mobs (`game/spawner.rs`).
spawners: spawner::SpawnerState, spawners: spawner::SpawnerState,
/// Reused candidate buffers for natural spawning.
spawn_scratch: natural_spawning::SpawnScratch,
trident_model: Option<horizon_client::mobs::Part>, trident_model: Option<horizon_client::mobs::Part>,
arrow_model: Option<horizon_client::mobs::Part>, arrow_model: Option<horizon_client::mobs::Part>,
tnts: Vec<tnt::PrimedTnt>, tnts: Vec<tnt::PrimedTnt>,
@@ -13895,6 +13897,7 @@ impl<'a> Game<'a> {
ominous_spawners: Vec::new(), ominous_spawners: Vec::new(),
creaking: Default::default(), creaking: Default::default(),
spawners: Default::default(), spawners: Default::default(),
spawn_scratch: Default::default(),
trident_model: prepared.trident_model, trident_model: prepared.trident_model,
arrow_model: prepared.arrow_model, arrow_model: prepared.arrow_model,
tnts: Vec::new(), tnts: Vec::new(),
+7 -3
View File
@@ -29,10 +29,13 @@ impl Game<'_> {
if rain <= 0.2 || thunder <= 0.9 || self.dim != super::Dim::Overworld { if rain <= 0.2 || thunder <= 0.9 || self.dim != super::Dim::Overworld {
return; return;
} }
let (_, mut chunks) = self.spawning_chunks(&self.level_players()); let mut scratch = std::mem::take(&mut self.spawn_scratch);
self.spawning_chunks(&self.level_players(), &mut scratch);
let levels = self.tickets.levels(); let levels = self.tickets.levels();
chunks.retain(|&(x, z)| levels.random_ticking(x, z)); scratch
for (cx, cz) in chunks { .candidates
.retain(|&(x, z)| levels.random_ticking(x, z));
for (cx, cz) in scratch.candidates.drain(..) {
if self.world.random.next_int_bound(100_000).unwrap_or(1) != 0 { if self.world.random.next_int_bound(100_000).unwrap_or(1) != 0 {
continue; continue;
} }
@@ -77,6 +80,7 @@ impl Game<'_> {
]; ];
self.spawn_bolt(at, trap); self.spawn_bolt(at, trap);
} }
self.spawn_scratch = scratch;
} }
/// `ServerLevel.findLightningRod`: `PoiManager.findClosest` over the /// `ServerLevel.findLightningRod`: `PoiManager.findClosest` over the
/// lightning rod points of interest within 128 blocks of `center` whose /// lightning rod points of interest within 128 blocks of `center` whose
@@ -24,6 +24,15 @@ use horizon_core::{
use horizon_world::registry::BlockStateId; use horizon_world::registry::BlockStateId;
use std::collections::HashMap; use std::collections::HashMap;
/// Reused buffers for one pass over the spawning chunks: the candidate chunks
/// and the set that keeps each one once. The buffers keep their capacity
/// between ticks.
#[derive(Default)]
pub(super) struct SpawnScratch {
pub(super) candidates: Vec<(i32, i32)>,
pub(super) seen: horizon_client::fast_hash::FxHashSet<(i32, i32)>,
}
/// A player in the level in place, as spawning and despawning see it. /// A player in the level in place, as spawning and despawning see it.
pub(super) struct LevelPlayer { pub(super) struct LevelPlayer {
pub(super) position: Vec3, pub(super) position: Vec3,
@@ -445,9 +454,17 @@ impl<'a> Game<'a> {
/// chunks (within 8 chunks of a player, `getSpawnCandidateChunks`) that /// chunks (within 8 chunks of a player, `getSpawnCandidateChunks`) that
/// are ticking and within 128 blocks of a non-spectator. Also returns how /// are ticking and within 128 blocks of a non-spectator. Also returns how
/// many chunks the tracker holds (`getNaturalSpawnChunkCount`). /// many chunks the tracker holds (`getNaturalSpawnChunkCount`).
pub(super) fn spawning_chunks(&self, players: &[LevelPlayer]) -> (i32, Vec<(i32, i32)>) { pub(super) fn spawning_chunks(
let mut candidates: Vec<(i32, i32)> = Vec::with_capacity(289 * players.len()); &self,
let mut seen = horizon_client::fast_hash::FxHashSet::default(); players: &[LevelPlayer],
scratch: &mut SpawnScratch,
) -> i32 {
let candidates = &mut scratch.candidates;
candidates.clear();
candidates.reserve(289 * players.len());
let seen = &mut scratch.seen;
seen.clear();
seen.reserve(289 * players.len());
for p in players { for p in players {
let (pcx, pcz) = ( let (pcx, pcz) = (
p.position.x.floor() as i32 >> 4, p.position.x.floor() as i32 >> 4,
@@ -471,7 +488,7 @@ impl<'a> Game<'a> {
.iter() .iter()
.any(|p| close_enough_for_spawning(p, (cx, cz))) .any(|p| close_enough_for_spawning(p, (cx, cz)))
}); });
(count, candidates) count
} }
/// `ServerChunkCache.tickChunks`: the spawn state over the loaded mobs, /// `ServerChunkCache.tickChunks`: the spawn state over the loaded mobs,
/// the categories under their global caps (creatures every 400 ticks), /// the categories under their global caps (creatures every 400 ticks),
@@ -482,8 +499,9 @@ impl<'a> Game<'a> {
if players.is_empty() { if players.is_empty() {
return; return;
} }
let (spawnable, mut chunks) = self.spawning_chunks(&players); let mut scratch = std::mem::take(&mut self.spawn_scratch);
for &key in &chunks { let spawnable = self.spawning_chunks(&players, &mut scratch);
for &key in &scratch.candidates {
*self.world.inhabited.entry(key).or_insert(0) += 1; *self.world.inhabited.entry(key).or_insert(0) += 1;
} }
let spawn_mobs = self.ui.rules.bool("spawn_mobs"); let spawn_mobs = self.ui.rules.bool("spawn_mobs");
@@ -524,12 +542,12 @@ impl<'a> Game<'a> {
Vec::new() Vec::new()
}; };
// Util.shuffle. // Util.shuffle.
for i in (1..chunks.len()).rev() { for i in (1..scratch.candidates.len()).rev() {
let j = random.next_int_bound(i as i32 + 1).unwrap_or(0) as usize; let j = random.next_int_bound(i as i32 + 1).unwrap_or(0) as usize;
chunks.swap(i, j); scratch.candidates.swap(i, j);
} }
if !categories.is_empty() { if !categories.is_empty() {
for chunk in chunks { for chunk in scratch.candidates.drain(..) {
let mut spawn = |l: &GameLevel, r: &SpawnRequest| { let mut spawn = |l: &GameLevel, r: &SpawnRequest| {
let spawned = l.game.finalize_spawned(r, &mut pending); let spawned = l.game.finalize_spawned(r, &mut pending);
if let Some(s) = &spawned { if let Some(s) = &spawned {
@@ -551,6 +569,7 @@ impl<'a> Game<'a> {
} }
self.world.random = random; self.world.random = random;
self.add_spawned(pending, false); self.add_spawned(pending, false);
self.spawn_scratch = scratch;
} }
/// `NoiseBasedChunkGenerator.spawnOriginalMobs`: creature packs for a /// `NoiseBasedChunkGenerator.spawnOriginalMobs`: creature packs for a
/// freshly generated chunk, from the biome at its corner column's top, /// freshly generated chunk, from the biome at its corner column's top,