diff --git a/crates/horizon-client/examples/chunk_bench.rs b/crates/horizon-client/examples/chunk_bench.rs index be60496f0..0d4a71ce6 100644 --- a/crates/horizon-client/examples/chunk_bench.rs +++ b/crates/horizon-client/examples/chunk_bench.rs @@ -9,7 +9,7 @@ use horizon_client::{ biome::Biomes, blocks::BlockFacts, chunk::{Chunk, MIN_Y, SECTIONS}, - light::{LightTables, light_chunk}, + light::{LightTables, apply_lighting, light_chunk}, mesher::{MeshTables, Mesher}, models, streaming::{ChunkGenerator, Proto}, @@ -105,7 +105,7 @@ fn main() -> Result<()> { ); let mut chunks: Vec = chunks.iter().map(|c| (**c).clone()).collect(); for ((x, z), sections) in lit { - chunks[((z - 1) * inner + x - 1) as usize].sections = sections; + apply_lighting(&mut chunks[((z - 1) * inner + x - 1) as usize], sections); } let chunks: Vec> = chunks.into_iter().map(Arc::new).collect(); let at = |x: i32, z: i32| chunks[((z - 1) * inner + x - 1) as usize].clone(); diff --git a/crates/horizon-client/examples/light_parity.rs b/crates/horizon-client/examples/light_parity.rs index 2307c6da1..afc3e3156 100644 --- a/crates/horizon-client/examples/light_parity.rs +++ b/crates/horizon-client/examples/light_parity.rs @@ -9,7 +9,7 @@ use horizon_client::{ biome::Biomes, blocks::BlockFacts, chunk::{Chunk, MIN_Y, SECTIONS, index}, - light::{LightTables, light_chunk}, + light::{LightTables, apply_lighting, light_chunk}, save, }; use horizon_core::position::ChunkPos; @@ -101,7 +101,7 @@ fn main() -> horizon_client::Result<()> { .unwrap_or((None, None)); let nib = |a: &Option>| a.as_ref().map(|a| (a[i >> 1] >> ((i & 1) * 4)) & 15); let id = chunks[&(x, z)].get(wx & 15, wy, wz & 15); - let packed = lit[si].light(i); + let packed = lit[si].get(i); println!( "{wy}: {} horizon sky {} block {} vanilla sky {:?} block {:?}", facts.states[id as usize].name, @@ -157,7 +157,7 @@ fn main() -> horizon_client::Result<()> { .map_or(15, |a| (a[(i & 255) >> 1] >> ((i & 1) * 4)) & 15), }; let vb = nibble(&block, 0); - let packed = section.light(i); + let packed = section.get(i); let (hs, hb) = (packed >> 4, packed & 15); total += 1; if hs != vs { @@ -228,7 +228,7 @@ fn incremental( std::array::from_fn(|dx| chunks.get(&(x + dx as i32 - 1, z + dz as i32 - 1))) }); let mut c = chunks[&(x, z)].clone(); - c.sections = light_chunk(®ion, tables, sky_light); + apply_lighting(&mut c, light_chunk(®ion, tables, sky_light)); c.lit = true; world.insert((x, z), Arc::new(c)); } @@ -299,7 +299,7 @@ fn incremental( let full = light_chunk(®ion, tables, sky_light); for (si, s) in full.iter().enumerate() { for i in 0..4096 { - let (a, b) = (s.light(i), world[&(kx, kz)].sections[si].light(i)); + let (a, b) = (s.get(i), world[&(kx, kz)].sections[si].light(i)); if a != b { bad += 1; if bad < 20 { diff --git a/crates/horizon-client/examples/mob_ai_probe.rs b/crates/horizon-client/examples/mob_ai_probe.rs index 1645bc01c..b33ebde49 100644 --- a/crates/horizon-client/examples/mob_ai_probe.rs +++ b/crates/horizon-client/examples/mob_ai_probe.rs @@ -11,7 +11,7 @@ use horizon_client::{ collisions, containers::{BlockEntity, CreakingHeart}, items::{ItemStack, Items}, - light::{LightTables, light_chunk}, + light::{LightTables, apply_lighting, light_chunk}, mobs::{ Damage, MobType, creaking, creaking_heart::{self, HeartEvent, HeartHost}, @@ -779,7 +779,7 @@ fn main() -> horizon_client::Result<()> { }) }); let mut c = (*world.chunks[&(x, z)]).clone(); - c.sections = light_chunk(®ion, &tables, true); + apply_lighting(&mut c, light_chunk(®ion, &tables, true)); c.lit = true; ((x, z), c) }) diff --git a/crates/horizon-client/examples/save_parity.rs b/crates/horizon-client/examples/save_parity.rs index 017f60294..ba2c4b0fa 100644 --- a/crates/horizon-client/examples/save_parity.rs +++ b/crates/horizon-client/examples/save_parity.rs @@ -10,7 +10,7 @@ use horizon_client::{ biome::Biomes, blocks::BlockFacts, chunk::{Chunk, MIN_Y, SECTIONS}, - light::{LightTables, light_chunk}, + light::{LightTables, apply_lighting, light_chunk}, save::{self, SaveTables}, }; use horizon_core::position::ChunkPos; @@ -157,7 +157,7 @@ fn main() -> horizon_client::Result<()> { std::array::from_fn(|dx| chunks.get(&(x + dx as i32 - 1, z + dz as i32 - 1))) }); let mut chunk = chunks[&(x, z)].clone(); - chunk.sections = light_chunk(®ion, &lights, sky_light); + apply_lighting(&mut chunk, light_chunk(®ion, &lights, sky_light)); chunk.lit = true; let ours = save::encode_with(&chunk, assets.registry, &biomes, Some(&tables), sky_light); let theirs = &originals[&(x, z)]; diff --git a/crates/horizon-client/src/chunk.rs b/crates/horizon-client/src/chunk.rs index 01147dea5..4e0936a05 100644 --- a/crates/horizon-client/src/chunk.rs +++ b/crates/horizon-client/src/chunk.rs @@ -20,6 +20,15 @@ pub enum Light { Uniform(u8), Full(Box<[u8; 4096]>), } +impl Light { + #[inline] + pub fn get(&self, i: usize) -> u8 { + match self { + Self::Uniform(v) => *v, + Self::Full(a) => a[i], + } + } +} #[inline] pub fn index(x: i32, y: i32, z: i32) -> usize { ((y & 15) << 8 | (z & 15) << 4 | (x & 15)) as usize @@ -59,10 +68,7 @@ impl Section { } #[inline] pub fn light(&self, i: usize) -> u8 { - match &self.light { - Light::Uniform(v) => *v, - Light::Full(a) => a[i], - } + self.light.get(i) } pub fn is_uniform(&self, v: u16) -> bool { self.blocks.is_uniform(u32::from(v)) diff --git a/crates/horizon-client/src/light.rs b/crates/horizon-client/src/light.rs index ae53676c7..6e1a1fece 100644 --- a/crates/horizon-client/src/light.rs +++ b/crates/horizon-client/src/light.rs @@ -16,7 +16,7 @@ //! directly to resident chunks after a block change. use crate::{ blocks::BlockFacts, - chunk::{Chunk, HEIGHT, Light, MIN_Y, SECTIONS, Section, index}, + chunk::{Chunk, HEIGHT, Light, MIN_Y, SECTIONS, index}, }; use horizon_core::geometry::{Direction, ShapeOp, VoxelShape}; use horizon_world::registry::{BlockRegistry, BlockStateId}; @@ -195,8 +195,8 @@ pub fn light_chunk( region: &[[Option<&Chunk>; 3]; 3], tables: &LightTables, sky_light: bool, -) -> Vec> { - let center = region[1][1].expect("centre chunk"); +) -> Vec { + region[1][1].expect("centre chunk"); // Highest occupied section in the region bounds the work. let mut top_section = 0; for chunk in region.iter().flatten().flatten() { @@ -321,20 +321,22 @@ pub fn light_chunk( }; spread(&mut sky, &mut sky_queue); spread(&mut block, &mut queue); - let mut sections = Vec::with_capacity(SECTIONS); - for (si, section) in center.sections.iter().enumerate() { - let mut out = (**section).clone(); - let open = if sky_light { 0xF0 } else { 0x00 }; - if si * 16 >= h { - out.light = Light::Uniform(open); - } else { - let mut data = Box::new([0u8; 4096]); + pack_lighting(h, &sky, &block, sky_light) +} + +/// Extract just the centre column's light; never copy worker snapshot sections. +fn pack_lighting(h: usize, sky: &[u8], block: &[u8], sky_light: bool) -> Vec { + let at = |x: usize, y: usize, z: usize| (y * W + z) * W + x; + let open = if sky_light { 0xF0 } else { 0x00 }; + (0..SECTIONS) + .map(|si| { + if si * 16 >= h { + return Light::Uniform(open); + } + let mut data = Box::new([open; 4096]); for ly in 0..16 { let y = si * 16 + ly; if y >= h { - for i in 0..256 { - data[ly << 8 | i] = open; - } continue; } for lz in 0..16 { @@ -344,12 +346,21 @@ pub fn light_chunk( } } } - out.light = Light::Full(data); - } - out.compact(); - sections.push(Arc::new(out)); + if data.iter().all(|&v| v == data[0]) { + Light::Uniform(data[0]) + } else { + Light::Full(data) + } + }) + .collect() +} + +/// Move a worker's light into current sections, preserving later block/biome +/// edits and immutable snapshots retained by other workers. +pub fn apply_lighting(chunk: &mut Chunk, lights: Vec) { + for (section, light) in chunk.sections.iter_mut().zip(lights) { + Arc::make_mut(section).light = light; } - sections } /// Resident chunks the incremental relighter reads and writes. @@ -622,7 +633,7 @@ mod tests { std::array::from_fn(|dx| chunks.get(&(x + dx as i32 - 1, z + dz as i32 - 1))) }); let mut c = chunks[&(x, z)].clone(); - c.sections = light_chunk(®ion, &tables(), true); + apply_lighting(&mut c, light_chunk(®ion, &tables(), true)); c.lit = true; ((x, z), Arc::new(c)) }) @@ -691,11 +702,198 @@ mod tests { for (si, s) in full.iter().enumerate() { for i in 0..4096 { assert_eq!( - s.light(i), + s.get(i), w[&(0, 0)].sections[si].light(i), "section {si} index {i}" ); } } } + #[test] + fn lighting_result_preserves_current_edits_and_snapshots() { + let mut current = Chunk::empty(0, 0); + current.set(8, MIN_Y, 8, TORCH); + let region = [[None; 3], [None, Some(¤t), None], [None; 3]]; + let lights = light_chunk(®ion, &tables(), true); + let Light::Full(data) = &lights[0] else { + panic!("torch light varies"); + }; + let result_storage = data.as_ptr(); + let snapshot = current.clone(); + current.set(8, MIN_Y, 8, STONE); + Arc::make_mut(&mut current.sections[0]).biomes.set(0, 3); + let edited_snapshot = current.clone(); + apply_lighting(&mut current, lights); + let Light::Full(data) = ¤t.sections[0].light else { + panic!("torch light varies"); + }; + assert_eq!( + result_storage, + data.as_ptr(), + "returned light storage moves without cloning" + ); + current.lit = true; + assert_eq!(current.get(8, MIN_Y, 8), STONE); + assert_eq!(current.sections[0].biomes.get(0), 3); + assert_eq!(snapshot.get(8, MIN_Y, 8), TORCH); + assert_eq!(edited_snapshot.sections[0].light(index(8, MIN_Y, 8)), 0xF0); + let mut world = HashMap::from([((0, 0), Arc::new(current))]); + // Streaming replays edits made after the job snapshot with force=true. + relight(&mut world, &tables(), true, &[(8, MIN_Y, 8)], true); + let region = [[None; 3], [None, Some(&*world[&(0, 0)]), None], [None; 3]]; + let fresh = light_chunk(®ion, &tables(), true); + for (section, light) in world[&(0, 0)].sections.iter().zip(fresh) { + for i in 0..4096 { + assert_eq!(section.light(i), light.get(i)); + } + } + } + + #[test] + fn empty_chunk_missing_neighbours_and_no_sky() { + let mut chunk = Chunk::empty(0, 0); + for sky in [false, true] { + let region = [[None; 3], [None, Some(&chunk), None], [None; 3]]; + let lights = light_chunk(®ion, &tables(), sky); + assert_eq!(lights.len(), SECTIONS); + assert!( + lights + .iter() + .all(|v| matches!(v, Light::Uniform(n) if *n == if sky { 0xF0 } else { 0 })) + ); + let section = Arc::as_ptr(&chunk.sections[0]); + apply_lighting(&mut chunk, lights); + assert_eq!(section, Arc::as_ptr(&chunk.sections[0])); + } + } + + // Original main's worker packaging and streaming application. Kept here + // so the benchmark compares the real former copy/compaction work. + fn baseline_pack( + center: &Chunk, + h: usize, + sky: &[u8], + block: &[u8], + ) -> Vec> { + let at = |x: usize, y: usize, z: usize| (y * W + z) * W + x; + let mut sections = Vec::with_capacity(SECTIONS); + for (si, section) in center.sections.iter().enumerate() { + let mut out = (**section).clone(); + if si * 16 >= h { + out.light = Light::Uniform(0xF0); + } else { + let mut data = Box::new([0u8; 4096]); + for ly in 0..16 { + let y = si * 16 + ly; + if y >= h { + for i in 0..256 { + data[ly << 8 | i] = 0xF0; + } + continue; + } + for lz in 0..16 { + for lx in 0..16 { + let p = at(16 + lx, y, 16 + lz); + data[index(lx as i32, ly as i32, lz as i32)] = sky[p] << 4 | block[p]; + } + } + } + out.light = Light::Full(data); + } + out.compact(); + sections.push(Arc::new(out)); + } + sections + } + fn baseline_apply(chunk: &mut Chunk, sections: Vec>) { + for (target, lit) in chunk.sections.iter_mut().zip(sections) { + let mut section = (**target).clone(); + section.light = lit.light.clone(); + *target = Arc::new(section); + } + } + + #[test] + #[ignore = "microbenchmark; run with --ignored --nocapture"] + fn benchmark_worker_light_results() { + use std::{hint::black_box, time::Instant}; + let mut fixture = Chunk::empty(0, 0); + for (si, s) in fixture.sections.iter_mut().enumerate() { + let section = Arc::make_mut(s); + for i in 0..4096 { + section.set(i, ((i + si) % 16) as u16); + } + for i in 0..64 { + section.biomes.set(i, (i % 4) as u32); + } + section.light = Light::Full(Box::new([0x87; 4096])); + } + let h = HEIGHT as usize; + let sky: Vec<_> = (0..W * W * h).map(|i| (i % 16) as u8).collect(); + let block: Vec<_> = (0..W * W * h).map(|i| ((i / 16) % 16) as u8).collect(); + let mut old = fixture.clone(); + let mut new = fixture.clone(); + baseline_apply(&mut old, baseline_pack(&fixture, h, &sky, &block)); + apply_lighting(&mut new, pack_lighting(h, &sky, &block, true)); + for (a, b) in old.sections.iter().zip(&new.sections) { + for i in 0..4096 { + assert_eq!(a.light(i), b.light(i)); + assert_eq!(a.get(i), b.get(i)); + } + for i in 0..64 { + assert_eq!(a.biomes.get(i), b.biomes.get(i)); + } + } + for shared in [false, true] { + let mut times = [Vec::new(), Vec::new()]; + for round in 0..11 { + for optimized in [round % 2 == 0, round % 2 != 0] { + let mut current = fixture.clone(); + let held = shared.then(|| current.clone()); + // Warm up and detach for the uniquely-owned scenario. + apply_lighting(&mut current, pack_lighting(h, &sky, &block, true)); + let started = Instant::now(); + for _ in 0..500 { + let retained = shared.then(|| current.clone()); + if optimized { + apply_lighting( + black_box(&mut current), + pack_lighting(h, black_box(&sky), black_box(&block), true), + ); + } else { + baseline_apply( + black_box(&mut current), + baseline_pack( + black_box(&fixture), + h, + black_box(&sky), + black_box(&block), + ), + ); + } + black_box(retained); + black_box(¤t); + } + if round > 0 { + times[usize::from(optimized)] + .push(started.elapsed().as_secs_f64() * 1e6 / 500.0); + } + black_box(held); + } + } + for values in &mut times { + values.sort_by(f64::total_cmp); + } + println!( + "pack+apply shared={shared}: baseline {:.3} us optimized {:.3} us speedup {:.2}x", + times[0][5], + times[1][5], + times[0][5] / times[1][5] + ); + } + let payload: usize = fixture.sections.iter().map(|s| s.heap_bytes()).sum(); + println!( + "fixture heap payload {payload} bytes; old worker clones it plus 24 section Arcs; old application clones current payload and 98304 returned light bytes. New worker clones zero section payload; new application clones current payload only when shared." + ); + } } diff --git a/crates/horizon-client/src/streaming.rs b/crates/horizon-client/src/streaming.rs index 91760c734..664606fbe 100644 --- a/crates/horizon-client/src/streaming.rs +++ b/crates/horizon-client/src/streaming.rs @@ -5,7 +5,7 @@ use crate::fast_hash::{FxHashMap, FxHashSet}; use crate::{ chunk::{Chunk, SECTIONS}, - light::{LightTables, light_chunk}, + light::{LightTables, apply_lighting, light_chunk}, mesher::{MeshTables, Mesher, Region, SectionMesh, borrow}, world::World, }; @@ -93,7 +93,7 @@ pub enum Done { /// A proto chunk, and whether it was decorated (loaded so from the save). Proto(i32, i32, Proto, bool), Decorated(i32, i32, Box<[Proto; 9]>), - Lit(i32, i32, Vec>, u64), + Lit(i32, i32, Vec, u64), Meshed(i32, i32, i32, SectionMesh, u64), } struct Queued { @@ -491,11 +491,7 @@ impl Streaming { if let Some(chunk) = world.chunks.get_mut(&(x, z)) { let chunk = Arc::make_mut(chunk); // Keep blocks edited since the snapshot; take only light. - for (target, lit) in chunk.sections.iter_mut().zip(sections) { - let mut section = (**target).clone(); - section.light = lit.light.clone(); - *target = Arc::new(section); - } + apply_lighting(chunk, sections); chunk.lit = true; self.lit.insert((x, z)); self.replay_light_edits(world, x, z, snapshot); diff --git a/docs/performance/lighting-worker-results.md b/docs/performance/lighting-worker-results.md new file mode 100644 index 000000000..7c52b1276 --- /dev/null +++ b/docs/performance/lighting-worker-results.md @@ -0,0 +1,56 @@ +# Lighting worker result packaging + +`light_chunk` now returns only `Vec`. `apply_lighting` moves each light +into the current section through `Arc::make_mut`, preserving current block and +biome edits and snapshots held by other workers. Streaming still rejects stale +sequences before applying results and replays edits after applying valid light. +All direct examples use the same application function. + +## Reproduce + +```sh +CARGO_INCREMENTAL=0 CARGO_TARGET_DIR=/home/bob/Documents/horizon/target cargo test -p horizon-client --lib --profile fast benchmark_worker_light_results -- --ignored --nocapture +``` + +The ignored test invokes the production `pack_lighting` and `apply_lighting` +functions and compares them to original main's packaging/application loops. +It checks every resulting block, biome, and light value before timing. Each +measurement uses 500 columns per batch, one discarded warm-up batch, and ten +measured batches with alternating baseline/optimized order and `black_box`. +The reported median is the upper middle of the ten samples. Both implementations +receive identical precomputed propagation arrays. The fixture contains 24 +sections with 16-state block palettes, four-biome palettes, and existing full +light arrays. A second case retains a fresh snapshot each iteration to force +copy-on-write during application. Fixtures are synthetic, not loaded saves. + +AMD Ryzen 5 5600X, Linux x86_64, rustc 1.99.0, optimized `fast` profile, +nonincremental, no LTO: + +| Packaging + application | Original main | New | Speedup | +| --- | ---: | ---: | ---: | +| Sections uniquely owned | 63.948 us/column | 44.397 us/column | 1.44x | +| Snapshot retained | 65.332 us/column | 53.025 us/column | 1.23x | + +These measurements exclude propagation, region snapshot creation, outer chunk +copy-on-write, channels, replay, and remeshing. They establish savings in the +actual worker output/application paths, not a whole lighting runtime or FPS gain. + +## Copy and allocation scope + +`Section::heap_bytes` reports 149,760 bytes of fixture payload per column. +Original packaging clones that payload and allocates 24 section Arcs. Original +application clones the current section payload, allocates another 24 section +Arcs, and clones 98,304 bytes of returned light. New packaging clones no section +payload and allocates no section Arcs. New application moves the returned light; +with unique sections it clones no payload and allocates no section Arcs. With a +retained snapshot it still copies the current section payload and allocates 24 +Arcs, as required to preserve the snapshot. New light arrays and the result Vec +still allocate. These are structural copy/allocation observations, not global +allocator instrumentation; byte figures exclude Arc headers, Vec headers, and +allocator overhead. + +Focused tests cover moved light buffer identity, current block/biome edits, +retained snapshots, forced replay versus fresh full relighting, empty chunks, +missing neighbours, no-sky dimensions, light attenuation, occlusion, and +incremental/full equivalence. The stale-sequence guard and missing-current-chunk +handling in streaming are unchanged.