[PR #1253] [MERGED] Natural spawning: measure only nearby players, resolve the entry type once, skip the mobsAt recheck at the pick (tick_mobs −8 % at 40 players) #3458

Closed
opened 2026-11-03 04:09:40 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

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

Base: main ← Head: perf/spawn-checks


📝 Commits (1)

  • 4ad6bb1 Natural spawning: resolve the entry's type once, skip the mobsAt recheck where it was picked, measure only nearby players

📊 Changes

3 files changed (+119 additions, -51 deletions)

View changed files

📝 crates/horizon-client/examples/natural_spawn_fixture.rs (+9 -13)
📝 crates/horizon-client/src/game/natural_spawning.rs (+12 -9)
📝 crates/horizon-client/src/mobs/spawn_selection.rs (+98 -29)

📄 Description

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

Natural spawning (perf-server-14, plan E step 2). It follows #1196 and makes three changes to spawn_category_for_position. Vanilla's lists, checks and random draws stay the same.

  • Nearest player. getNearestPlayer(x, y, z, -1, false) is only compared with 24² (isRightDistanceToPlayerAndSpawnPoint) and the despawn distances (128² for every category). So each start now measures, once, only the non-spectators that could be within 128 blocks of an attempt within 64 blocks of the start. A player outside that set reads as infinitely far, and every check gives the same answer for that as for the real distance. An attempt that wanders more than 64 blocks from its start measures every player. The level now hands the non-spectator positions over once per tick (NaturalLevel::nearest_player_positions), so there is no per-attempt search.
  • Entry type. The entry's type (mob_type: species, size, category) is resolved once, when the group picks its entry. It used to be looked up twice per attempt (in isValidSpawnPostitionForType and again for getMobForSpawn). The pass-ending getMobForSpawn failure stays covered: a type the game cannot create never passes isValidSpawnPostitionForType, so it cannot reach that point.
  • mobsAt recheck. canSpawnMobAt's mobsAt(...).contains(entry) is skipped at the position the entry was just picked from, because the list there holds the entry. Nothing between the pick and the check changes what mobsAt reads. Positions other than the pick are still rechecked.
  • Water-ambient tag. The reduce_water_ambient_spawns biome tag is read only for the water-ambient category, in vanilla's && order.

Numbers

ThinkPad server (nice 19), with 40 mixed bots driven from the desktop through tools/server_bench.py: seed 12345, view distance 10, 120 s warmup, 180 s measured. Three interleaved runs per variant. Spans are averaged over the last 3600 ticks of each run (HORIZON_SPIKES=0, all spans logged). Base is #1196's head (84c7f391), which is what this branch sat on.

run MSPT avg p95 tick_mobs ms tick_blocks ms mobs
new 1 30.37 57.75 11.32 5.20 446
base 1 31.15 58.12 12.49 5.34 452
base 2 31.55 58.96 12.32 5.21 451
new 2 30.47 57.33 11.55 5.19 446
new 3 29.93 52.79 11.15 5.13 454
base 3 30.56 55.21 12.15 5.18 457
mean 30.26 vs 31.09 (−2.7 %) 11.34 vs 12.32 (−8.0 %, −0.98 ms) 5.17 vs 5.24 449 vs 453

Before this change, a desktop profile (perf, LBR, inlined) put natural spawning at 24 % of the server thread. Most of that sat in spawn_category_for_position (19.4 %): the nearest-player search took 6.85 %, the start-state read 5.27 %, isValidSpawnPostitionForType 2.95 % and mobsAt 2.21 %, of which structure lookups were 1.53 %.

Parity

  • tools/parity.py natural_spawn spawn_rules finalize_spawn spawner: 4 passed. natural_spawn covers players near and far (100–200 blocks), spectators, the world spawn, fortress bounds and structure overrides.
  • tools/parity.py --changed origin/main before the rebase (on #1196): 98 passed, 0 failed. After the rebase onto main (4ad6bb18): 102 passed, 0 failed.

Self-check

  • cargo fmt --all --check: clean
  • cargo clippy --workspace --all-targets -- -D warnings: clean
  • cargo test -p horizon-client --lib spawn: 20 passed
  • Merges cleanly with the open contributor PRs #1200, #1202, #1203 and #1205 (git merge-tree). #1200 also adds a field to natural_spawning.rs's GameLevel, and the two merge without conflict.

🤖 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/1253 **Author:** [@ludovic111](https://github.com/ludovic111) **Created:** 10/22/2026 **Status:** ✅ Merged **Merged:** 10/23/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `perf/spawn-checks` --- ### 📝 Commits (1) - [`4ad6bb1`](https://github.com/horizon-rust/horizon/commit/4ad6bb18efe9a08c02504f382502a578510dc19e) Natural spawning: resolve the entry's type once, skip the mobsAt recheck where it was picked, measure only nearby players ### 📊 Changes **3 files changed** (+119 additions, -51 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/examples/natural_spawn_fixture.rs` (+9 -13) 📝 `crates/horizon-client/src/game/natural_spawning.rs` (+12 -9) 📝 `crates/horizon-client/src/mobs/spawn_selection.rs` (+98 -29) </details> ### 📄 Description > 🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself. Natural spawning (perf-server-14, plan E step 2). It follows #1196 and makes three changes to `spawn_category_for_position`. Vanilla's lists, checks and random draws stay the same. - **Nearest player.** `getNearestPlayer(x, y, z, -1, false)` is only compared with 24² (`isRightDistanceToPlayerAndSpawnPoint`) and the despawn distances (128² for every category). So each start now measures, once, only the non-spectators that could be within 128 blocks of an attempt within 64 blocks of the start. A player outside that set reads as infinitely far, and every check gives the same answer for that as for the real distance. An attempt that wanders more than 64 blocks from its start measures every player. The level now hands the non-spectator positions over once per tick (`NaturalLevel::nearest_player_positions`), so there is no per-attempt search. - **Entry type.** The entry's type (`mob_type`: species, size, category) is resolved once, when the group picks its entry. It used to be looked up twice per attempt (in `isValidSpawnPostitionForType` and again for `getMobForSpawn`). The pass-ending `getMobForSpawn` failure stays covered: a type the game cannot create never passes `isValidSpawnPostitionForType`, so it cannot reach that point. - **`mobsAt` recheck.** `canSpawnMobAt`'s `mobsAt(...).contains(entry)` is skipped at the position the entry was just picked from, because the list there holds the entry. Nothing between the pick and the check changes what `mobsAt` reads. Positions other than the pick are still rechecked. - **Water-ambient tag.** The `reduce_water_ambient_spawns` biome tag is read only for the water-ambient category, in vanilla's `&&` order. ### Numbers ThinkPad server (nice 19), with 40 mixed bots driven from the desktop through `tools/server_bench.py`: seed 12345, view distance 10, 120 s warmup, 180 s measured. Three interleaved runs per variant. Spans are averaged over the last 3600 ticks of each run (`HORIZON_SPIKES=0`, all spans logged). Base is #1196's head (84c7f391), which is what this branch sat on. | run | MSPT avg | p95 | `tick_mobs` ms | `tick_blocks` ms | mobs | |---|---:|---:|---:|---:|---:| | new 1 | 30.37 | 57.75 | 11.32 | 5.20 | 446 | | base 1 | 31.15 | 58.12 | 12.49 | 5.34 | 452 | | base 2 | 31.55 | 58.96 | 12.32 | 5.21 | 451 | | new 2 | 30.47 | 57.33 | 11.55 | 5.19 | 446 | | new 3 | 29.93 | 52.79 | 11.15 | 5.13 | 454 | | base 3 | 30.56 | 55.21 | 12.15 | 5.18 | 457 | | **mean** | **30.26 vs 31.09 (−2.7 %)** | | **11.34 vs 12.32 (−8.0 %, −0.98 ms)** | 5.17 vs 5.24 | 449 vs 453 | Before this change, a desktop profile (perf, LBR, inlined) put natural spawning at 24 % of the server thread. Most of that sat in `spawn_category_for_position` (19.4 %): the nearest-player search took 6.85 %, the start-state read 5.27 %, `isValidSpawnPostitionForType` 2.95 % and `mobsAt` 2.21 %, of which structure lookups were 1.53 %. ### Parity - `tools/parity.py natural_spawn spawn_rules finalize_spawn spawner`: 4 passed. `natural_spawn` covers players near and far (100–200 blocks), spectators, the world spawn, fortress bounds and structure overrides. - `tools/parity.py --changed origin/main` before the rebase (on #1196): 98 passed, 0 failed. After the rebase onto main (4ad6bb18): 102 passed, 0 failed. ### Self-check - `cargo fmt --all --check`: clean - `cargo clippy --workspace --all-targets -- -D warnings`: clean - `cargo test -p horizon-client --lib spawn`: 20 passed - Merges cleanly with the open contributor PRs #1200, #1202, #1203 and #1205 (`git merge-tree`). #1200 also adds a field to `natural_spawning.rs`'s `GameLevel`, and the two merge without conflict. 🤖 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-03 04:09:40 +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#3458
No description provided.