[PR #508] [MERGED] Mobs: find the mobs to push through a grid, not a scan of every mob (−14 % tick time) #2607

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

📋 Pull Request Information

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

Base: main ← Head: perf/mob-push-grid


📝 Commits (2)

  • 4f17351 Mobs: find the mobs to push through a grid, not a scan of every mob
  • 8a48f3b mob_ai probe: keep the snapshot grid as the game does

📊 Changes

6 files changed (+181 additions, -6 deletions)

View changed files

📝 crates/horizon-client/examples/mob_ai_probe.rs (+12 -5)
📝 crates/horizon-client/src/game/creatures.rs (+5 -0)
📝 crates/horizon-client/src/mobs.rs (+3 -0)
📝 crates/horizon-client/src/mobs/ai.rs (+1 -1)
➕ crates/horizon-client/src/mobs/seen_grid.rs (+148 -0)
📝 crates/horizon-client/src/mobs/world.rs (+12 -0)

📄 Description

What

LivingEntity.pushEntities compared each mob's box with every other mob of the tick's snapshot. That is quadratic: about 1 ms per tick with 600 mobs, 13 % of the mob ticks, and it grows with mob farms.

The snapshot is now also kept in a grid (mobs::seen_grid) of 4-block columns by each box's minimum corner, updated as each mob finishes its tick, as the snapshot is. The push reads only the columns its box can reach. Candidates come back in snapshot order, so the pushes, their order and the summed velocities are the same. A query wider than the grid, or not finite, falls back to the full scan. The mob_ai probe keeps the same grid, so the vanilla harness checks pushes through it.

Why

Mob farms and crowded villages make this the term that grows fastest with mob count (#495).

Results

Fast profile, thread CPU time per tick, base/new × 3:

main this PR
Tick, mean 8.36 ms 7.15 ms (−14 %)
Tick, p95 11.9 ms 10.6 ms
Mob ticks, mean 7.27 ms 6.06 ms

Same result: with the old scan and the grid both run on every push and compared, 800,000 pushes gave the same targets in the same order. Unit tests check the grid against brute force over 2,000 random moves and queries, and the fallback for unbounded areas.

All four server-tick PRs together (release build)

This PR is one of four independent server-tick PRs (supporting block, collision scan, push grid, POI table). Built together with the release profile (thin LTO), base/new × 3:

Release, same world and settings main the four PRs
Tick CPU time, mean 7.40 ms 4.45 ms (−40 %)
Tick CPU time, p95 / p99 / max 11.8 / 20.6 / 35.5 ms 8.2 / 10.2 / 13.2 ms
Mob ticks (tick_mobs), mean 6.35 ms 3.46 ms
First 200 ticks after the world opens (CPU, #489) 1,431 ms 1,056 ms (−26 %)
Frame CPU time, mean (wall clock) 4.95 ms 1.79 ms
Average FPS / 1 % low (median of 3) 175 / 11 484 / 18
GPU time per frame not measured (offscreen) no rendering change

FPS and 1 % lows are from a machine at load ~40: compare the two columns, not the absolute values.

How it was measured

  • Machine: Mac mini M4 Pro (14 cores, Metal), shared with other agents' builds and harnesses (load average 35–40), so wall-clock frame times swing run to run. The primary figure is the integrated server's tick time in thread CPU time: a local, uncommitted patch times the existing spans with CLOCK_THREAD_CPUTIME_ID, so time spent descheduled by other processes doesn't count.
  • World: seed 1234, creative, the village of the village benchmark scene (-960, 66, 176), night (time set 18000, advance_time false). Next to it, a fenced pen with 400 summoned mobs (80 villagers, 80 cows, 80 sheep, 60 zombies, 60 chickens, 40 pigs). About 600 mobs in all. The world was saved once and restored before every run. Render distance 12, FPS uncapped, VSync off, offscreen 1280×720.
  • Run: load, 300 ticks of warm-up, then bench 40 (about 800 ticks). Builds are interleaved base/new × 3, and the table gives the mean of the three runs' averages (percentiles: median of the three).
  • Identical behaviour: checked in game by running the old and the new code side by side on every call over the same world (details above), and by the parity harnesses.

Self-check

  • cargo fmt --all --check: clean.
  • cargo clippy --workspace --all-targets -- -D warnings: clean, on one commit with the four server-tick PRs merged (and -p horizon-client -p horizon-world on each branch).
  • cargo test -p horizon-client --lib: 752 passed, 0 failed (four PRs merged). cargo test -p horizon-world: 154 passed.
  • Local parity, tools/parity.py --changed origin/main on the merged commit (4 at a time on a loaded Mac mini): 287 harnesses affected; 267 ran (the other 20 had already passed with the same inputs): 267 passed, 0 failed (116 min).

Nothing needs Ludovic.

🤖 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/508 **Author:** [@ludovic111](https://github.com/ludovic111) **Created:** 10/16/2026 **Status:** ✅ Merged **Merged:** 10/16/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `perf/mob-push-grid` --- ### 📝 Commits (2) - [`4f17351`](https://github.com/horizon-rust/horizon/commit/4f1735170ee1590aad830e350d5547d57aac502d) Mobs: find the mobs to push through a grid, not a scan of every mob - [`8a48f3b`](https://github.com/horizon-rust/horizon/commit/8a48f3b383232b3569eab9713efc20c11daca864) mob_ai probe: keep the snapshot grid as the game does ### 📊 Changes **6 files changed** (+181 additions, -6 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/examples/mob_ai_probe.rs` (+12 -5) 📝 `crates/horizon-client/src/game/creatures.rs` (+5 -0) 📝 `crates/horizon-client/src/mobs.rs` (+3 -0) 📝 `crates/horizon-client/src/mobs/ai.rs` (+1 -1) ➕ `crates/horizon-client/src/mobs/seen_grid.rs` (+148 -0) 📝 `crates/horizon-client/src/mobs/world.rs` (+12 -0) </details> ### 📄 Description ## What `LivingEntity.pushEntities` compared each mob's box with every other mob of the tick's snapshot. That is quadratic: about 1 ms per tick with 600 mobs, 13 % of the mob ticks, and it grows with mob farms. The snapshot is now also kept in a grid (`mobs::seen_grid`) of 4-block columns by each box's minimum corner, updated as each mob finishes its tick, as the snapshot is. The push reads only the columns its box can reach. Candidates come back in snapshot order, so the pushes, their order and the summed velocities are the same. A query wider than the grid, or not finite, falls back to the full scan. The `mob_ai` probe keeps the same grid, so the vanilla harness checks pushes through it. ## Why Mob farms and crowded villages make this the term that grows fastest with mob count (#495). ## Results Fast profile, thread CPU time per tick, base/new × 3: | | `main` | this PR | | --- | --- | --- | | Tick, mean | 8.36 ms | **7.15 ms** (−14 %) | | Tick, p95 | 11.9 ms | 10.6 ms | | Mob ticks, mean | 7.27 ms | 6.06 ms | Same result: with the old scan and the grid both run on every push and compared, 800,000 pushes gave the same targets in the same order. Unit tests check the grid against brute force over 2,000 random moves and queries, and the fallback for unbounded areas. ### All four server-tick PRs together (release build) This PR is one of four independent server-tick PRs (supporting block, collision scan, push grid, POI table). Built together with the release profile (thin LTO), base/new × 3: | Release, same world and settings | `main` | the four PRs | | --- | --- | --- | | Tick CPU time, mean | 7.40 ms | **4.45 ms** (−40 %) | | Tick CPU time, p95 / p99 / max | 11.8 / 20.6 / 35.5 ms | **8.2 / 10.2 / 13.2 ms** | | Mob ticks (`tick_mobs`), mean | 6.35 ms | **3.46 ms** | | First 200 ticks after the world opens (CPU, #489) | 1,431 ms | **1,056 ms** (−26 %) | | Frame CPU time, mean (wall clock) | 4.95 ms | 1.79 ms | | Average FPS / 1 % low (median of 3) | 175 / 11 | 484 / 18 | | GPU time per frame | not measured (offscreen) | no rendering change | FPS and 1 % lows are from a machine at load ~40: compare the two columns, not the absolute values. ### How it was measured - **Machine:** Mac mini M4 Pro (14 cores, Metal), shared with other agents' builds and harnesses (load average 35–40), so wall-clock frame times swing run to run. The primary figure is the integrated server's **tick time in thread CPU time**: a local, uncommitted patch times the existing `spans` with `CLOCK_THREAD_CPUTIME_ID`, so time spent descheduled by other processes doesn't count. - **World:** seed 1234, creative, the village of the `village` benchmark scene (-960, 66, 176), night (`time set 18000`, `advance_time false`). Next to it, a fenced pen with 400 summoned mobs (80 villagers, 80 cows, 80 sheep, 60 zombies, 60 chickens, 40 pigs). About 600 mobs in all. The world was saved once and restored before every run. Render distance 12, FPS uncapped, VSync off, offscreen 1280×720. - **Run:** load, 300 ticks of warm-up, then `bench 40` (about 800 ticks). Builds are interleaved base/new × 3, and the table gives the mean of the three runs' averages (percentiles: median of the three). - **Identical behaviour:** checked in game by running the old and the new code side by side on every call over the same world (details above), and by the parity harnesses. ### Self-check - `cargo fmt --all --check`: clean. - `cargo clippy --workspace --all-targets -- -D warnings`: clean, on one commit with the four server-tick PRs merged (and `-p horizon-client -p horizon-world` on each branch). - `cargo test -p horizon-client --lib`: 752 passed, 0 failed (four PRs merged). `cargo test -p horizon-world`: 154 passed. - Local parity, `tools/parity.py --changed origin/main` on the merged commit (4 at a time on a loaded Mac mini): **287 harnesses affected; 267 ran (the other 20 had already passed with the same inputs): 267 passed, 0 failed** (116 min). Nothing needs Ludovic. 🤖 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:57:35 +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#2607
No description provided.