Skip provably unchanged native End surface traversal

This commit is contained in:
rdbtCVS
2026-10-22 18:39:53 +01:00
parent 022600481e
commit 0928c83d4c
3 changed files with 341 additions and 27 deletions
@@ -3,6 +3,7 @@
//! what the render thread pays for it in `Streaming::update`.
//!
//! streaming_bench RESOURCES [--threads N] [--radius R] [--seed S] [--fly CHUNKS_PER_S] [--seconds T] [--hz F]
//! [--dimension overworld|the_nether|the_end] [--center-x X] [--center-z Z]
//!
//! First the area around (0, 0) fills; then, with `--fly`, the centre moves along +x
//! at that speed for T seconds while frames run at F per second. Each phase reports
@@ -72,14 +73,33 @@ static ALLOCATOR: CountingAllocator = CountingAllocator;
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
fn cpu_seconds() -> f64 {
std::fs::read_to_string("/proc/self/stat")
.ok()
.and_then(|stat| {
let fields = stat
.rsplit_once(')')?
.1
.split_whitespace()
.collect::<Vec<_>>();
Some((fields[11].parse::<f64>().ok()? + fields[12].parse::<f64>().ok()?) / 100.0)
})
.unwrap_or(0.0)
}
#[derive(Default)]
struct Phase {
updates: Vec<f64>,
local: (u64, u64),
uploads: usize,
stage_samples: [u64; 7],
cpu_start: f64,
}
impl Phase {
fn update(&mut self, streaming: &mut Streaming, world: &mut World) {
if self.updates.is_empty() {
self.cpu_start = cpu_seconds();
}
let before = LOCAL.with(Cell::get);
let started = Instant::now();
let (uploads, _) = streaming.update(world);
@@ -89,6 +109,9 @@ impl Phase {
self.local.1 += after.1 - before.1;
self.uploads += uploads.len();
drop(uploads);
for (sum, (_, count)) in self.stage_samples.iter_mut().zip(streaming.stages()) {
*sum += count as u64;
}
}
fn report(&self, name: &str, wall: Duration, streaming: &Streaming, generated: u64) {
let mut sorted = self.updates.clone();
@@ -106,6 +129,16 @@ impl Phase {
at(0.99),
sorted.last().copied().unwrap_or(0.0),
);
println!(
"BENCH {}",
serde_json::json!({
"phase": name, "wall_s": wall.as_secs_f64(),
"cpu_s": cpu_seconds() - self.cpu_start,
"chunks": streaming.generated_total - generated, "sections": self.uploads,
"updates": self.updates.len(), "update_ms": total,
"mean_stages": self.stage_samples.map(|n| n as f64 / self.updates.len() as f64),
})
);
if TRACK {
println!(
" update allocations: {} ({:.1} MiB)",
@@ -142,6 +175,22 @@ fn main() -> Result<()> {
let fly = flag("--fly", 0.0);
let seconds = flag("--seconds", 10.0);
let hz = flag("--hz", 240.0);
let dimension = args
.iter()
.position(|a| a == "--dimension")
.and_then(|i| args.get(i + 1))
.map(|d| {
if d.contains(':') {
d.clone()
} else {
format!("minecraft:{d}")
}
})
.unwrap_or_else(|| "minecraft:overworld".into());
let center = (
flag("--center-x", 0.0) as i32,
flag("--center-z", 0.0) as i32,
);
let assets = Arc::new(Assets::load(&root)?);
let render = Arc::new(assets.reload(&root)?);
@@ -154,11 +203,12 @@ fn main() -> Result<()> {
models::load_visibility(&root, assets.registry)?,
)?);
let light = Arc::new(LightTables::load(&root, assets.registry, &facts)?);
let terrain = Arc::new(VanillaTerrain::new(
let terrain = Arc::new(VanillaTerrain::for_dimension(
&root.join("worldgen"),
assets.registry,
&biomes,
seed,
&dimension,
)?);
let context = Arc::new(WorkerContext {
assets,
@@ -170,8 +220,12 @@ fn main() -> Result<()> {
let mut world = World::new(facts.air, seed);
let mut streaming = Streaming::new(terrain, context, threads);
streaming.radius = radius;
streaming.center = (0, 0);
println!("{threads} workers, radius {radius}, seed {seed}");
streaming.center = center;
streaming.nether_shading = dimension == "minecraft:the_nether";
streaming.sky_light = dimension != "minecraft:the_nether";
println!(
"{threads} workers, radius {radius}, seed {seed}, dimension {dimension}, center {center:?}"
);
let (allocations, bytes) = (ALLOCATIONS.load(Relaxed), BYTES.load(Relaxed));
// Fill: update as often as the game would uncapped, until settled.
@@ -198,12 +252,12 @@ fn main() -> Result<()> {
let mut behind = Vec::new();
while started.elapsed().as_secs_f64() < seconds {
let next = Instant::now() + frame;
let x = (started.elapsed().as_secs_f64() * fly) as i32;
streaming.center = (x, 0);
let x = center.0 + (started.elapsed().as_secs_f64() * fly) as i32;
streaming.center = (x, center.1);
phase.update(&mut streaming, &mut world);
// Columns within 2 chunks of the player still missing their meshes.
let near = (-2..=2)
.flat_map(|dz| (-2..=2).map(move |dx| (x + dx, dz)))
.flat_map(|dz| (-2..=2).map(move |dx| (x + dx, center.1 + dz)))
.filter(|&(cx, cz)| !streaming.is_lit(cx, cz))
.count();
behind.push(near);
+117 -1
View File
@@ -3,6 +3,7 @@
//! chunkgen_bench <client resource dir> [--seed S] [--size N] [--threads T]
//! [--center CX CZ] [--verify <material-stage parity dir>] [--no-decorate]
//! [--dimension overworld|the_nether|the_end]
//! [--warmup] [--json] [--no-map] [--snapshot PATH]
use horizon_core::position::ChunkPos;
use horizon_world::{
DATA_VERSION,
@@ -274,7 +275,9 @@ fn main() -> Result<(), Error> {
let resources = PathBuf::from(args.next().expect("client resource directory"));
let (mut seed, mut size, mut threads, mut center) = (0i64, 16i32, 2usize, (0, 0));
let mut parity = None;
let mut snapshot_path = None;
let mut decorate = true;
let (mut warmup, mut json, mut map) = (false, false, true);
let mut dimension = String::from("minecraft:overworld");
let mut starts: Option<(i32, i32, i32, i32, PathBuf)> = None;
while let Some(arg) = args.next() {
@@ -285,7 +288,11 @@ fn main() -> Result<(), Error> {
"--threads" => threads = next().parse()?,
"--center" => center = (next().parse()?, next().parse()?),
"--verify" => parity = Some(PathBuf::from(next())),
"--snapshot" => snapshot_path = Some(PathBuf::from(next())),
"--no-decorate" => decorate = false,
"--warmup" => warmup = true,
"--json" => json = true,
"--no-map" => map = false,
"--dimension" => {
let d = next();
dimension = if d.contains(':') {
@@ -326,6 +333,16 @@ fn main() -> Result<(), Error> {
dump_starts(&generator, seed, [x0, z0, x1, z1], &out)?;
return Ok(());
}
if warmup {
let mut worker = generator.worker();
for z in -4..=4 {
for x in -4..=4 {
std::hint::black_box(
worker.generate_proto(ChunkPos::new(center.0 + 64 + x, center.1 + 64 + z))?,
);
}
}
}
let (x0, z0) = (center.0 - size / 2, center.1 - size / 2);
let count = (size * size) as usize;
let next = AtomicUsize::new(0);
@@ -353,6 +370,7 @@ fn main() -> Result<(), Error> {
handles.into_iter().try_for_each(|h| h.join().unwrap())
})?;
let elapsed = start.elapsed();
let cpu_elapsed = cpu_seconds() - cpu;
println!(
"generated {count} proto chunks with {threads} threads in {elapsed:.2?}: {:.1} chunks/s ({:.1} ms/chunk/thread)",
count as f64 / elapsed.as_secs_f64(),
@@ -360,7 +378,7 @@ fn main() -> Result<(), Error> {
);
println!(
" cpu time {:.1} ms/chunk",
(cpu_seconds() - cpu) * 1000. / count as f64
cpu_elapsed * 1000. / count as f64
);
let timings = timings.into_inner().unwrap();
let per_chunk = |f: fn(&StageTimings) -> std::time::Duration| {
@@ -379,10 +397,23 @@ fn main() -> Result<(), Error> {
per_chunk(|t| t.surface),
per_chunk(|t| t.carvers)
);
if json {
println!(
"BENCH {}",
serde_json::json!({"phase": "proto", "chunks": count,
"threads": threads, "wall_s": elapsed.as_secs_f64(), "cpu_s": cpu_elapsed,
"biomes_ms_chunk": per_chunk(|t| t.biomes), "noise_ms_chunk": per_chunk(|t| t.noise),
"structures_ms_chunk": per_chunk(|t| t.structures),
"surface_ms_chunk": per_chunk(|t| t.surface), "carvers_ms_chunk": per_chunk(|t| t.carvers)})
);
}
let mut chunks: Vec<Option<ProtoChunk>> = slots
.into_iter()
.map(|slot| slot.into_inner().unwrap())
.collect();
if let Some(path) = &snapshot_path {
snapshot(&chunks, path, "terrain")?;
}
if decorate && size >= 3 {
let totals = Mutex::new(DecorateStats::default());
let start = Instant::now();
@@ -441,6 +472,13 @@ fn main() -> Result<(), Error> {
}
}
let elapsed = start.elapsed();
if json {
println!(
"BENCH {}",
serde_json::json!({"phase": "decorate", "chunks": decorated,
"wall_s": elapsed.as_secs_f64()})
);
}
let totals = totals.into_inner().unwrap();
println!(
"decorated {decorated} chunks in {elapsed:.2?}: {:.1} chunks/s; feature roots placed {}, unsupported {}, failed {}",
@@ -461,6 +499,9 @@ fn main() -> Result<(), Error> {
);
}
}
if let Some(path) = &snapshot_path {
snapshot(&chunks, path, "decorated")?;
}
let mut structures = BTreeMap::<(String, i32, i32), [[i32; 3]; 2]>::new();
for chunk in chunks.iter().flatten() {
for (name, start, min, max) in chunk.structures() {
@@ -492,6 +533,9 @@ fn main() -> Result<(), Error> {
}
}
}
if !map {
return Ok(());
}
let mut legend = BTreeMap::<char, BTreeMap<String, usize>>::new();
let step = (size * 16 / 96).max(1);
for bz in (0..size * 16).step_by(step as usize) {
@@ -534,3 +578,75 @@ fn main() -> Result<(), Error> {
);
Ok(())
}
// Diagnostic only: full ordered chunk state, outside performance measurements.
fn snapshot(chunks: &[Option<ProtoChunk>], path: &Path, phase: &str) -> Result<(), Error> {
use std::io::Write;
let mut out =
std::io::BufWriter::new(fs::File::create(format!("{}-{phase}.bin", path.display()))?);
for chunk in chunks.iter().flatten() {
let terrain = &chunk.terrain;
writeln!(
out,
"chunk {:?} {:?} {} {:?} {:?}",
chunk.position(),
terrain.generation_stage(),
chunk.is_decorated(),
chunk.structures(),
terrain.below_zero_retrogen().map(|v| v.to_nbt())
)?;
for section in 0..terrain.height() as usize / 16 {
let data = terrain.section(section).unwrap();
writeln!(out, "section {section} {:?}", data.to_nbt()?)?;
for index in 0..4096 {
out.write_all(&data.get(index)?.0.to_le_bytes())?;
}
}
// Compare public reads too, including all-air section behavior.
for y in terrain.min_y()..terrain.min_y() + terrain.height() {
for z in 0..16 {
for x in 0..16 {
out.write_all(&terrain.block(x, y, z).0.to_le_bytes())?;
}
}
}
for y in (terrain.min_y() >> 2)..((terrain.min_y() + terrain.height()) >> 2) {
for z in 0..4 {
for x in 0..4 {
writeln!(
out,
"biome {}",
chunk.biome_at(
chunk.position().x * 16 + x * 4,
y * 4,
chunk.position().z * 16 + z * 4
)
)?;
}
}
}
for kind in HeightmapType::ALL {
writeln!(
out,
"heightmap {kind:?} {:?}",
terrain.heightmap(kind).map(|m| m.raw_data())
)?;
}
writeln!(out, "post {:?}", terrain.postprocessing())?;
writeln!(out, "blockticks {:?}", terrain.block_ticks().pack(0))?;
writeln!(out, "fluidticks {:?}", terrain.fluid_ticks().pack(0))?;
for (position, tag) in &chunk.block_entities {
writeln!(out, "blockentity {position:?}")?;
let bytes =
horizon_world::nbt::write_network(&Tag::Compound(tag.clone()), Default::default())?;
out.write_all(&(bytes.len() as u64).to_le_bytes())?;
out.write_all(&bytes)?;
}
let entities = Tag::List(chunk.entities.iter().cloned().map(Tag::Compound).collect());
let bytes = horizon_world::nbt::write_network(&entities, Default::default())?;
out.write_all(&(bytes.len() as u64).to_le_bytes())?;
out.write_all(&bytes)?;
}
out.flush()?;
Ok(())
}
+164 -20
View File
@@ -995,6 +995,46 @@ where
worker.terrain_context = context;
result
}
/// With the native End source there are no badlands/iceberg extensions.
/// Disabled aquifers with air as their default fluid can only add lava or
/// air to the default solid, so this constant rule would rewrite each solid
/// with the state it already has. Check their imported material flags too.
/// Noise filling already built the WG maps; these writes add no fluid marks.
fn identity_end_surface(&self, pos: ChunkPos) -> bool {
if !matches!(self.source, ResolvedBiomeSource::End(_))
// At wrapped block coordinates the ordinary biome-region lookup
// can fail; retain that lookup and its error outside this range.
|| ![pos.x, pos.z].into_iter().all(|c| {
c.checked_mul(16).is_some_and(|b| {
b.checked_sub(16).is_some() && b.checked_add(31).is_some()
})
})
|| self.aquifer.is_some()
|| Some(self.settings.default_block)
!= self.data.registry.default_state("minecraft:end_stone")
|| Some(self.settings.default_fluid)
!= self.data.registry.default_state("minecraft:air")
|| !self.data.states.get(self.settings.default_fluid).air
|| !self
.data
.registry
.default_state("minecraft:lava")
.is_some_and(|lava| self.data.states.get(lava).fluid)
{
return false;
}
let mut rule = &self.rule;
// Restrict the shortcut to shallow block references; other rules still
// run their normal compilation, queries and error handling.
for _ in 0..32 {
match rule {
MaterialRule::Reference(next) => rule = next,
MaterialRule::Block(state) => return *state == self.settings.default_block,
_ => return false,
}
}
false
}
fn generate_with_context(
&self,
worker: &mut WorkerState,
@@ -1103,27 +1143,29 @@ where
.map_err(stage("noise"))?;
let generation = self.generation();
worker.timings.noise += lap(&mut clock);
build_surface(
&mut chunk,
self.rule.clone(),
SurfaceInputs::<R, _> {
generation,
noise_volume: volume,
root: self.root.clone(),
surface: SurfaceQueries {
pipeline: self,
temperatures: &mut worker.temperatures,
biome: Biomes::Region(&biomes),
possible: Some(possible),
if !self.identity_end_surface(pos) {
build_surface(
&mut chunk,
self.rule.clone(),
SurfaceInputs::<R, _> {
generation,
noise_volume: volume,
root: self.root.clone(),
surface: SurfaceQueries {
pipeline: self,
temperatures: &mut worker.temperatures,
biome: Biomes::Region(&biomes),
possible: Some(possible),
},
preliminary_surface: self.preliminary.clone(),
density_context: context,
write_min_y: min,
write_max_y: min + height - 1,
},
preliminary_surface: self.preliminary.clone(),
density_context: context,
write_min_y: min,
write_max_y: min + height - 1,
},
&self.extensions,
)
.map_err(stage("surface"))?;
&self.extensions,
)
.map_err(stage("surface"))?;
}
worker.timings.surface += lap(&mut clock);
let carvers = &data.biome_carvers;
let mask = generate_carving_mask(self.seed, pos, generation, false, |origin| {
@@ -2407,6 +2449,108 @@ impl GeneratorWorker {
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn end_identity_surface_matches_normal_surface_and_rejects_custom_rules() {
let root = std::env::var_os("HORIZON_TEST_RESOURCES")
.map(std::path::PathBuf::from)
.unwrap_or_else(|| {
Path::new(env!("CARGO_MANIFEST_DIR")).join("../../.local/horizon-client/26.3")
});
if !root.join("worldgen/world_presets.json").exists() {
eprintln!("skipping: no worldgen bundle at {}", root.display());
return;
}
let registry = Box::leak(Box::new(crate::level::data::load_registry(&root).unwrap()));
let data = GeneratorData::load(&root.join("worldgen"), registry).unwrap();
let preset: Value = serde_json::from_str(&data.world_presets["minecraft:normal"]).unwrap();
let settings: Value = serde_json::from_str(&data.settings["minecraft:end"]).unwrap();
let mut pipeline = Pipeline::new(
data.clone(),
12345,
LegacyRandom::new(12345),
&preset["dimensions"]["minecraft:the_end"]["generator"],
&settings,
false,
None,
)
.unwrap();
let mut worker = OverworldGenerator::for_world(
data.clone(),
12345,
"minecraft:normal",
"minecraft:the_end",
false,
)
.unwrap()
.worker();
assert!(pipeline.identity_end_surface(ChunkPos::new(0, 0)));
for pos in [
ChunkPos::new(0, 0),
ChunkPos::new(128, 128),
ChunkPos::new(-128, 192),
ChunkPos::new(1_875_000, -1_875_000),
] {
let actual = pipeline.generate(&mut worker.state, pos).unwrap();
let rule = pipeline.rule.clone();
// A singleton sequence evaluates the same block rule through the full surface path.
pipeline.rule = MaterialRule::Sequence(vec![rule.clone()]);
assert!(!pipeline.identity_end_surface(ChunkPos::new(0, 0)));
let expected = pipeline.generate(&mut worker.state, pos).unwrap();
pipeline.rule = rule;
assert_eq!(
actual.terrain.generation_stage(),
expected.terrain.generation_stage()
);
for section in 0..actual.terrain.height() as usize / 16 {
assert_eq!(
actual.terrain.section(section).unwrap().to_nbt().unwrap(),
expected.terrain.section(section).unwrap().to_nbt().unwrap()
);
}
for kind in HeightmapType::ALL {
assert_eq!(
actual.terrain.heightmap(kind).map(|m| m.raw_data()),
expected.terrain.heightmap(kind).map(|m| m.raw_data())
);
}
assert_eq!(
actual.terrain.postprocessing(),
expected.terrain.postprocessing()
);
assert_eq!(
actual.terrain.block_ticks().pack(0),
expected.terrain.block_ticks().pack(0)
);
assert_eq!(
actual.terrain.fluid_ticks().pack(0),
expected.terrain.fluid_ticks().pack(0)
);
assert_eq!(actual.block_entities, expected.block_entities);
assert_eq!(actual.entities, expected.entities);
assert_eq!(actual.structures(), expected.structures());
}
assert!(!pipeline.identity_end_surface(ChunkPos::new(i32::MAX, 0)));
let old_rule = pipeline.rule.clone();
pipeline.rule = MaterialRule::Block(registry.default_state("minecraft:stone").unwrap());
assert!(!pipeline.identity_end_surface(ChunkPos::new(0, 0)));
pipeline.rule = old_rule;
let old_fluid = pipeline.settings.default_fluid;
pipeline.settings.default_fluid = registry.default_state("minecraft:stone").unwrap();
assert!(!pipeline.identity_end_surface(ChunkPos::new(0, 0)));
pipeline.settings.default_fluid = old_fluid;
let old_source = std::mem::replace(
&mut pipeline.source,
ResolvedBiomeSource::Fixed(super::super::biome_source::FixedBiomeSource {
biome: "minecraft:the_end".into(),
}),
);
assert!(!pipeline.identity_end_surface(ChunkPos::new(0, 0)));
pipeline.source = old_source;
pipeline.aquifer = Some(std::array::from_fn(|_| {
pipeline.router.get(RouterField::Erosion).clone()
}));
assert!(!pipeline.identity_end_surface(ChunkPos::new(0, 0)));
}
#[test]
fn far_surface_matches_undecorated_land_and_keeps_end_void() {