Skip to content

Minecraft: Java Edition 26.3 rewritten from scratch in Rust, checked against vanilla with 281 differential harnesses. Get it with the Horizon launcher.

chunk_load_bench: the workers' mesh path, per-section percentiles, parallel throughput

Ludovic Marie5334979f6051
diff --git a/crates/horizon-client-render/src/mesher.rs b/crates/horizon-client-render/src/mesher.rs
index f235d255d..876177636 100644
--- a/crates/horizon-client-render/src/mesher.rs
+++ b/crates/horizon-client-render/src/mesher.rs
@@ -283,7 +283,7 @@ impl SectionMesh {
pub const BOUNDS_PAD: f32 = 0.5;
/// Retain geometry capacity after an upload, but none of its old contents.
- pub(crate) fn clear(&mut self) {
+ pub fn clear(&mut self) {
self.solid.clear();
self.translucent.clear();
self.block_entities.clear();
@@ -889,7 +889,7 @@ impl Padded {
/// geometry still owned by the renderer, and total about 150 KiB per worker
/// (the leaf mask adds 8,000 cells, depths and a queue).
#[derive(Default)]
-pub(crate) struct MeshScratch {
+pub struct MeshScratch {
padded: Padded,
own: Option<Box<[u32; 4096]>>,
visibility: Vec<usize>,
@@ -1144,7 +1144,9 @@ impl Mesher<'_> {
&mut MeshScratch::default(),
)
}
- pub(crate) fn mesh_reusing(
+ /// The meshing workers' path: `out`'s buffers and `scratch` are reused
+ /// rather than allocated per section.
+ pub fn mesh_reusing(
&mut self,
region: &[[Option<&Chunk>; 3]; 3],
sy: i32,
diff --git a/crates/horizon-client/examples/chunk_load_bench.rs b/crates/horizon-client/examples/chunk_load_bench.rs
index 1043d27f6..9f651215b 100644
--- a/crates/horizon-client/examples/chunk_load_bench.rs
+++ b/crates/horizon-client/examples/chunk_load_bench.rs
@@ -5,13 +5,16 @@
//!
//! cargo run --release -p horizon-client --example chunk_load_bench -- \
//! .local/horizon-client/26.3 [--seed 1234] [--center CX CZ] [--radius 8]
-//! [--threads N] [--rounds 3]
+//! [--threads N] [--rounds 3] [--mesh-threads N] [--json out.json]
//!
//! The default centre is the village of `tools/perf_bench.py`. The generation
//! figure is wall time with N workers (the game's own pool); the mesh figure is
-//! this thread's CPU time in `Mesher::mesh`, the best of `--rounds`, so other
-//! work on the machine changes it less. The checksum of the meshes shows that an
-//! A/B pair of builds made the same ones.
+//! this thread's CPU time in the workers' path (`Mesher::mesh_reusing` with a
+//! recycled mesh and scratch, then `sort_solid`), each section's best of
+//! `--rounds`, so other work on the machine changes it less; then the wall time
+//! of `--mesh-threads` threads meshing them all at once. The checksum of the
+//! meshes (vertices and visibility) shows that an A/B pair of builds made the
+//! same ones.
extern crate horizon_client_lib as horizon_client;
use horizon_client::{
assets::Assets,
@@ -20,7 +23,7 @@ use horizon_client::{
chunk::HEIGHT,
graphics::LeafCullingMode,
light::LightTables,
- mesher::{MeshTables, Mesher, Region, borrow},
+ mesher::{MeshScratch, MeshTables, Mesher, Region, SectionMesh, TerrainVertex, borrow},
models,
spans::thread_cpu,
streaming::{Streaming, WorkerContext},
@@ -32,7 +35,10 @@ use horizon_world::registry::BlockStateId;
use std::{
hash::{DefaultHasher, Hash, Hasher},
path::PathBuf,
- sync::Arc,
+ sync::{
+ Arc,
+ atomic::{AtomicUsize, Ordering},
+ },
thread,
time::{Duration, Instant},
};
@@ -131,43 +137,176 @@ fn main() -> Result<(), Error> {
})
.flat_map(|region| (0..HEIGHT / 16).map(move |sy| (region.clone(), sy)))
.collect();
- let mut best = Duration::MAX;
- let (mut vertices, mut checksum) = (0, 0);
+ let one = mesh_rounds(&context, seed, &regions, rounds)?;
+ let mut sorted = one.per_section.clone();
+ sorted.sort_unstable();
+ let pct = |p: f64| {
+ let i = ((sorted.len() as f64 - 1.0) * p).round() as usize;
+ sorted.get(i).map_or(0.0, |d| d.as_secs_f64() * 1e6)
+ };
+ let total: Duration = one.per_section.iter().sum();
+ let filled: Vec<Duration> = one
+ .per_section
+ .iter()
+ .zip(&one.vertices_per_section)
+ .filter(|&(_, &v)| v > 0)
+ .map(|(d, _)| *d)
+ .collect();
+ let filled_total: Duration = filled.iter().sum();
+ let bytes = one.vertices * std::mem::size_of::<TerrainVertex>();
+ println!(
+ "meshed {} sections ({} with geometry, {} vertices, {:.1} MB of vertices, checksum {:016x}) in {:.1} ms of CPU ({:.1} us per section)",
+ regions.len(),
+ filled.len(),
+ one.vertices,
+ bytes as f64 / 1e6,
+ one.checksum,
+ total.as_secs_f64() * 1e3,
+ total.as_secs_f64() * 1e6 / regions.len().max(1) as f64
+ );
+ println!(
+ "per section (best of {rounds} rounds, us): p50 {:.1} p90 {:.1} p99 {:.1} max {:.1}; with geometry: mean {:.1}; {:.0} sections/s per core",
+ pct(0.5),
+ pct(0.9),
+ pct(0.99),
+ pct(1.0),
+ filled_total.as_secs_f64() * 1e6 / filled.len().max(1) as f64,
+ regions.len() as f64 / total.as_secs_f64().max(1e-9),
+ );
+ // Every worker meshing at once: the throughput a world being explored gets,
+ // memory bandwidth and the allocator included.
+ let mesh_threads: usize = flag(&args, "--mesh-threads", threads);
+ let parallel = mesh_parallel(&context, seed, &regions, mesh_threads)?;
+ println!(
+ "{mesh_threads} threads meshed {} sections in {:.1} ms of wall time: {:.0} sections/s",
+ regions.len(),
+ parallel.as_secs_f64() * 1e3,
+ regions.len() as f64 / parallel.as_secs_f64().max(1e-9)
+ );
+ if let Some(path) = args
+ .iter()
+ .position(|a| a == "--json")
+ .and_then(|i| args.get(i + 1))
+ {
+ let json = format!(
+ concat!(
+ "{{\"sections\":{},\"filled\":{},\"vertices\":{},\"vertex_bytes\":{},",
+ "\"checksum\":\"{:016x}\",\"cpu_ms\":{:.3},\"p50_us\":{:.2},\"p90_us\":{:.2},",
+ "\"p99_us\":{:.2},\"max_us\":{:.2},\"filled_mean_us\":{:.2},",
+ "\"mesh_threads\":{},\"parallel_per_s\":{:.1}}}\n"
+ ),
+ regions.len(),
+ filled.len(),
+ one.vertices,
+ bytes,
+ one.checksum,
+ total.as_secs_f64() * 1e3,
+ pct(0.5),
+ pct(0.9),
+ pct(0.99),
+ pct(1.0),
+ filled_total.as_secs_f64() * 1e6 / filled.len().max(1) as f64,
+ mesh_threads,
+ regions.len() as f64 / parallel.as_secs_f64().max(1e-9),
+ );
+ std::fs::write(path, json)?;
+ }
+ Ok(())
+}
+
+fn mesher<'a>(context: &'a WorkerContext, seed: i64) -> Mesher<'a> {
+ Mesher {
+ nether_shading: false,
+ shader_pack_lighting: false,
+ zoom: BiomeZoom::new(obfuscate_seed(seed)),
+ blend_radius: 2,
+ smooth_lighting: true,
+ cutout_leaves: true,
+ translucent_layers: &[],
+ leaf_culling: LeafCullingMode::Off,
+ assets: &context.assets,
+ render: &context.render,
+ tables: &context.tables,
+ biomes: &context.biomes,
+ visibility: context.tables.visibility.clone(),
+ }
+}
+
+struct Rounds {
+ /// Each section's CPU time, the least of the rounds.
+ per_section: Vec<Duration>,
+ vertices_per_section: Vec<usize>,
+ vertices: usize,
+ checksum: u64,
+}
+
+/// Meshes every section `rounds` times on this thread the way a worker does:
+/// one mesher (its face cache kept), one scratch, the mesh buffers recycled,
+/// and the solid quads sorted by side.
+fn mesh_rounds(
+ context: &WorkerContext,
+ seed: i64,
+ regions: &[(Region, i32)],
+ rounds: usize,
+) -> Result<Rounds, Error> {
+ let mut mesher = mesher(context, seed);
+ let mut scratch = MeshScratch::default();
+ let mut recycled = SectionMesh::default();
+ let mut per_section = vec![Duration::MAX; regions.len()];
+ let mut vertices_per_section = vec![0; regions.len()];
+ let mut checksum = 0;
for _ in 0..rounds.max(1) {
- let mut mesher = Mesher {
- nether_shading: false,
- shader_pack_lighting: false,
- zoom: BiomeZoom::new(obfuscate_seed(seed)),
- blend_radius: 2,
- smooth_lighting: true,
- cutout_leaves: true,
- translucent_layers: &[],
- leaf_culling: LeafCullingMode::Off,
- assets: &context.assets,
- render: &context.render,
- tables: &context.tables,
- biomes: &context.biomes,
- visibility: context.tables.visibility.clone(),
- };
- vertices = 0;
let mut hash = DefaultHasher::new();
- let mut spent = Duration::ZERO;
- for (region, sy) in &regions {
+ for (i, (region, sy)) in regions.iter().enumerate() {
let started = thread_cpu().ok_or("no thread CPU clock here")?;
- let mesh = mesher.mesh(&borrow(region), *sy);
- spent += thread_cpu().ok_or("no thread CPU clock here")? - started;
- vertices += mesh.solid.len() + mesh.translucent.len();
+ let mut mesh = mesher.mesh_reusing(&borrow(region), *sy, recycled, &mut scratch);
+ mesh.sort_solid();
+ let spent = thread_cpu().ok_or("no thread CPU clock here")? - started;
+ per_section[i] = per_section[i].min(spent);
+ vertices_per_section[i] = mesh.solid.len() + mesh.translucent.len();
bytemuck::cast_slice::<_, u8>(&mesh.solid).hash(&mut hash);
bytemuck::cast_slice::<_, u8>(&mesh.translucent).hash(&mut hash);
+ mesh.visibility.hash(&mut hash);
+ recycled = mesh;
}
checksum = hash.finish();
- best = best.min(spent);
}
- println!(
- "meshed {} sections ({vertices} vertices, checksum {checksum:016x}) in {:.1} ms of CPU ({:.1} us per section)",
- regions.len(),
- best.as_secs_f64() * 1e3,
- best.as_secs_f64() * 1e6 / regions.len().max(1) as f64
- );
- Ok(())
+ let vertices = vertices_per_section.iter().sum();
+ Ok(Rounds {
+ per_section,
+ vertices_per_section,
+ vertices,
+ checksum,
+ })
+}
+
+/// Wall time for `threads` workers to mesh every section once between them.
+fn mesh_parallel(
+ context: &WorkerContext,
+ seed: i64,
+ regions: &[(Region, i32)],
+ threads: usize,
+) -> Result<Duration, Error> {
+ let next = AtomicUsize::new(0);
+ let started = Instant::now();
+ thread::scope(|scope| {
+ for _ in 0..threads.max(1) {
+ scope.spawn(|| {
+ let mut mesher = mesher(context, seed);
+ let mut scratch = MeshScratch::default();
+ let mut recycled = SectionMesh::default();
+ loop {
+ let i = next.fetch_add(1, Ordering::Relaxed);
+ let Some((region, sy)) = regions.get(i) else {
+ break;
+ };
+ let mut mesh =
+ mesher.mesh_reusing(&borrow(region), *sy, recycled, &mut scratch);
+ mesh.sort_solid();
+ recycled = mesh;
+ }
+ });
+ }
+ });
+ Ok(started.elapsed())
}