Merge pull request #873 from ludovic111/perf/spawn-surface-heightmap

🤖 Agent: Merged. Natural spawning reads a WORLD_SURFACE heightmap kept on the chunk, as vanilla: column-height lookups 333/183 → 0/3 samples, natural spawning per tick 1.36/0.78 → 0.66/0.49 ms. 9 harnesses pass (natural_spawn, spawn_rules, heightmap, chunk_generation, chunk_stage, spawner, finalize_spawn, world_spawn, raid).
This commit is contained in:
Ludovic Marie
2026-10-18 16:50:15 +02:00
committed by GitHub
6 changed files with 118 additions and 18 deletions
@@ -66,7 +66,6 @@ fn fixture(per_chunk: usize) -> (Vec<Arc<Chunk>>, Entities, Names) {
for s in &mut template.sections[..8] {
Arc::make_mut(s).blocks = PalettedContainer::new(block_strategy(), 1);
}
template.heights = Arc::new([64; 256]);
for i in 0..256 {
let x = i % 16 - 8;
let z = i / 16 - 8;
+102 -5
View File
@@ -3,7 +3,7 @@
//! (copy-on-write through `Arc::make_mut`).
use horizon_core::paletted::{PalettedContainer, Strategy};
use horizon_world::registry::BlockStateId;
use std::sync::Arc;
use std::sync::{Arc, OnceLock};
pub const MIN_Y: i32 = -64;
pub const HEIGHT: i32 = 384;
@@ -45,6 +45,10 @@ impl Light {
}
}
#[inline]
fn column(x: i32, z: i32) -> usize {
((z & 15) << 4 | (x & 15)) as usize
}
#[inline]
pub fn index(x: i32, y: i32, z: i32) -> usize {
((y & 15) << 8 | (z & 15) << 4 | (x & 15)) as usize
}
@@ -118,8 +122,10 @@ pub struct Chunk {
pub x: i32,
pub z: i32,
pub sections: Vec<Arc<Section>>,
/// Highest non-air, light-blocking-or-fluid Y per column (MOTION_BLOCKING-like), for rain/sky.
pub heights: Arc<[i16; 256]>,
/// `Heightmap.Types.WORLD_SURFACE` per column (`z << 4 | x`): one above
/// the highest non-air block, or `MIN_Y`. Built on the first read; a
/// `set` that can move a column's surface drops it.
surface: OnceLock<Box<[i16; 256]>>,
pub lit: bool,
/// Light read from a save with `isLightOn`: no light job is needed.
pub light_loaded: bool,
@@ -148,7 +154,7 @@ impl Chunk {
sections: (0..SECTIONS)
.map(|_| Arc::new(Section::default()))
.collect(),
heights: Arc::new([MIN_Y as i16; 256]),
surface: OnceLock::new(),
lit: false,
light_loaded: false,
fresh: false,
@@ -190,7 +196,52 @@ impl Chunk {
if !(0..SECTIONS as i32).contains(&s) {
return v;
}
Arc::make_mut(&mut self.sections[s as usize]).set(index(x, y, z), v)
let previous = Arc::make_mut(&mut self.sections[s as usize]).set(index(x, y, z), v);
// `Heightmap.update`: a write two or more below the surface can't
// move it. Any other change rebuilds the map on the next read.
if previous != v
&& let Some(surface) = self.surface.get()
&& y >= i32::from(surface[column(x, z)]) - 1
{
self.surface = OnceLock::new();
}
previous
}
/// `getHeight(Heightmap.Types.WORLD_SURFACE, x, z)` (`x`, `z` within the
/// chunk): one above the highest block that isn't `air`, or `MIN_Y`.
/// Sections must change only through `set` once this has been read.
pub fn world_surface(&self, x: i32, z: i32, air: impl Fn(u32) -> bool) -> i32 {
let surface = self.surface.get_or_init(|| self.prime_surface(air));
i32::from(surface[column(x, z)])
}
/// `Heightmap.primeHeightmaps` for WORLD_SURFACE: each section from the
/// top, skipping those of one air state, until every column is found.
fn prime_surface(&self, air: impl Fn(u32) -> bool) -> Box<[i16; 256]> {
let mut surface = Box::new([MIN_Y as i16; 256]);
let mut found = [false; 256];
let mut left = 256;
let mut values = [0u32; 4096];
for (s, section) in self.sections.iter().enumerate().rev() {
if left == 0 {
break;
}
if section.blocks.uniform().is_some_and(&air) {
continue;
}
section.unpack(&mut values);
let base = MIN_Y + s as i32 * 16;
for i in 0..256 {
if found[i] {
continue;
}
if let Some(dy) = (0..16).rev().find(|&dy| !air(values[dy << 8 | i])) {
surface[i] = (base + dy as i32 + 1) as i16;
found[i] = true;
left -= 1;
}
}
}
surface
}
#[inline]
pub fn light(&self, x: i32, y: i32, z: i32) -> u8 {
@@ -261,4 +312,50 @@ mod tests {
);
assert_eq!(Chunk::empty(0, 0).highest(air, |_| unreachable!()), None);
}
/// The kept WORLD_SURFACE heights match a scan of every column after
/// each of a few thousand writes (air, cave air, stone, whole sections).
#[test]
fn world_surface_follows_writes() {
let (air, cave_air, stone) = (0, 7, 1);
let is_air = |v: u32| v == air || v == cave_air;
let scan = |c: &Chunk, x: i32, z: i32| {
(MIN_Y..MIN_Y + HEIGHT)
.rev()
.find(|&y| !is_air(c.get(x, y, z)))
.map_or(MIN_Y, |y| y + 1)
};
let mut chunk = Chunk::empty(0, 0);
assert_eq!(chunk.world_surface(5, 5, is_air), MIN_Y);
for y in MIN_Y..MIN_Y + 70 {
for i in 0..256 {
chunk.set(i & 15, y, i >> 4, stone);
}
}
for i in 0..4096 {
Arc::make_mut(&mut chunk.sections[8]).set(i, cave_air);
}
let mut random = horizon_core::random::LegacyRandom::new(9);
use horizon_core::random::RandomSource;
for step in 0..4000 {
let x = random.next_int_bound(16).unwrap();
let z = random.next_int_bound(16).unwrap();
let y = MIN_Y + 40 + random.next_int_bound(120).unwrap();
let v = [air, cave_air, stone][random.next_int_bound(3).unwrap() as usize];
chunk.set(x, y, z, v);
if step % 7 == 0 {
for i in 0..256 {
assert_eq!(
chunk.world_surface(i & 15, i >> 4, is_air),
scan(&chunk, i & 15, i >> 4)
);
}
}
}
// Out-of-range writes and unchanged states keep the map.
let before = chunk.world_surface(3, 3, is_air);
chunk.set(3, MIN_Y + HEIGHT, 3, stone);
chunk.set(3, before - 1, 3, chunk.get(3, before - 1, 3));
assert!(chunk.surface.get().is_some());
assert_eq!(chunk.world_surface(3, 3, |_| unreachable!()), before);
}
}
+2 -5
View File
@@ -99,11 +99,8 @@ impl Game<'_> {
}
/// `getHeight(WORLD_SURFACE)`: above the highest non-air block.
pub(super) fn world_surface(&self, x: i32, z: i32) -> i32 {
let (min, max) = (super::MIN_Y, super::MIN_Y + super::HEIGHT);
(min..max)
.rev()
.find(|&y| !self.res.facts.is_air(self.world.block(x, y, z)))
.map_or(min, |y| y + 1)
self.world
.world_surface(x, z, |id| self.res.facts.is_air(id))
}
/// `BeaconBlockEntity.tick` of the beacon at `key`.
pub(super) fn tick_beacon_at(&mut self, key: (i32, i32, i32)) {
@@ -198,10 +198,14 @@ impl NaturalLevel for GameLevel<'_, '_> {
fn spawn_mobs(&self) -> bool {
self.game.ui.rules.bool("spawn_mobs")
}
/// Scanned down the column: sections of air alone are skipped (air counts
/// for no heightmap), and an unloaded column reads as air.
/// WORLD_SURFACE (every spawn attempt's) is the chunk's kept heightmap.
/// The others are scanned down the column: sections of air alone are
/// skipped (air counts for no heightmap). An unloaded column reads as air.
fn heightmap(&self, kind: Heightmap, x: i32, z: i32) -> i32 {
let world = self.w.world;
if kind == Heightmap::WorldSurface {
return self.w.world_surface(x, z);
}
world
.chunks
.get(&(x >> 4, z >> 4))
@@ -589,11 +589,7 @@ impl SpawnLevel for super::MobWorld<'_> {
self.sea_level
}
fn world_surface(&self, x: i32, z: i32) -> i32 {
let top = crate::chunk::MIN_Y + crate::chunk::HEIGHT;
(crate::chunk::MIN_Y..top)
.rev()
.find(|&y| !self.facts.is_air(self.world.block(x, y, z)))
.map_or(crate::chunk::MIN_Y, |y| y + 1)
self.world.world_surface(x, z, |id| self.facts.is_air(id))
}
fn peaceful(&self) -> bool {
self.difficulty.effective == 0.0
+7
View File
@@ -283,6 +283,13 @@ impl World {
}
Ok(())
}
/// `getHeight(Heightmap.Types.WORLD_SURFACE, x, z)`: one above the highest
/// block that isn't `air`, or `MIN_Y` (also where the chunk isn't loaded).
pub fn world_surface(&self, x: i32, z: i32, air: impl Fn(BlockStateId) -> bool) -> i32 {
self.chunks.get(&(x >> 4, z >> 4)).map_or(MIN_Y, |chunk| {
chunk.world_surface(x, z, |v| air(BlockStateId(v)))
})
}
pub fn block(&self, x: i32, y: i32, z: i32) -> BlockStateId {
self.chunks
.get(&(x >> 4, z >> 4))