[PR #873] [MERGED] Natural spawning reads a kept WORLD_SURFACE heightmap #2875

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

📋 Pull Request Information

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

Base: main ← Head: perf/spawn-surface-heightmap


📝 Commits (1)

  • e8247df Natural spawning reads a kept WORLD_SURFACE heightmap

📊 Changes

6 files changed (+118 additions, -18 deletions)

View changed files

📝 crates/horizon-client/examples/save_batch_bench.rs (+0 -1)
📝 crates/horizon-client/src/chunk.rs (+102 -5)
📝 crates/horizon-client/src/game/beacon.rs (+2 -5)
📝 crates/horizon-client/src/game/natural_spawning.rs (+6 -2)
📝 crates/horizon-client/src/mobs/spawn_rules.rs (+1 -5)
📝 crates/horizon-client/src/world.rs (+7 -0)

📄 Description

🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself.

What

Every natural spawn attempt (NaturalSpawner.getRandomPosWithin) asks for the column's getHeight(WORLD_SURFACE, x, z). Horizon scanned the column down to answer it, at the cost of a chunk-map lookup and a block-name compare (is_air) per block. Vanilla reads the chunk's heightmap instead.

  • Chunk now keeps a WORLD_SURFACE heightmap: one above the highest non-air block per column, or MIN_Y.
    • It is built on the first read, one section at a time from the top. Sections of a single air state are skipped.
    • Chunk::set drops it when a write can move a column's surface. As in Heightmap.update, a write two or more blocks below the surface leaves it alone.
  • World::world_surface reads it. Natural spawning's heightmap(WorldSurface), the spawn rules' world_surface (the bat check, MobWorld) and beacons use it. Each of them scanned before.
  • Writes to sections that bypass set happen only while a chunk is being built (generation, save decoding, network decoding), before anything reads the map. The field's doc says so.
  • The unused heights field is gone. It was never written after Chunk::empty.

The other heightmap types natural spawning uses (getTopNonCollidingPos, at spawn time only) still scan.

Why

Brief perf-ticks-9, task 1: about 0.15 ms per tick in perf-ticks-8's profile, on the game thread.

Measured

Mac mini M4 Pro, shared and busy (load average 65–110). Fast profile with line tables. Each run starts a fresh world (seed 1234, creative, render distance 12, 529 chunks, 176–195 mobs), waits until the spawn area is ready, settles 40 s, then records the game thread for 20 s with macOS sample. The runs alternated base, new, base, new. Base is main e508a5ce.

run ticks natural spawning (spawn_for_chunk + SpawnState::new) of which WORLD_SURFACE lookups
base 1 498 675 samples (1.36 ms/tick) 333 (0.67 ms/tick)
new 1 471 310 (0.66 ms/tick) 0
base 2 456 355 (0.78 ms/tick) 183 (0.40 ms/tick)
new 2 438 216 (0.49 ms/tick) 3 (0.01 ms/tick)

The lookups were about half of natural spawning, and they are now gone from the profile. Samples count wall time on a machine this loaded, so the absolute numbers are high. The share is the robust number.

Checks

  • cargo fmt --all --check and cargo clippy --workspace --all-targets -- -D warnings: clean.
  • cargo test -p horizon-client --lib: 868 passed. That includes the new world_surface_follows_writes: 4,000 random writes (air, cave air, stone, a section rewritten), every column compared with a full scan every 7 writes. It also checks that out-of-range writes and unchanged states keep the map.
  • Parity, local: 9 of 9 pass on d5b46047, the same change before the rebase onto 4f9df738: natural_spawn, spawn_rules, heightmap, chunk_generation, chunk_stage, spawner, finalize_spawn, world_spawn, raid.
  • CI parity covers the rest of --changed.

Needs Ludovic

Nothing.

🤖 Generated with Claude Code


🔄 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/873 **Author:** [@ludovic111](https://github.com/ludovic111) **Created:** 10/18/2026 **Status:** ✅ Merged **Merged:** 10/18/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `perf/spawn-surface-heightmap` --- ### 📝 Commits (1) - [`e8247df`](https://github.com/horizon-rust/horizon/commit/e8247df5f0f659ebecd675f18e29679a3c8b349e) Natural spawning reads a kept WORLD_SURFACE heightmap ### 📊 Changes **6 files changed** (+118 additions, -18 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/examples/save_batch_bench.rs` (+0 -1) 📝 `crates/horizon-client/src/chunk.rs` (+102 -5) 📝 `crates/horizon-client/src/game/beacon.rs` (+2 -5) 📝 `crates/horizon-client/src/game/natural_spawning.rs` (+6 -2) 📝 `crates/horizon-client/src/mobs/spawn_rules.rs` (+1 -5) 📝 `crates/horizon-client/src/world.rs` (+7 -0) </details> ### 📄 Description > 🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself. ## What Every natural spawn attempt (`NaturalSpawner.getRandomPosWithin`) asks for the column's `getHeight(WORLD_SURFACE, x, z)`. Horizon scanned the column down to answer it, at the cost of a chunk-map lookup and a block-name compare (`is_air`) per block. Vanilla reads the chunk's heightmap instead. - `Chunk` now keeps a WORLD_SURFACE heightmap: one above the highest non-air block per column, or `MIN_Y`. - It is built on the first read, one section at a time from the top. Sections of a single air state are skipped. - `Chunk::set` drops it when a write can move a column's surface. As in `Heightmap.update`, a write two or more blocks below the surface leaves it alone. - `World::world_surface` reads it. Natural spawning's `heightmap(WorldSurface)`, the spawn rules' `world_surface` (the bat check, `MobWorld`) and beacons use it. Each of them scanned before. - Writes to `sections` that bypass `set` happen only while a chunk is being built (generation, save decoding, network decoding), before anything reads the map. The field's doc says so. - The unused `heights` field is gone. It was never written after `Chunk::empty`. The other heightmap types natural spawning uses (`getTopNonCollidingPos`, at spawn time only) still scan. ## Why Brief perf-ticks-9, task 1: about 0.15 ms per tick in perf-ticks-8's profile, on the game thread. ## Measured Mac mini M4 Pro, shared and busy (load average 65–110). Fast profile with line tables. Each run starts a fresh world (seed 1234, creative, render distance 12, 529 chunks, 176–195 mobs), waits until the spawn area is ready, settles 40 s, then records the game thread for 20 s with macOS `sample`. The runs alternated base, new, base, new. Base is main e508a5ce. | run | ticks | natural spawning (`spawn_for_chunk` + `SpawnState::new`) | of which WORLD_SURFACE lookups | |---|---|---|---| | base 1 | 498 | 675 samples (1.36 ms/tick) | 333 (0.67 ms/tick) | | new 1 | 471 | 310 (0.66 ms/tick) | 0 | | base 2 | 456 | 355 (0.78 ms/tick) | 183 (0.40 ms/tick) | | new 2 | 438 | 216 (0.49 ms/tick) | 3 (0.01 ms/tick) | The lookups were about half of natural spawning, and they are now gone from the profile. Samples count wall time on a machine this loaded, so the absolute numbers are high. The share is the robust number. ## Checks - `cargo fmt --all --check` and `cargo clippy --workspace --all-targets -- -D warnings`: clean. - `cargo test -p horizon-client --lib`: 868 passed. That includes the new `world_surface_follows_writes`: 4,000 random writes (air, cave air, stone, a section rewritten), every column compared with a full scan every 7 writes. It also checks that out-of-range writes and unchanged states keep the map. - Parity, local: 9 of 9 pass on d5b46047, the same change before the rebase onto 4f9df738: `natural_spawn`, `spawn_rules`, `heightmap`, `chunk_generation`, `chunk_stage`, `spawner`, `finalize_spawn`, `world_spawn`, `raid`. - CI parity covers the rest of `--changed`. ## Needs Ludovic Nothing. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <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:46 +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#2875
No description provided.