[PR #1196] [MERGED] Natural spawning: borrow the spawn lists, nearest distance without a search #3410

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

📋 Pull Request Information

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

Base: main ← Head: perf/spawn-lists-borrowed


📝 Commits (1)

  • 84c7f39 Natural spawning: borrow the spawn lists, and only the nearest distance

📊 Changes

2 files changed (+31 additions, -22 deletions)

View changed files

📝 crates/horizon-client/src/game/natural_spawning.rs (+7 -1)
📝 crates/horizon-client/src/mobs/spawn_selection.rs (+24 -21)

📄 Description

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

What

Two copies natural spawning made on every spawn attempt, gone:

  • mobs_at (NaturalSpawner.mobsAt) returned an owned Vec<SpawnEntry>: the biome's (or the structure override's) whole spawn list, cloned with every entity id String in it. A group start called it to pick an entry, and every attempt called it again (isValidSpawnPostitionForType) only to check the picked entry is still in the list. It now returns the list as the level stores it (&[SpawnEntry]); the fortress list is built once in a static.
  • The nearest player's distance (getNearestPlayer, asked on every attempt) searched for the nearest player and then measured it again. It is now the minimum of the non-spectators' distances, with no branch per player.

Why

Brief perf-server-13, "then the next biggest server cost". In the 40-bot profile (ThinkPad, frame-pointer build, perf record --call-graph fp on the server thread), natural spawning was 24 % of the server thread: spawn_category_for_position 17 %, of which the nearest-player search 4.6, valid_position_for_type 4.0, the spawn-list copies (SpawnEntry clone and drop) about 2.4.

Parity

Same lists in the same order, so the same weighted picks and the same random draws; the same minimum distance (the old search kept the first of equal distances, and only the distance is used). natural_spawn, spawn_rules, finalize_spawn and spawner pass, and cargo test -p horizon-client --lib spawn (20) passes.

Numbers

Same runs as #1195 (ThinkPad server, 40 mixed bots on the desktop, seed 12345, view distance 10, three interleaved runs each, tick_mobs is where spawning runs):

run MSPT avg p95 p99 CPU ms/tick (server thread) tick_mobs ms/tick mobs chunks/s
main 1 / 2 / 3 30.32 / 31.14 / 30.85 50.9 / 55.0 / 50.7 83.2 / 83.0 / 84.4 29.19 / 29.85 / 29.28 12.32 / 12.62 / 12.33 455 / 449 / 450 246.8 / 246.9 / 247.3
this 1 / 2 / 3 30.02 / 30.17 / 30.44 54.5 / 54.3 / 53.0 84.0 / 84.9 / 79.0 28.88 / 29.20 / 29.00 11.76 / 12.07 / 12.03 449 / 454 / 442 246.8 / 246.2 / 246.2

tick_mobs 12.42 → 11.95 ms (−3.8 %), CPU per tick 29.44 → 29.03, MSPT 30.77 → 30.21 (−1.8 %).

Checks

  • cargo fmt --all --check, cargo clippy -p horizon-client --all-targets -- -D warnings: clean.
  • Parity on main + this + #1195 (one integration commit, desktop): tools/parity.py --changed origin/main, 102 harnesses, 99 passed; save_roundtrip and world_reopen need a display (vscreen only), dedicated_server fails the same way on plain main.

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/1196 **Author:** [@ludovic111](https://github.com/ludovic111) **Created:** 10/22/2026 **Status:** ✅ Merged **Merged:** 10/22/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `perf/spawn-lists-borrowed` --- ### 📝 Commits (1) - [`84c7f39`](https://github.com/horizon-rust/horizon/commit/84c7f391481c4cbe08e612c953025ab7e74781c6) Natural spawning: borrow the spawn lists, and only the nearest distance ### 📊 Changes **2 files changed** (+31 additions, -22 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/game/natural_spawning.rs` (+7 -1) 📝 `crates/horizon-client/src/mobs/spawn_selection.rs` (+24 -21) </details> ### 📄 Description > 🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself. ## What Two copies natural spawning made on every spawn attempt, gone: - `mobs_at` (`NaturalSpawner.mobsAt`) returned an owned `Vec<SpawnEntry>`: the biome's (or the structure override's) whole spawn list, cloned with every entity id `String` in it. A group start called it to pick an entry, and every attempt called it again (`isValidSpawnPostitionForType`) only to check the picked entry is still in the list. It now returns the list as the level stores it (`&[SpawnEntry]`); the fortress list is built once in a static. - The nearest player's distance (`getNearestPlayer`, asked on every attempt) searched for the nearest player and then measured it again. It is now the minimum of the non-spectators' distances, with no branch per player. ## Why Brief perf-server-13, "then the next biggest server cost". In the 40-bot profile (ThinkPad, frame-pointer build, `perf record --call-graph fp` on the server thread), natural spawning was **24 %** of the server thread: `spawn_category_for_position` 17 %, of which the nearest-player search 4.6, `valid_position_for_type` 4.0, the spawn-list copies (`SpawnEntry` clone and drop) about 2.4. ## Parity Same lists in the same order, so the same weighted picks and the same random draws; the same minimum distance (the old search kept the first of equal distances, and only the distance is used). `natural_spawn`, `spawn_rules`, `finalize_spawn` and `spawner` pass, and `cargo test -p horizon-client --lib spawn` (20) passes. ## Numbers Same runs as #1195 (ThinkPad server, 40 mixed bots on the desktop, seed 12345, view distance 10, three interleaved runs each, `tick_mobs` is where spawning runs): | run | MSPT avg | p95 | p99 | CPU ms/tick (server thread) | `tick_mobs` ms/tick | mobs | chunks/s | |---|---:|---:|---:|---:|---:|---:|---:| | main 1 / 2 / 3 | 30.32 / 31.14 / 30.85 | 50.9 / 55.0 / 50.7 | 83.2 / 83.0 / 84.4 | 29.19 / 29.85 / 29.28 | 12.32 / 12.62 / 12.33 | 455 / 449 / 450 | 246.8 / 246.9 / 247.3 | | this 1 / 2 / 3 | 30.02 / 30.17 / 30.44 | 54.5 / 54.3 / 53.0 | 84.0 / 84.9 / 79.0 | 28.88 / 29.20 / 29.00 | **11.76 / 12.07 / 12.03** | 449 / 454 / 442 | 246.8 / 246.2 / 246.2 | `tick_mobs` 12.42 → 11.95 ms (−3.8 %), CPU per tick 29.44 → 29.03, MSPT 30.77 → 30.21 (−1.8 %). ## Checks - `cargo fmt --all --check`, `cargo clippy -p horizon-client --all-targets -- -D warnings`: clean. - Parity on main + this + #1195 (one integration commit, desktop): `tools/parity.py --changed origin/main`, 102 harnesses, 99 passed; `save_roundtrip` and `world_reopen` need a display (vscreen only), `dedicated_server` fails the same way on plain main. ## 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-03 04:09:20 +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#3410
No description provided.