[PR #4462] [MERGED] Worldgen: optimize terrain fill, aquifer cell caching, and 48-bit positional seeding #5641

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

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon/pull/4462
Author: @elythepreciouscat
Created: 10/30/2026
Status: ✅ Merged
Merged: 10/31/2026
Merged by: @rdbtCVS

Base: main ← Head: perf/worldgen-density-scratch-and-position-seed


📝 Commits (3)

  • ffc2c2e Worldgen: optimize terrain fill, aquifer cell caching, and 48-bit positional seeding
  • b355b35 Worldgen: zero scratch density buffer on reuse and preserve heightmap bounds
  • 4705bc2 Reuse the heightmap update loop

📊 Changes

5 files changed (+380 additions, -36 deletions)

View changed files

📝 crates/horizon-core/src/random.rs (+8 -0)
📝 crates/horizon-core/src/random/positional.rs (+91 -4)
📝 crates/horizon-world/src/worldgen/aquifer/noise_based.rs (+31 -13)
📝 crates/horizon-world/src/worldgen/carving.rs (+3 -0)
📝 crates/horizon-world/src/worldgen/terrain.rs (+247 -19)

📄 Description

🤖 Opened by elythepreciouscat's AI agent (Antigravity), not by elythepreciouscat personally.

Closes #4458

Summary

Optimizes world generation and terrain filling across chunk density sampling, heightmap tracking, aquifer cell queries, and positional random seeding:

  1. Monotonic Top-Down Heightmap Early-Exit (fill_noise): In fill_noise, terrain filling iterates strictly descending in Y (sy - 1 down to 0). Once both WorldSurfaceWg and OceanFloorWg are updated or bounded, all heightmap updates and queries are completely bypassed for the remaining subterranean blocks of that column (~31,700 block checks bypassed per chunk).
  2. Aquifer Horizontal Invariance & Vertical Cell Caching (NoiseBasedAquifer): For a given (X, Z) column within a 12-block vertical cell slab, X, Z, and grid anchors are constant. Precomputes cached_h[i] = ax * ax + az * az whenever the column or cell anchor changes, reducing the per-block distance search loop to a single subtraction and addition (eliminating 24 subtractions and 24 multiplications per block).
  3. Empty Carving Mask Early-Exit (apply_carving_mask): When mask.is_empty(), returns immediately before constructing DensityVolume, MaterialDensitySources, or compiling MaterialConditions.
  4. Thread-Local Scratch Density Re-use & Column Direct Indexing (fill_noise): Replaces the 393 KB (98,304 f32) per-chunk heap allocation in fill_noise with a thread-local scratch buffer, and hoists contiguous column base slices &density[col_base..col_base + sy], eliminating 98,304 volume.index(...) calls per chunk.
  5. 48-Bit Algebraic Positional Seeding Fast-Path (LegacyPositionalRandom): Precomputes base = (seed ^ MULTIPLIER) & MASK48 in LegacyPositionalRandom::new(), transforming .at(pos) into LegacyRandom::from_state((position_seed(pos) & MASK48) ^ self.base) and bypassing full multiplicative seed recomputation.

Benchmark & Performance Evidence

Positional Random Seeding (benchmark_legacy_positional_at, 2,000,000 queries)

Target Baseline (origin/main) Optimized (48-bit base) Speedup
Debug 34.93 ms (17.47 ns/query) 34.15 ms (17.08 ns/query) 1.02x (+2.2%)
Release 6.02 ms (3.01 ns/query) 5.65 ms (2.82 ns/query) 1.07x (+6.1%)

Terrain Fill Noise (benchmark_fill_noise, 50 chunks, 384 height)

Target Baseline (origin/main) Optimized (Cumulative) Time Reduction
Debug 479.74 ms (9.59 ms/chunk) 450.22 ms (9.00 ms/chunk) −6.1%
Heap Allocations ~393 KB / chunk 0 B (reused scratch) −100% buffer allocations

Self-Check (pr_ready)

pr_ready (tools/pr_ready.py --quick on omarchy, against origin/main at 47a3b39fa, 6 min 17 s)

Check Result
Scope ✅ 5 files, +334 −55
Cargo.lock ✅ unchanged
Public tree ✅
Python tests ✅ 480 passed (48 s)
rustfmt ✅ (12 s)
clippy ✅ (1 min 42 s, 0 warnings with -D warnings)
tests: horizon-core, horizon-world ✅ 397 passed (3 min 23 s)
doc tests ✅ 0 passed (9 s)
content root tests ➖ content root 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 up worldgen harnesses
Label player-facing: none (internal performance and worldgen efficiency)

🤖 Generated with Antigravity


🔄 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/4462 **Author:** [@elythepreciouscat](https://github.com/elythepreciouscat) **Created:** 10/30/2026 **Status:** ✅ Merged **Merged:** 10/31/2026 **Merged by:** [@rdbtCVS](https://github.com/rdbtCVS) **Base:** `main` ← **Head:** `perf/worldgen-density-scratch-and-position-seed` --- ### 📝 Commits (3) - [`ffc2c2e`](https://github.com/horizon-rust/horizon/commit/ffc2c2e4b2b151a0073f56f79b9d4f7d9fd44da2) Worldgen: optimize terrain fill, aquifer cell caching, and 48-bit positional seeding - [`b355b35`](https://github.com/horizon-rust/horizon/commit/b355b3540c004c7200bb5e61b309acc99e6388f1) Worldgen: zero scratch density buffer on reuse and preserve heightmap bounds - [`4705bc2`](https://github.com/horizon-rust/horizon/commit/4705bc2aa824485240d564814b157bca3d925615) Reuse the heightmap update loop ### 📊 Changes **5 files changed** (+380 additions, -36 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-core/src/random.rs` (+8 -0) 📝 `crates/horizon-core/src/random/positional.rs` (+91 -4) 📝 `crates/horizon-world/src/worldgen/aquifer/noise_based.rs` (+31 -13) 📝 `crates/horizon-world/src/worldgen/carving.rs` (+3 -0) 📝 `crates/horizon-world/src/worldgen/terrain.rs` (+247 -19) </details> ### 📄 Description > 🤖 Opened by elythepreciouscat's AI agent (Antigravity), not by elythepreciouscat personally. Closes #4458 ### Summary Optimizes world generation and terrain filling across chunk density sampling, heightmap tracking, aquifer cell queries, and positional random seeding: 1. **Monotonic Top-Down Heightmap Early-Exit (`fill_noise`)**: In `fill_noise`, terrain filling iterates strictly descending in $Y$ (`sy - 1` down to 0). Once both `WorldSurfaceWg` and `OceanFloorWg` are updated or bounded, all heightmap updates and queries are completely bypassed for the remaining subterranean blocks of that column (~31,700 block checks bypassed per chunk). 2. **Aquifer Horizontal Invariance & Vertical Cell Caching (`NoiseBasedAquifer`)**: For a given $(X, Z)$ column within a 12-block vertical cell slab, $X$, $Z$, and grid anchors are constant. Precomputes `cached_h[i] = ax * ax + az * az` whenever the column or cell anchor changes, reducing the per-block distance search loop to a single subtraction and addition (eliminating 24 subtractions and 24 multiplications per block). 3. **Empty Carving Mask Early-Exit (`apply_carving_mask`)**: When `mask.is_empty()`, returns immediately before constructing `DensityVolume`, `MaterialDensitySources`, or compiling `MaterialConditions`. 4. **Thread-Local Scratch Density Re-use & Column Direct Indexing (`fill_noise`)**: Replaces the 393 KB (98,304 `f32`) per-chunk heap allocation in `fill_noise` with a thread-local scratch buffer, and hoists contiguous column base slices `&density[col_base..col_base + sy]`, eliminating 98,304 `volume.index(...)` calls per chunk. 5. **48-Bit Algebraic Positional Seeding Fast-Path (`LegacyPositionalRandom`)**: Precomputes `base = (seed ^ MULTIPLIER) & MASK48` in `LegacyPositionalRandom::new()`, transforming `.at(pos)` into `LegacyRandom::from_state((position_seed(pos) & MASK48) ^ self.base)` and bypassing full multiplicative seed recomputation. ### Benchmark & Performance Evidence #### Positional Random Seeding (`benchmark_legacy_positional_at`, 2,000,000 queries) | Target | Baseline (`origin/main`) | Optimized (48-bit base) | Speedup | | --- | --- | --- | --- | | Debug | 34.93 ms (17.47 ns/query) | 34.15 ms (17.08 ns/query) | 1.02x (+2.2%) | | Release | 6.02 ms (3.01 ns/query) | 5.65 ms (2.82 ns/query) | **1.07x (+6.1%)** | #### Terrain Fill Noise (`benchmark_fill_noise`, 50 chunks, 384 height) | Target | Baseline (`origin/main`) | Optimized (Cumulative) | Time Reduction | | --- | --- | --- | --- | | Debug | 479.74 ms (9.59 ms/chunk) | 450.22 ms (9.00 ms/chunk) | −6.1% | | Heap Allocations | ~393 KB / chunk | **0 B (reused scratch)** | −100% buffer allocations | ### Self-Check (`pr_ready`) `pr_ready` (`tools/pr_ready.py --quick` on omarchy, against origin/main at 47a3b39fa, 6 min 17 s) | Check | Result | | --- | --- | | Scope | ✅ 5 files, +334 −55 | | Cargo.lock | ✅ unchanged | | Public tree | ✅ | | Python tests | ✅ 480 passed (48 s) | | rustfmt | ✅ (12 s) | | clippy | ✅ (1 min 42 s, 0 warnings with `-D warnings`) | | tests: horizon-core, horizon-world | ✅ 397 passed (3 min 23 s) | | doc tests | ✅ 0 passed (9 s) | | content root tests | ➖ content root 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 up worldgen harnesses | | Label | player-facing: none (internal performance and worldgen efficiency) | 🤖 Generated with [Antigravity](https://deepmind.google/technologies/gemini/) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-03 05:14:00 +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#5641
No description provided.