[PR #768] [CLOSED] Density arithmetic: match the bulk operation once, not per element #2795

Closed
opened 2026-11-02 15:59:06 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon/pull/768
Author: @rdbtCVS
Created: 10/17/2026
Status: ❌ Closed

Base: main ← Head: perf/density-bulk-arithmetic-20261017


📝 Commits (1)

  • 190befc Density arithmetic: match the bulk operation once, not per element

📊 Changes

3 files changed (+211 additions, -18 deletions)

View changed files

📝 crates/horizon-core/src/density/arithmetic.rs (+183 -6)
📝 crates/horizon-core/src/density/beardifier.rs (+7 -4)
📝 crates/horizon-core/src/noise/perlin.rs (+21 -8)

📄 Description

Opened by rdbt's AI agent (opencode), not by rdbt himself.

What and why

The volume ("bulk") paths of the density arithmetic samplers re-dispatched on the operation enum for every element, so the loop could not vectorize. UnaryOp::apply/BinaryOp::apply are called once per element inside UnarySampler::sample_volume/BinarySampler::sample_volume; the enum is loop-invariant but LLVM never hoisted the match out.

Matching the operation once, outside the loop (UnaryOp::apply_volume, BinaryOp::apply_volume), lets each operation become a tight, vectorizable loop. The occasional operations (Sqrt, Log, Power*, Reciprocal, Sign, Squeeze, LeakyRelu) keep falling back to the shared elementwise implementation.

The element results are bit-identical to the previous bulk path: apply_volume mirrors apply(value, true) arm for arm, and two new unit tests (applies_volume_like_elementwise, applies_binary_volume_like_elementwise) pin that for every operation over -inf, -0.0, 0.0, inf, NaN and finite values. The bulk flag still distinguishes the bulk comparison semantics from the scalar java_min/java_max ones; that is unchanged.

Two smaller, strictly-fewer-operations changes while profiling the same stage:

  • Beardifier::sample_unchecked resolved the process-wide kernel() OnceLock once per piece and once per junction; it is now resolved once per sample (the array reference is threaded into beard).
  • PerlinNoise::add_volume recomputed the two X-column permutations p = permute(cell_x), q = permute(cell_x + 1) for every Y cell even though cell_x is fixed for the column. They are now hoisted out of the Y loop (gradients_at). No measurable change on its own; kept because it is strictly less work in the recompute branch.

InterpolatedSampler::sample_block_volume was profiled too but left alone: its inner column loop is a serial value += increment (repeated addition is observable to match vanilla's rounding), so a rewrite I tried was ~6% slower and I reverted it.

Vanilla reference

None needed: no behaviour change. The vanilla float semantics (bulk min/max taking the operand rather than Math.min/max, repeated-addition interpolation, wrap/floor coordinate reduction) are preserved exactly; apply_volume only changes how the same operations are scheduled. Run through cargo test -p horizon-core (94 pass, including the existing scalar_shortcuts_and_bulk_arithmetic_keep_distinct_results).

Numbers

Isolated bulk ops, on-CPU min of many rounds (host under load, so wall time is useless), --profile release, 98304 elements, ConstantSampler(0.3) input:

operation before after
Multiply(4.0) 0.158 ms 0.015 ms
Clamp(-1, 1) 0.199 ms 0.027 ms
Minimum(0.25) 0.179 ms 0.047 ms
Negate / Abs / Square 0.16–0.28 ms 0.014–0.07 ms

chunkgen_bench (seed 1234, 256 chunks, 1 thread, --no-decorate, best of 5 alternating base/new runs, busy host load ~35):

stage before after
noise 5.18 ms/chunk 4.30 ms/chunk (−17%)
surface 3.93 ms/chunk 2.84 ms/chunk (−28%)
carvers 0.47 ms/chunk 0.48 ms/chunk
cpu time 22.5 ms/chunk 20.4 ms/chunk (−9%)

The noise stage is where these samplers run; the surface (material rules) pass also samples density volumes, and both drop well past 10%. Total per-chunk CPU (which includes work outside the four timed stages, and with decoration off here) is ~9–11% depending on run.

Parity

Ran the scoped harnesses locally against the checksum-pinned vanilla 26.3 client + server JARs; every output line matches:

harness client server
tools/test_noise_parity.py 1,443,539 ✅ 1,443,539 ✅
tools/test_density_parity.py — 15,524,627 ✅
tools/test_structure_density_parity.py 1,360,095 ✅ 1,360,095 ✅
tools/test_terrain_parity.py 7,950,873 ✅ 7,950,873 ✅
tools/test_worldgen_parity.py 961,693 ✅ 961,693 ✅

These cover exactly the samplers this PR touches (base noise, density arithmetic, beardifier, terrain fill) plus a full worldgen codec round-trip.

Self-check

**pr_ready** (`tools/pr_ready.py` on archlinux, against origin/main at dc1a8a4f3, 6 s)

| Check | Result |
| --- | --- |
| Scope | ✅ 3 files, +211 −18 |
| Cargo.lock | ✅ unchanged |
| Public tree | ✅ |
| Python tests | ➖ no Python change |
| rustfmt | ✅ (5 s) |
| clippy | ✅ |
| tests: horizon-core | ✅ 94 passed (1 s) |
| tokens.css up to date | ➖ theme untouched |
| launcher rustfmt | ➖ launcher untouched |
| launcher clippy | ➖ launcher untouched |
| launcher tests | ➖ launcher untouched |
| Parity | ✅ ran locally, all match (see Parity above) |
| Label | no label (a fix, no player-facing change) |

Screenshots

None: no player-visible change.

Please tick "Allow edits by maintainers" so fixups can be pushed without a round trip.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/horizon-rust/horizon/pull/768 **Author:** [@rdbtCVS](https://github.com/rdbtCVS) **Created:** 10/17/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `perf/density-bulk-arithmetic-20261017` --- ### 📝 Commits (1) - [`190befc`](https://github.com/horizon-rust/horizon/commit/190befce50bde6474a0a5b892215740645dd04f3) Density arithmetic: match the bulk operation once, not per element ### 📊 Changes **3 files changed** (+211 additions, -18 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-core/src/density/arithmetic.rs` (+183 -6) 📝 `crates/horizon-core/src/density/beardifier.rs` (+7 -4) 📝 `crates/horizon-core/src/noise/perlin.rs` (+21 -8) </details> ### 📄 Description Opened by rdbt's AI agent (opencode), not by rdbt himself. ## What and why The volume ("bulk") paths of the density arithmetic samplers re-dispatched on the operation enum for every element, so the loop could not vectorize. `UnaryOp::apply`/`BinaryOp::apply` are called once per element inside `UnarySampler::sample_volume`/`BinarySampler::sample_volume`; the enum is loop-invariant but LLVM never hoisted the match out. Matching the operation **once**, outside the loop (`UnaryOp::apply_volume`, `BinaryOp::apply_volume`), lets each operation become a tight, vectorizable loop. The occasional operations (`Sqrt`, `Log`, `Power*`, `Reciprocal`, `Sign`, `Squeeze`, `LeakyRelu`) keep falling back to the shared elementwise implementation. The element results are bit-identical to the previous bulk path: `apply_volume` mirrors `apply(value, true)` arm for arm, and two new unit tests (`applies_volume_like_elementwise`, `applies_binary_volume_like_elementwise`) pin that for every operation over `-inf`, `-0.0`, `0.0`, `inf`, `NaN` and finite values. The `bulk` flag still distinguishes the bulk comparison semantics from the scalar `java_min`/`java_max` ones; that is unchanged. Two smaller, strictly-fewer-operations changes while profiling the same stage: - `Beardifier::sample_unchecked` resolved the process-wide `kernel()` `OnceLock` once per piece and once per junction; it is now resolved once per sample (the array reference is threaded into `beard`). - `PerlinNoise::add_volume` recomputed the two X-column permutations `p = permute(cell_x)`, `q = permute(cell_x + 1)` for every Y cell even though `cell_x` is fixed for the column. They are now hoisted out of the Y loop (`gradients_at`). No measurable change on its own; kept because it is strictly less work in the recompute branch. `InterpolatedSampler::sample_block_volume` was profiled too but left alone: its inner column loop is a serial `value += increment` (repeated addition is observable to match vanilla's rounding), so a rewrite I tried was ~6% slower and I reverted it. ## Vanilla reference None needed: no behaviour change. The vanilla float semantics (bulk `min`/`max` taking the operand rather than `Math.min`/`max`, repeated-addition interpolation, wrap/floor coordinate reduction) are preserved exactly; `apply_volume` only changes how the same operations are scheduled. Run through `cargo test -p horizon-core` (94 pass, including the existing `scalar_shortcuts_and_bulk_arithmetic_keep_distinct_results`). ## Numbers Isolated bulk ops, on-CPU min of many rounds (host under load, so wall time is useless), `--profile release`, 98304 elements, `ConstantSampler(0.3)` input: | operation | before | after | |---|---:|---:| | `Multiply(4.0)` | 0.158 ms | 0.015 ms | | `Clamp(-1, 1)` | 0.199 ms | 0.027 ms | | `Minimum(0.25)` | 0.179 ms | 0.047 ms | | `Negate` / `Abs` / `Square` | 0.16–0.28 ms | 0.014–0.07 ms | `chunkgen_bench` (seed 1234, 256 chunks, 1 thread, `--no-decorate`, best of 5 alternating base/new runs, busy host load ~35): | stage | before | after | |---|---:|---:| | noise | 5.18 ms/chunk | 4.30 ms/chunk (−17%) | | surface | 3.93 ms/chunk | 2.84 ms/chunk (−28%) | | carvers | 0.47 ms/chunk | 0.48 ms/chunk | | **cpu time** | **22.5 ms/chunk** | **20.4 ms/chunk (−9%)** | The `noise` stage is where these samplers run; the `surface` (material rules) pass also samples density volumes, and both drop well past 10%. Total per-chunk CPU (which includes work outside the four timed stages, and with decoration off here) is ~9–11% depending on run. ## Parity Ran the scoped harnesses locally against the checksum-pinned vanilla 26.3 client + server JARs; every output line matches: | harness | client | server | |---|---:|---:| | `tools/test_noise_parity.py` | 1,443,539 ✅ | 1,443,539 ✅ | | `tools/test_density_parity.py` | — | 15,524,627 ✅ | | `tools/test_structure_density_parity.py` | 1,360,095 ✅ | 1,360,095 ✅ | | `tools/test_terrain_parity.py` | 7,950,873 ✅ | 7,950,873 ✅ | | `tools/test_worldgen_parity.py` | 961,693 ✅ | 961,693 ✅ | These cover exactly the samplers this PR touches (base noise, density arithmetic, beardifier, terrain fill) plus a full worldgen codec round-trip. ## Self-check ``` **pr_ready** (`tools/pr_ready.py` on archlinux, against origin/main at dc1a8a4f3, 6 s) | Check | Result | | --- | --- | | Scope | ✅ 3 files, +211 −18 | | Cargo.lock | ✅ unchanged | | Public tree | ✅ | | Python tests | ➖ no Python change | | rustfmt | ✅ (5 s) | | clippy | ✅ | | tests: horizon-core | ✅ 94 passed (1 s) | | tokens.css up to date | ➖ theme untouched | | launcher rustfmt | ➖ launcher untouched | | launcher clippy | ➖ launcher untouched | | launcher tests | ➖ launcher untouched | | Parity | ✅ ran locally, all match (see Parity above) | | Label | no label (a fix, no player-facing change) | ``` ## Screenshots None: no player-visible change. Please tick "Allow edits by maintainers" so fixups can be pushed without a round trip. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 15:59:06 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
horizon-rust/horizon#2795
No description provided.