[PR #7437] [CLOSED] density: fuse compiled arithmetic runs into single op-list samplers #7604

Closed
opened 2026-11-03 07:20:37 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon/pull/7437
Author: @socksprox
Created: 11/2/2026
Status: ❌ Closed

Base: main ← Head: density-fused-eval


📝 Commits (1)

  • c6d9978 density: fuse compiled arithmetic runs into single op-list samplers

📊 Changes

7 files changed (+937 additions, -9 deletions)

View changed files

📝 crates/horizon-core/src/density/arithmetic.rs (+8 -2)
➕ crates/horizon-core/src/density/fused.rs (+895 -0)
📝 crates/horizon-core/src/density/graph/compiler.rs (+3 -2)
📝 crates/horizon-core/src/density/mod.rs (+11 -0)
📝 crates/horizon-core/src/density/power.rs (+3 -0)
📝 crates/horizon-core/src/density/rounding.rs (+4 -1)
📝 crates/horizon-core/src/density/selection.rs (+13 -4)

📄 Description

🤖 Opened by socksprox's AI agent (Devin, SWE-2 High), not by socksprox personally.

Summary

Closes #7341 (claimed; the design questions there are folded into this implementation — happy to adjust if you'd rather see a different shape).

Compiled density functions evaluate as Arc<dyn DensitySampler> trees with a virtual call, a full buffer pass, and a scratch buffer per operand at every interior node — the main per-node cost in chunk-gen's sample_volume hot path (worldgen/terrain.rs, generator.rs, climate.rs, aquifer/, material_condition/).

This adds density/fused.rs: a post-order op-list evaluator (FusedSampler) that flattens maximal runs of pointwise-arithmetic samplers — UnarySampler, BinarySampler, PowerSampler, RoundSampler, LerpSampler, RangeChoiceSampler, IntervalSelectSampler, ConstantSampler — into one sample_volume pass over shared buffer slots. DensityCompiler wraps it around compile_raw results bottom-up, so regions are maximal and non-fusible nodes (noise, splines, slices, caches, interpolation, surface search, world fields, foreign DensitySampler impls) become leaf slots. Regions of <2 combining ops stay as plain nodes; DensityGraph interning/equality is untouched.

Bit-exactness is structural, not tuned:

  • Ops are emitted in the tree's post-order → leaf fills happen in identical left-to-right order (error ordering, cache cells, counted-sampler probes preserved).
  • In-place operands share their parent's slot, so the strictest buffer contract (check_exact_buffer vs check_buffer) propagates along the output spine, and tail writes into oversized caller buffers land exactly where the tree put them (UnaryOp::apply_bulk covers the full slot; binary/power ops zip at volume.size()).
  • Combine ops call the same apply_bulk/java_power/interpolate/RoundMode helpers — bulk vs scalar min/max split untouched, no FMA, no reassociation.
  • sample_value delegates to the retained subtree, preserving lazy branches (skipped child errors + cache writes stay skipped). Degenerate/oversized volumes also fall back to the subtree.

Not a vanilla port — this is a Horizon-internal evaluation-path optimization; the observable contract stays the compiled-sampler semantics already pinned by density_parity.

Test plan

  • New differential tests in fused.rs: 400 random trees + 200 fused-child merges evaluated tree-vs-fused bit-for-bit across volumes, oversized tails, scalar calls, and the error contract
  • cargo test -p horizon-core --lib — 123 passed
  • CI parity: tools/parity.py --changed-files selects density, worldgen, worldgen_random, datapack_worldgen, structure_density et al. for these paths (no .local/ here, so not run locally)

pr_ready

**pr_ready** (`tools/pr_ready.py --quick` on MacBookPro, against origin/main at fdd290b99, 3 min 16 s)

| Check | Result |
| --- | --- |
| Scope | ⚠️ 7 files, +937 −9; 946 lines of code: over 500, split it if it can ship in parts |
| Cargo.lock | ✅ unchanged |
| Public tree | ✅ |
| Python tests | ✅ 612 passed (1 min 13 s) |
| rustfmt | ✅ (7 s) |
| clippy | ✅ (1 min 40 s) |
| tests: horizon-core | ✅ 131 passed (15 s) |
| doc tests | ✅ 0 passed |
| content root tests | ➖ content root untouched |
| mod API up to date | ➖ native hooks untouched |
| tokens.css up to date | ➖ theme untouched |
| launcher rustfmt | ➖ launcher untouched |
| launcher clippy | ➖ launcher untouched |
| launcher tests | ➖ launcher untouched |
| Parity | ➖ not run (--quick): CI's parity job picks the affected harnesses |
| Label | player-facing: `feature` if players get something new, no label for a fix (`release-now` if urgent) |

Scope note: ~900 lines because the op list is one atomic change (~600 impl incl. doc comments + ~300 differential tests); it doesn't split into independently shippable halves without dead code.

🤖 Generated with Devin


🔄 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/7437 **Author:** [@socksprox](https://github.com/socksprox) **Created:** 11/2/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `density-fused-eval` --- ### 📝 Commits (1) - [`c6d9978`](https://github.com/horizon-rust/horizon/commit/c6d99786ed46c6d33abe9637e2e08b2fa36e0879) density: fuse compiled arithmetic runs into single op-list samplers ### 📊 Changes **7 files changed** (+937 additions, -9 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-core/src/density/arithmetic.rs` (+8 -2) ➕ `crates/horizon-core/src/density/fused.rs` (+895 -0) 📝 `crates/horizon-core/src/density/graph/compiler.rs` (+3 -2) 📝 `crates/horizon-core/src/density/mod.rs` (+11 -0) 📝 `crates/horizon-core/src/density/power.rs` (+3 -0) 📝 `crates/horizon-core/src/density/rounding.rs` (+4 -1) 📝 `crates/horizon-core/src/density/selection.rs` (+13 -4) </details> ### 📄 Description > 🤖 Opened by socksprox's AI agent (Devin, SWE-2 High), not by socksprox personally. ## Summary Closes #7341 (claimed; the design questions there are folded into this implementation — happy to adjust if you'd rather see a different shape). Compiled density functions evaluate as `Arc<dyn DensitySampler>` trees with a virtual call, a full buffer pass, and a scratch buffer per operand at every interior node — the main per-node cost in chunk-gen's `sample_volume` hot path (`worldgen/terrain.rs`, `generator.rs`, `climate.rs`, `aquifer/`, `material_condition/`). This adds `density/fused.rs`: a post-order op-list evaluator (`FusedSampler`) that flattens maximal runs of pointwise-arithmetic samplers — `UnarySampler`, `BinarySampler`, `PowerSampler`, `RoundSampler`, `LerpSampler`, `RangeChoiceSampler`, `IntervalSelectSampler`, `ConstantSampler` — into one `sample_volume` pass over shared buffer slots. `DensityCompiler` wraps it around `compile_raw` results bottom-up, so regions are maximal and non-fusible nodes (noise, splines, slices, caches, interpolation, surface search, world fields, foreign `DensitySampler` impls) become leaf slots. Regions of <2 combining ops stay as plain nodes; `DensityGraph` interning/equality is untouched. **Bit-exactness is structural, not tuned:** - Ops are emitted in the tree's post-order → leaf fills happen in identical left-to-right order (error ordering, cache cells, counted-sampler probes preserved). - In-place operands share their parent's slot, so the strictest buffer contract (`check_exact_buffer` vs `check_buffer`) propagates along the output spine, and tail writes into oversized caller buffers land exactly where the tree put them (`UnaryOp::apply_bulk` covers the full slot; binary/power ops zip at `volume.size()`). - Combine ops call the same `apply_bulk`/`java_power`/`interpolate`/`RoundMode` helpers — bulk vs scalar min/max split untouched, no FMA, no reassociation. - `sample_value` delegates to the retained subtree, preserving lazy branches (skipped child errors + cache writes stay skipped). Degenerate/oversized volumes also fall back to the subtree. Not a vanilla port — this is a Horizon-internal evaluation-path optimization; the observable contract stays the compiled-sampler semantics already pinned by `density_parity`. ## Test plan - [x] New differential tests in `fused.rs`: 400 random trees + 200 fused-child merges evaluated tree-vs-fused bit-for-bit across volumes, oversized tails, scalar calls, and the error contract - [x] `cargo test -p horizon-core --lib` — 123 passed - [ ] CI parity: `tools/parity.py --changed-files` selects `density`, `worldgen`, `worldgen_random`, `datapack_worldgen`, `structure_density` et al. for these paths (no `.local/` here, so not run locally) ## pr_ready ``` **pr_ready** (`tools/pr_ready.py --quick` on MacBookPro, against origin/main at fdd290b99, 3 min 16 s) | Check | Result | | --- | --- | | Scope | ⚠️ 7 files, +937 −9; 946 lines of code: over 500, split it if it can ship in parts | | Cargo.lock | ✅ unchanged | | Public tree | ✅ | | Python tests | ✅ 612 passed (1 min 13 s) | | rustfmt | ✅ (7 s) | | clippy | ✅ (1 min 40 s) | | tests: horizon-core | ✅ 131 passed (15 s) | | doc tests | ✅ 0 passed | | content root tests | ➖ content root untouched | | mod API up to date | ➖ native hooks untouched | | tokens.css up to date | ➖ theme untouched | | launcher rustfmt | ➖ launcher untouched | | launcher clippy | ➖ launcher untouched | | launcher tests | ➖ launcher untouched | | Parity | ➖ not run (--quick): CI's parity job picks the affected harnesses | | Label | player-facing: `feature` if players get something new, no label for a fix (`release-now` if urgent) | ``` Scope note: ~900 lines because the op list is one atomic change (~600 impl incl. doc comments + ~300 differential tests); it doesn't split into independently shippable halves without dead code. 🤖 Generated with [Devin](https://devin.ai) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-03 07:20:37 +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#7604
No description provided.