[PR #949] [MERGED] Reuse terrain sampler scratch buffers between chunks #2931

Closed
opened 2026-11-02 16:00:13 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon/pull/949
Author: @rdbtCVS
Created: 10/19/2026
Status: ✅ Merged
Merged: 10/19/2026
Merged by: @ludovic111

Base: main ← Head: t3/reuse-terrain-sampler-scratch


📝 Commits (1)

  • 2fe7cb3 Reuse terrain sampler scratch buffers between chunks

📊 Changes

3 files changed (+97 additions, -6 deletions)

View changed files

📝 crates/horizon-core/src/density/mod.rs (+7 -0)
📝 crates/horizon-core/src/density/tests.rs (+70 -0)
📝 crates/horizon-world/src/worldgen/generator.rs (+20 -6)

📄 Description

What and why

Terrain generation currently creates and drops a fresh SamplerContext::cached() for every chunk. Keep its scratch float buffers in the generation worker between chunks. After every returned result, including errors, discard all density cache cells and world-field bindings; preserve only scratch allocations and the cache-enabled mode. Scratch storage is retained for the worker's lifetime, so this trades retained memory for fewer allocations.

This is independent of #943 and #946, based on f422cbb7. No earlier experimental patches are included.

A short local screen suggests a modest improvement, not an established speedup. Same fast profile/resources/instrumentation, size 16 (256 terrain / 196 decorated chunks), discarded offset 9x9 warmup, alternating baseline/candidate order, one warmup pair discarded:

  • Six workers, three measured pairs/site: median paired B/C terrain throughput +1.20% primary / +1.96% secondary; terrain+decoration +0.91% / +1.71%.
  • Terrain B/C pair ratios: primary 1.01560, 1.01126, 1.01200; secondary 1.04821, 1.01962, 0.99051.
  • One-worker screen, one measured pair/site after discarded warmup: terrain +2.99% / +3.01%, combined +2.48% / +2.67%. No observed single-worker regression in this small sample.
  • An earlier screen overlapped unrelated host compilation and gave contradictory results; it is excluded from conclusions. The user reported those loads had stopped before this bounded replacement screen. No extended gate/scaling campaign was run.

Primary seed 12345, center (0,0); secondary seed -1, center (32,-32). Workload command shape: chunkgen_bench RESOURCES --seed SEED --size 16 --threads WORKERS --center X Z. The temporary local runner adds offset warmup, exact phase elapsed/count reporting and optional full snapshots outside timing. Build command: cargo build --profile fast -p horizon-world --example concurrency_bench. Both binaries used the same runner; instrumentation is not committed. Raw timings, runner/reproduction scripts and comparison artifacts are retained locally under /tmp/horizon-concurrency-20261018/scratch.

Vanilla reference

Read with tools/rea_vanilla.py: NoiseChunk constructor and NoiseBasedChunkGenerator.doFill. Vanilla acquires a reusable density buffer pool while constructing fresh context user fields and caching samplers for the chunk.

Validation:

  • Full terrain and decorated output matched byte for byte at both sites with 1 and 6 workers: eight complete comparisons, including blocks, sections, biomes, heightmaps, postprocessing, ticks, structure metadata, block entities and entities.
  • Reset tests cover scalar/volume invalidation at repeated coordinates, graph cache-ID reuse, beardifier/blender binding release, scratch allocation preservation after an error, and uncached mode. All 34 density tests passed.
  • Fresh pinned client and server density parity: 15,524,627 matching output lines per side.
  • Fresh pinned client and server terrain parity: 114 fixtures, 7,950,873 matching output lines per side.
  • Broader affected parity suites were omitted by quick readiness. These finite corpora do not establish universal world-generation parity; no full nonempty-blending or gameplay validation is claimed.

Self-check

pr_ready (tools/pr_ready.py --quick on archlinux, against origin/main at f422cbb79, 1 min 45 s)

Check Result
Scope ✅ 3 files, +97 −6
Cargo.lock ✅ unchanged
Public tree ✅
Python tests ➖ no Python change
rustfmt ✅ (4 s)
clippy ✅ (26 s)
tests: horizon-core, horizon-world ✅ 282 passed (1 min 13 s)
tokens.css up to date ➖ theme untouched
launcher rustfmt ➖ launcher untouched
launcher clippy ➖ launcher untouched
launcher tests ➖ launcher untouched
Parity ➖ 315 affected, to run in CI (--quick): active_effect_tick, active_effects, ambient_sound, anchor, arrow, attribute_health, attribute_map_queries, attribute, attribute_updates, audio_attributes, auto_jump, bamboo and 303 more
Label player-facing: feature if players get something new, no label for a fix (release-now if urgent)

🔄 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/949 **Author:** [@rdbtCVS](https://github.com/rdbtCVS) **Created:** 10/19/2026 **Status:** ✅ Merged **Merged:** 10/19/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `t3/reuse-terrain-sampler-scratch` --- ### 📝 Commits (1) - [`2fe7cb3`](https://github.com/horizon-rust/horizon/commit/2fe7cb3a4bb156daab731ff5b031621f6e39c763) Reuse terrain sampler scratch buffers between chunks ### 📊 Changes **3 files changed** (+97 additions, -6 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-core/src/density/mod.rs` (+7 -0) 📝 `crates/horizon-core/src/density/tests.rs` (+70 -0) 📝 `crates/horizon-world/src/worldgen/generator.rs` (+20 -6) </details> ### 📄 Description ## What and why Terrain generation currently creates and drops a fresh `SamplerContext::cached()` for every chunk. Keep its scratch float buffers in the generation worker between chunks. After every returned result, including errors, discard all density cache cells and world-field bindings; preserve only scratch allocations and the cache-enabled mode. Scratch storage is retained for the worker's lifetime, so this trades retained memory for fewer allocations. This is independent of #943 and #946, based on f422cbb7. No earlier experimental patches are included. A short local screen suggests a modest improvement, **not an established speedup**. Same `fast` profile/resources/instrumentation, size 16 (256 terrain / 196 decorated chunks), discarded offset 9x9 warmup, alternating baseline/candidate order, one warmup pair discarded: - Six workers, three measured pairs/site: median paired B/C terrain throughput **+1.20% primary / +1.96% secondary**; terrain+decoration **+0.91% / +1.71%**. - Terrain B/C pair ratios: primary 1.01560, 1.01126, 1.01200; secondary 1.04821, 1.01962, 0.99051. - One-worker screen, one measured pair/site after discarded warmup: terrain +2.99% / +3.01%, combined +2.48% / +2.67%. No observed single-worker regression in this small sample. - An earlier screen overlapped unrelated host compilation and gave contradictory results; it is excluded from conclusions. The user reported those loads had stopped before this bounded replacement screen. No extended gate/scaling campaign was run. Primary seed 12345, center (0,0); secondary seed -1, center (32,-32). Workload command shape: `chunkgen_bench RESOURCES --seed SEED --size 16 --threads WORKERS --center X Z`. The temporary local runner adds offset warmup, exact phase elapsed/count reporting and optional full snapshots outside timing. Build command: `cargo build --profile fast -p horizon-world --example concurrency_bench`. Both binaries used the same runner; instrumentation is not committed. Raw timings, runner/reproduction scripts and comparison artifacts are retained locally under `/tmp/horizon-concurrency-20261018/scratch`. ## Vanilla reference Read with `tools/rea_vanilla.py`: `NoiseChunk` constructor and `NoiseBasedChunkGenerator.doFill`. Vanilla acquires a reusable density buffer pool while constructing fresh context user fields and caching samplers for the chunk. Validation: - Full terrain and decorated output matched byte for byte at both sites with 1 and 6 workers: eight complete comparisons, including blocks, sections, biomes, heightmaps, postprocessing, ticks, structure metadata, block entities and entities. - Reset tests cover scalar/volume invalidation at repeated coordinates, graph cache-ID reuse, beardifier/blender binding release, scratch allocation preservation after an error, and uncached mode. All 34 density tests passed. - Fresh pinned client **and** server density parity: 15,524,627 matching output lines per side. - Fresh pinned client **and** server terrain parity: 114 fixtures, 7,950,873 matching output lines per side. - Broader affected parity suites were omitted by quick readiness. These finite corpora do not establish universal world-generation parity; no full nonempty-blending or gameplay validation is claimed. ## Self-check **pr_ready** (`tools/pr_ready.py --quick` on archlinux, against origin/main at f422cbb79, 1 min 45 s) | Check | Result | | --- | --- | | Scope | ✅ 3 files, +97 −6 | | Cargo.lock | ✅ unchanged | | Public tree | ✅ | | Python tests | ➖ no Python change | | rustfmt | ✅ (4 s) | | clippy | ✅ (26 s) | | tests: horizon-core, horizon-world | ✅ 282 passed (1 min 13 s) | | tokens.css up to date | ➖ theme untouched | | launcher rustfmt | ➖ launcher untouched | | launcher clippy | ➖ launcher untouched | | launcher tests | ➖ launcher untouched | | Parity | ➖ 315 affected, to run in CI (--quick): active_effect_tick, active_effects, ambient_sound, anchor, arrow, attribute_health, attribute_map_queries, attribute, attribute_updates, audio_attributes, auto_jump, bamboo and 303 more | | Label | player-facing: `feature` if players get something new, no label for a fix (`release-now` if urgent) | --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 16:00:13 +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#2931
No description provided.