From c47b30fcf0db87b8023e6784d941a4f06524b453 Mon Sep 17 00:00:00 2001 From: rdbtCVS <87152661+rdbtCVS@users.noreply.github.com> Date: Sun, 18 Oct 2026 03:49:02 +0100 Subject: [PATCH] Optimize binary density, Beardifier and Perlin sampling --- crates/horizon-core/src/density/arithmetic.rs | 91 ++++++++++++++++++- crates/horizon-core/src/density/beardifier.rs | 11 ++- crates/horizon-core/src/density/tests.rs | 72 +++++++++++++++ crates/horizon-core/src/noise/perlin.rs | 29 ++++-- docs/noise-aquifer-benchmark.md | 52 +++++++++-- 5 files changed, 233 insertions(+), 22 deletions(-) diff --git a/crates/horizon-core/src/density/arithmetic.rs b/crates/horizon-core/src/density/arithmetic.rs index 7961adf04..51892ffc7 100644 --- a/crates/horizon-core/src/density/arithmetic.rs +++ b/crates/horizon-core/src/density/arithmetic.rs @@ -283,6 +283,23 @@ impl BinaryOp { } } } + fn apply_bulk(self, buffer: &mut [f32], right: &[f32]) { + macro_rules! apply { + ($operation:expr) => { + for (left, &right) in buffer.iter_mut().zip(right) { + *left = ($operation)(*left, right); + } + }; + } + match self { + Self::Add => apply!(|left, right| left + right), + Self::Subtract => apply!(|left, right| left - right), + Self::Multiply => apply!(|left, right| left * right), + Self::Divide => apply!(|left, right| left / right), + Self::Minimum { .. } => apply!(|left, right| if right < left { right } else { left }), + Self::Maximum { .. } => apply!(|left, right| if right > left { right } else { left }), + } + } } #[derive(Clone, Debug)] pub struct BinarySampler { @@ -319,9 +336,7 @@ impl DensitySampler for BinarySampler { self.left.sample_volume(context, buffer, volume)?; context.with_buffer(volume.size(), |context, right| { self.right.sample_volume(context, right, volume)?; - for (left, right) in buffer.iter_mut().zip(right) { - *left = self.operation.apply(*left, *right, true); - } + self.operation.apply_bulk(buffer, right); Ok(()) }) } @@ -420,4 +435,74 @@ mod bulk_tests { } } } + + #[inline(never)] + fn dispatched_binary_reference(operation: BinaryOp, left: f32, right: f32) -> f32 { + operation.apply(left, right, true) + } + + #[test] + fn fixed_binary_loops_preserve_float_bits() { + let edges = [ + 0, + 0x8000_0000, + 1, + 0x8000_0001, + 0x007f_ffff, + 0x0080_0000, + 0x3f80_0000, + 0xbf80_0000, + 0x7f7f_ffff, + 0xff7f_ffff, + 0x7f80_0000, + 0xff80_0000, + 0x7fc0_0000, + 0x7fa0_0001, + 0xffc0_1234, + ]; + let mut left = Vec::new(); + let mut right = Vec::new(); + for a in edges { + for b in edges { + left.push(f32::from_bits(a)); + right.push(f32::from_bits(b)); + } + } + let mut bits = 0x484f_5249u32; + for _ in 0..8192 { + bits ^= bits << 13; + bits ^= bits >> 17; + bits ^= bits << 5; + left.push(f32::from_bits(bits)); + bits ^= bits << 13; + bits ^= bits >> 17; + bits ^= bits << 5; + right.push(f32::from_bits(bits)); + } + for operation in [ + BinaryOp::Add, + BinaryOp::Subtract, + BinaryOp::Multiply, + BinaryOp::Divide, + BinaryOp::Minimum { + right_min: f32::NEG_INFINITY, + }, + BinaryOp::Maximum { + right_max: f32::INFINITY, + }, + ] { + for length in [0, 1, 3, 16, 257, left.len()] { + let mut actual = left[..length].to_vec(); + operation.apply_bulk(&mut actual, &right[..length]); + for (i, actual) in actual.into_iter().enumerate() { + let expected = dispatched_binary_reference(operation, left[i], right[i]); + assert_eq!( + actual.to_bits(), + expected.to_bits(), + "{operation:?}, index {i}" + ); + } + } + } + } } diff --git a/crates/horizon-core/src/density/beardifier.rs b/crates/horizon-core/src/density/beardifier.rs index 73b94a066..2e2868fb8 100644 --- a/crates/horizon-core/src/density/beardifier.rs +++ b/crates/horizon-core/src/density/beardifier.rs @@ -77,6 +77,9 @@ impl Beardifier { } fn sample_unchecked(&self, [x, y, z]: [i32; 3]) -> f32 { let mut value = 0.; + // The kernel is a process-wide constant; resolving it once keeps the + // per-piece and per-junction beard evaluations off the OnceLock. + let kernel = kernel(); for piece in &self.pieces { let low = piece.bounds.minimum; let high = piece.bounds.maximum; @@ -98,7 +101,7 @@ impl Beardifier { TerrainAdjustment::None => 0., TerrainAdjustment::Bury => bury(dx as f32, distance_y as f32 / 2., dz as f32), TerrainAdjustment::BeardThin | TerrainAdjustment::BeardBox => { - beard(dx, distance_y, dz, dy) * 0.8 + beard(kernel, dx, distance_y, dz, dy) * 0.8 } TerrainAdjustment::Encapsulate => { bury(dx as f32 / 2., distance_y as f32 / 2., dz as f32 / 2.) * 0.8 @@ -109,7 +112,7 @@ impl Beardifier { let dx = x.wrapping_sub(junction.source_x); let dy = y.wrapping_sub(junction.source_ground_y); let dz = z.wrapping_sub(junction.source_z); - value += beard(dx, dy, dz, dy) * 0.4; + value += beard(kernel, dx, dy, dz, dy) * 0.4; } value } @@ -184,7 +187,7 @@ fn bury(x: f32, y: f32, z: f32) -> f32 { 1. - squared.sqrt() / 6. } } -fn beard(x: i32, y: i32, z: i32, ground_delta: i32) -> f32 { +fn beard(kernel: &[f32; 13824], x: i32, y: i32, z: i32, ground_delta: i32) -> f32 { let indices = [x, y, z].map(|v| v.wrapping_add(12)); if indices.iter().any(|v| !(0..24).contains(v)) { return 0.; @@ -192,7 +195,7 @@ fn beard(x: i32, y: i32, z: i32, ground_delta: i32) -> f32 { let vertical = ground_delta as f32 + 0.5; let squared = (x as f32 * x as f32 + vertical * vertical) + z as f32 * z as f32; let factor = (-vertical * fast_inverse_sqrt(f64::from(squared / 2.)) as f32) / 2.; - factor * kernel()[((indices[2] * 24 + indices[0]) * 24 + indices[1]) as usize] + factor * kernel[((indices[2] * 24 + indices[0]) * 24 + indices[1]) as usize] } fn fast_inverse_sqrt(value: f64) -> f64 { let half = 0.5 * value; diff --git a/crates/horizon-core/src/density/tests.rs b/crates/horizon-core/src/density/tests.rs index f94b95356..1c0497d68 100644 --- a/crates/horizon-core/src/density/tests.rs +++ b/crates/horizon-core/src/density/tests.rs @@ -795,3 +795,75 @@ fn unary_bulk_keeps_child_failure_writes_and_full_buffer_tail() { .unwrap(); assert_eq!(buffer, [9.0; 3]); } + +#[test] +fn binary_bulk_preserves_child_failure_order_and_output() { + #[derive(Debug)] + struct Input { + name: &'static str, + value: f32, + fail: bool, + calls: Arc>>, + } + impl DensitySampler for Input { + fn sample_value(&self, _: &mut SamplerContext, _: [i32; 3]) -> Result { + unreachable!("bulk-only fixture") + } + fn sample_volume( + &self, + _: &mut SamplerContext, + buffer: &mut [f32], + _: DensityVolume, + ) -> Result<(), SampleError> { + self.calls.lock().unwrap().push(self.name); + buffer.fill(self.value); + if self.fail { + Err(SampleError::ZeroGradientPeriod) + } else { + Ok(()) + } + } + } + let volume = DensityVolume::new([1; 3], [0; 3], [1; 3]).unwrap(); + for left_fails in [true, false] { + let calls = Arc::new(std::sync::Mutex::new(Vec::new())); + let sampler = BinarySampler { + left: Arc::new(Input { + name: "left", + value: 3.0, + fail: left_fails, + calls: calls.clone(), + }), + right: Arc::new(Input { + name: "right", + value: 7.0, + fail: true, + calls: calls.clone(), + }), + operation: BinaryOp::Multiply, + }; + let mut context = SamplerContext::default(); + let mut wrong_size = [17.0; 2]; + assert!( + sampler + .sample_volume(&mut context, &mut wrong_size, volume) + .is_err() + ); + assert_eq!(wrong_size, [17.0; 2]); + assert!(calls.lock().unwrap().is_empty()); + let mut output = [17.0]; + assert_eq!( + sampler.sample_volume(&mut context, &mut output, volume), + Err(SampleError::ZeroGradientPeriod) + ); + assert_eq!(output, [3.0]); + assert_eq!( + *calls.lock().unwrap(), + if left_fails { + vec!["left"] + } else { + vec!["left", "right"] + } + ); + } +} diff --git a/crates/horizon-core/src/noise/perlin.rs b/crates/horizon-core/src/noise/perlin.rs index 101ded427..725373eff 100644 --- a/crates/horizon-core/src/noise/perlin.rs +++ b/crates/horizon-core/src/noise/perlin.rs @@ -40,18 +40,29 @@ impl PerlinNoise { } fn gradients(&self, cell: [i32; 3]) -> [[i32; 3]; 8] { - let p = self.table.permute(cell[0]); - let q = self.table.permute(cell[0].wrapping_add(1)); - let lower = self.table.permute(p.wrapping_add(cell[1])); - let upper = self.table.permute(p.wrapping_add(cell[1]).wrapping_add(1)); - let lower_x = self.table.permute(q.wrapping_add(cell[1])); - let upper_x = self.table.permute(q.wrapping_add(cell[1]).wrapping_add(1)); + self.gradients_at( + self.table.permute(cell[0]), + self.table.permute(cell[0].wrapping_add(1)), + cell[1], + cell[2], + ) + } + + /// Corner gradients for a Y cell, with the X column's permutation already + /// resolved. `add_volume` reuses one X column across every Y, so hoisting + /// the two X permutes out of that loop avoids repeating them per Y. + #[inline] + fn gradients_at(&self, p: i32, q: i32, cell_y: i32, cell_z: i32) -> [[i32; 3]; 8] { + let lower = self.table.permute(p.wrapping_add(cell_y)); + let upper = self.table.permute(p.wrapping_add(cell_y).wrapping_add(1)); + let lower_x = self.table.permute(q.wrapping_add(cell_y)); + let upper_x = self.table.permute(q.wrapping_add(cell_y).wrapping_add(1)); let rows = [ lower, lower_x, upper, upper_x, lower, lower_x, upper, upper_x, ]; std::array::from_fn(|i| { self.table - .gradient(rows[i].wrapping_add(cell[2]).wrapping_add((i / 4) as i32)) + .gradient(rows[i].wrapping_add(cell_z).wrapping_add((i / 4) as i32)) }) } @@ -166,6 +177,8 @@ impl PerlinNoise { let cell_x = floor_i32(shifted_x); let dx = (shifted_x - f64::from(cell_x)) as f32; let fade_x = smoothstep(dx); + let p = self.table.permute(cell_x); + let q = self.table.permute(cell_x.wrapping_add(1)); let mut previous_y = i32::MIN; let mut xz_dots = [0.0_f32; 8]; let mut y_gradients = [0.0_f32; 8]; @@ -176,7 +189,7 @@ impl PerlinNoise { let dy = shifted_y - f64::from(cell_y); let fade_y = smoothstep(dy as f32); if previous_y != cell_y { - let gradients = self.gradients([cell_x, cell_y, cell_z]); + let gradients = self.gradients_at(p, q, cell_y, cell_z); for i in 0..8 { xz_dots[i] = gradients[i][0] as f32 * (dx - (i & 1) as f32) + gradients[i][2] as f32 * (dz - ((i >> 2) & 1) as f32); diff --git a/docs/noise-aquifer-benchmark.md b/docs/noise-aquifer-benchmark.md index a9634c743..e14645247 100644 --- a/docs/noise-aquifer-benchmark.md +++ b/docs/noise-aquifer-benchmark.md @@ -1,13 +1,51 @@ -# Bulk unary density evaluation benchmark +# Bulk density and noise generation benchmark -Dispatching unary operations once per bulk buffer reduced initialized Overworld terrain generation from 12.271 to 10.566 ms/input chunk (13.89%) with one worker and from 3.838 to 3.408 (11.19%) with four. Terrain plus decoration improved 11.70% and 9.48%. A second seed/site improved terrain 13.66% and 13.70%. These are incremental results against baseline `e4f8154cb4115aa1ee622381d9b12387cdf515d4`. +The PR dispatches unary and binary density operations once per bulk buffer, resolves the shared Beardifier kernel once per sampled position, and reuses Perlin X-column permutations across the Y loop. Binary bit-exact tests cover every operation, distinct NaN payloads, signed zeros, infinities, subnormals, random float pairs and SIMD/tail-sized buffers. Child-failure tests retain left/right evaluation order and partial output. Scalar shortcuts, arithmetic expressions, RNG consumption and generation traversal are preserved. -Each case used one discarded pair and five measured alternating baseline/candidate pairs. The primary case used seed 12345, center (0,0), 256 terrain chunks and 196 decorated interior chunks. An offset 9×9 generation pass initialized the seeded generator without pre-generating target chunks. The second site used seed -1 at (32,-32). Timing is complete terrain wall time divided by input count; combined results retain border work. Cold initialization was measured separately. +## Whole-terrain measurements -Nether terrain medians improved 3.15% / 2.03% with one/four workers; End improved 3.03% / 5.39%. Some individual Nether/End pairs regressed or had large favorable outliers. Gains are workload-dependent and should not be generalized to every seed, biome, compiler, or platform. Peak RSS was effectively unchanged and no new heap buffer was introduced. +**Times below are complete terrain wall elapsed divided by 256 input chunks, with no thread multiplier.** Four-worker figures are elapsed latency/throughput measurements, not per-thread CPU costs. Combined time adds the complete decoration phase, still divided by 256; 196 interior chunks are decorated with unchanged border work. At the primary site, 256 terrain chunks take about 2.578 seconds with one worker versus 0.860 seconds with four: approximately 3.00× throughput, or 75% of ideal fourfold scaling. Two/eight-worker behavior was not measured. -Measured on AMD Ryzen 5 5600X, 32 GiB RAM, Linux, Rust 1.99.0, optimized `fast` profile (release optimization, LTO off, 16 codegen units). Builds and measurements ran sequentially; background desktop activity was not isolated. The benchmark used the existing chunkgen benchmark with identical optional initialization and exact elapsed-time instrumentation on both revisions. `crates/horizon-world/examples/chunkgen_bench.rs` provides the standard workload entry point; its human-readable thread-multiplied ms/chunk display must not be mistaken for elapsed/count. +Fresh equivalent optimized builds compare baseline `e4f8154cb4115aa1ee622381d9b12387cdf515d4` with the extended candidate. Every case has one discarded process pair, then five measured pairs alternating baseline/candidate order. Initialized processes first generate and discard the same offset 9×9 region at center+(64,64), using the same seeded generator, before timing fresh measured workers. This excludes lazy ring initialization without pre-generating target chunks. All cases use size 16; primary seed/site is 12345/(0,0), second Overworld seed/site is -1/(32,-32). -Validation passed: 191 targeted Rust tests; fresh pinned Java client/server density parity (15,524,627 lines each); terrain-fill parity (114 fixtures, 200 fills, 7,950,873 lines per Java side); and 24 exact terrain/decorated region comparisons across three dimensions, two seeds/sites, and one/four workers. Snapshots included block states and palettes, quart biomes, entities/block entities, scheduled ticks, all six heightmaps, ordered postprocessing, non-air counts, and chunk metadata. This establishes parity within the declared corpus, not complete gameplay parity. +Medians below are baseline → extended candidate, ms/input chunk, followed by percent reduction. -The repository quick PR check also passed public-tree validation, Python tests, formatting, clippy, and core tests. Generated timing files, diagnostic runners, patches, and validation logs are retained locally outside the repository and are not part of this PR. +- **overworld, 1 worker(s):** terrain 12.685400 → 10.069365 (20.62%); combined 14.696298 → 12.105007 (17.63%). +- **overworld, 4 worker(s):** terrain 4.045564 → 3.359536 (16.96%); combined 4.663124 → 3.944952 (15.40%). +- **the_nether, 1 worker(s):** terrain 5.088375 → 4.848149 (4.72%); combined 5.481617 → 5.239165 (4.42%). +- **the_nether, 4 worker(s):** terrain 1.446298 → 1.366468 (5.52%); combined 1.574890 → 1.472913 (6.48%). +- **the_end, 1 worker(s):** terrain 1.085323 → 0.980046 (9.70%); combined 1.161651 → 1.053080 (9.35%). +- **the_end, 4 worker(s):** terrain 0.281733 → 0.259724 (7.81%); combined 0.312280 → 0.288982 (7.46%). +- **overworld, 1 worker(s), second site:** terrain 11.585829 → 9.079823 (21.63%); combined 12.650823 → 10.145760 (19.80%). +- **overworld, 4 worker(s), second site:** terrain 3.105819 → 2.519504 (18.88%); combined 3.410155 → 2.812551 (17.52%). +- **overworld, 1 worker(s), non-initialized generator:** terrain 23.429316 → 20.942006 (10.62%); combined 25.599745 → 23.005476 (10.13%). + +The non-initialized case retains lazy startup work and is separate from recurring-cost qualification; resource loading is outside all terrain brackets. All 45 measured terrain pairs improved, but Nether one-worker median terrain improvement remains below 5%. Results are workload-dependent. The primary one-worker noise bracket improves 5.901774 → 4.773963 ms/chunk (19.11%); shared bulk material-density work also improves, with surface 4.576660 → 3.181641 (30.48%). Whole-terrain gains must not be described as noise-stage-only gains. Parallel stage brackets are aggregate worker elapsed and are not exclusive CPU shares. + +## Added benefit beyond unary dispatch + +A separate five-pair alternating comparison uses the existing unary-only PR revision `594b50278044df6b72b641cd02d5050925c86203` as baseline. It uses the same initialized primary workload and fresh builds. These numbers measure the binary/Beardifier/Perlin additions together; they do not assign a separate speedup to each addition. + +- **1 worker(s):** terrain 10.913267 → 10.034900 (8.05%); combined 12.867017 → 12.079432 (6.12%). +- **4 worker(s):** terrain 3.536831 → 3.312338 (6.35%); combined 4.110268 → 3.897104 (5.19%). + +All ten incremental terrain pairs improved. Pair reductions ranged 4.72–9.93% with one worker and 2.10–8.61% with four, so not every individual pair exceeds 5%. A preliminary gate was excluded in its entirety because a concurrent Horizon client consumed roughly seven cores. Its raw samples remain in `contended-gate/`; all qualification runs began after the client was closed. No individual samples were selectively removed from the restarted runs. + +## Machine and validation + +AMD Ryzen 5 5600X, 6 cores/12 logical CPUs, 32 GiB RAM, Linux, Rust 1.99.0. Optimized `fast` profile: release optimization, LTO disabled, 16 codegen units. Builds and timed processes were sequential; ordinary desktop activity was not isolated. Primary terrain process CPU medians are 12.4 → 10.0 ms/input chunk (one worker) and 15.9 → 13.2 (four), using coarse 100 Hz accounting. Peak RSS is effectively unchanged: 786288 → 786256 KiB and 833496 → 833528 KiB. No new heap buffer is introduced. Executable text increases 1804 bytes against the original baseline and decreases 1056 bytes against the unary-only revision. + +Validation of the extended candidate passed: + +- 193 targeted optimized Rust unit/integration tests, including binary floating-point and failure-order tests, density/world fields, storage/palettes, material/carving stages, biome regions, retrogen and ticks. +- Fresh pinned Java client/server density parity: 15,524,627 output lines per side. +- Dedicated noise parity: 1,443,539 lines per side, covering Perlin/smeared bulk paths, strided/wrapped volumes, buffer tails, constructor RNG and derivatives. +- Dedicated Beardifier/structure-density parity: 183 fixtures, 1,360,095 lines per side. +- Terrain-fill parity: 114 fixtures, 200 fills, 7,950,873 lines per side, including prefilled chunks, repeated fills, partial vertical volumes, aquifers, WG heightmaps and ordered postprocessing. +- 24/24 exact region phase comparisons: three dimensions × two seeds/sites × one/four workers, each with 81 retained chunks and 49 interior decorations. Every retained chunk is compared before and after decoration, including borders. Snapshots include raw/public block reads and palettes, quart biomes, entities/block entities as binary NBT, complete scheduled ticks, all six heightmaps, ordered postprocessing, actual non-air counts and chunk metadata. Temporary diagnostic accessors were removed from production source. + +This validates the declared corpora and sites, not every possible seed, biome or platform. Lighting, meshing, saving, streaming and full application startup were not benchmarked. + +The existing `crates/horizon-world/examples/chunkgen_bench.rs` is the standard workload entry point. Both performance builds used the same saved diagnostic source adding optional generator initialization and exact elapsed/count output; its thread-multiplied human display was not used. Diagnostic builds use `CARGO_TARGET_DIR=/srv/bob-data/horizon-stage3d-target cargo build --profile fast -p horizon-world --example chunkgen_profile`. Raw timings, exact commands, binary/source hashes, scripts and parity outputs are retained outside Git at `/srv/bob-data/horizon-noise-aquifer-e477fa56/extension/`: `gate-raw.json`, `matrix-raw.json`, `summary.json`, `marginal/marginal-raw.json`, `binary-hashes.json`, and the `*-parity.json` reports. Prepared resources are `/srv/bob-data/horizon/.local/horizon-client/26.3`. Generated artifacts and private fixtures remain outside the PR. + +The repository quick PR self-check also passes: public-tree validation, workspace rustfmt, workspace/all-target clippy with warnings denied, and 96 full core-crate tests. Relevant pinned Java suites were run separately as listed above. A production-only rebuild after removing diagnostic support has byte-identical executable instructions and read-only constants to the measured candidate.