[PR #567] [MERGED] Villagers search an index of POI blocks instead of scanning every block #2645

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

📋 Pull Request Information

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

Base: main ← Head: perf/villager-poi-index


📝 Commits (1)

  • a2cce74 Villagers search an index of POI blocks instead of scanning every block

📊 Changes

4 files changed (+185 additions, -61 deletions)

View changed files

📝 crates/horizon-client/src/lib.rs (+1 -0)
📝 crates/horizon-client/src/mobs/villager_brain.rs (+47 -61)
➕ crates/horizon-client/src/poi_sections.rs (+134 -0)
📝 crates/horizon-client/src/world.rs (+3 -0)

📄 Description

What

Villagers looking for a job site, a bed or a meeting point (AcquirePoi, and the bed searches of babies and sleeping villagers) read every block of every section in a 48-block sphere around them, over 7x7 chunks: up to tens of thousands of block reads per search, for each villager every 20 to 40 ticks. With 80 villagers it was the largest single cost of the server tick: pois_in_range alone was 16 % of tick_world, and PalettedContainer::get from it most of the rest of try_start's 25 %.

Vanilla answers these searches from PoiManager, which keeps a record per POI block, by section. Horizon now does the same: PoiSections (new, poi_sections.rs) keeps the list of the POI blocks of each section (block index and kind, in index order), built from the section's blocks the first time a search reaches it. A section with no POI state in its palette gets an empty list without reading its blocks.

Lists are kept exact without hooking every block write: resident sections are immutable behind Arc and every edit goes through Arc::make_mut, so the list holds a Weak to the section it was built from. Any edit (or a replaced section or chunk) gives the section a new address, and the next search rebuilds that one section's list. This is the scheme random_tick_sections already uses for chunks. Lists of unloaded sections are dropped once the map doubles.

Same results

  • pois_in_range visits the chunks in the same order (z, then x), and within a chunk the same positions in the same order (y, z, x: the list's index order), then applies the same filters in the same order and the same stable sort by distance. Air and non-POI blocks were skipped before as well.
  • homes_in_range scanned the cube y, z, x; it now collects the beds from the lists and sorts them by (y, z, x), a total order on positions, so the list is the same.
  • Checked side by side in game: a local build ran the old scan next to the new one on every call and compared the results, in the busy world below with block edits during the run (composters, lecterns and beds placed and removed in the pen, baby villagers summoned, time switched between dusk and night): 10,500 pois_in_range and 80 homes_in_range calls, 0 differences, with the bed counts following the edits (5, 7, 10 beds).
  • Unit tests: lists follow in-place edits, replaced sections and chunks, unused palette entries; unloaded sections are pruned.

Results

Fast profile (release optimisations without LTO), integrated-server tick in thread CPU time, base/new × 3 interleaved:

Fast profile, same world and settings main this PR
Tick CPU time, mean 7.25 ms 6.34 ms (-13 %)
Tick CPU time, p95 / p99 / max 11.1 / 13.4 / 18.2 ms 10.7 / 12.1 / 13.8 ms
Mob ticks (tick_mobs), mean 6.01 ms 5.05 ms
First 200 ticks after the world opens (CPU) 1380 ms 1177 ms
Frame CPU time, mean (wall clock) 8.84 ms 3.99 ms
Average FPS / 1 % low (median of 3) 151 / 11 235 / 12

FPS and frame times are from a Mac mini at load 40–70 (other agents' builds and harnesses): compare the columns, not the absolute values. GPU time: no rendering change.

Profile (sample, 20 s, main thread): pois_in_range went from 16.4 % to 2.6 % of tick_world (its remaining cost is the per-section map lookup and the ticket checks); villager_brain::B::try_start from 25.5 % to 14.6 %, now mostly the path search to the POIs found.

How it was measured

  • Machine: Mac mini M4 Pro (14 cores, Metal), shared with other sessions. Tick time is thread CPU time (CLOCK_THREAD_CPUTIME_ID around each tick, a local uncommitted probe), so time spent descheduled doesn't count.
  • World: seed 1234, creative, next to the village benchmark scene (-960, 66, 176), night (time set 18000, advance_time false), a fenced pen with 400 summoned mobs (80 villagers, 80 cows, 80 sheep, 60 zombies, 60 chickens, 40 pigs), about 600 mobs with the village. Saved once and restored before every run. Render distance 12, FPS uncapped, VSync off, offscreen.
  • Run: load, 15 s warm-up, then bench 40 (about 800 ticks).

Self-check

  • cargo fmt --all --check: clean.
  • cargo clippy -p horizon-client --all-targets -- -D warnings: clean.
  • cargo test -p horizon-client --lib: 760 passed, 0 failed.
  • Local parity, tools/parity.py --changed origin/main: 84 harnesses affected: 83 passed, world_spawn matched 59/60 and 58/60 seeds in two full runs on the loaded machine; each differing seed (5882138163895760678, 12345, -2080727421395798566) matches when rerun alone, and vanilla itself returned different spawns for them across runs (its own log notes "vanilla itself gave several spawns"). The search order of world spawn doesn't touch this code. mob_ai and dedicated_server pass.

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/567 **Author:** [@ludovic111](https://github.com/ludovic111) **Created:** 10/16/2026 **Status:** ✅ Merged **Merged:** 10/17/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `perf/villager-poi-index` --- ### 📝 Commits (1) - [`a2cce74`](https://github.com/horizon-rust/horizon/commit/a2cce7486ddb847d5455894e7e7f00ef75fa76c7) Villagers search an index of POI blocks instead of scanning every block ### 📊 Changes **4 files changed** (+185 additions, -61 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/lib.rs` (+1 -0) 📝 `crates/horizon-client/src/mobs/villager_brain.rs` (+47 -61) ➕ `crates/horizon-client/src/poi_sections.rs` (+134 -0) 📝 `crates/horizon-client/src/world.rs` (+3 -0) </details> ### 📄 Description ## What Villagers looking for a job site, a bed or a meeting point (`AcquirePoi`, and the bed searches of babies and sleeping villagers) read **every block** of every section in a 48-block sphere around them, over 7x7 chunks: up to tens of thousands of block reads per search, for each villager every 20 to 40 ticks. With 80 villagers it was the largest single cost of the server tick: `pois_in_range` alone was 16 % of `tick_world`, and `PalettedContainer::get` from it most of the rest of `try_start`'s 25 %. Vanilla answers these searches from `PoiManager`, which keeps a record per POI block, by section. Horizon now does the same: `PoiSections` (new, `poi_sections.rs`) keeps the list of the POI blocks of each section (block index and kind, in index order), built from the section's blocks the first time a search reaches it. A section with no POI state in its palette gets an empty list without reading its blocks. Lists are kept exact without hooking every block write: resident sections are immutable behind `Arc` and every edit goes through `Arc::make_mut`, so the list holds a `Weak` to the section it was built from. Any edit (or a replaced section or chunk) gives the section a new address, and the next search rebuilds that one section's list. This is the scheme `random_tick_sections` already uses for chunks. Lists of unloaded sections are dropped once the map doubles. ## Same results - `pois_in_range` visits the chunks in the same order (z, then x), and within a chunk the same positions in the same order (y, z, x: the list's index order), then applies the same filters in the same order and the same stable sort by distance. Air and non-POI blocks were skipped before as well. - `homes_in_range` scanned the cube y, z, x; it now collects the beds from the lists and sorts them by (y, z, x), a total order on positions, so the list is the same. - **Checked side by side in game:** a local build ran the old scan next to the new one on every call and compared the results, in the busy world below with block edits during the run (composters, lecterns and beds placed and removed in the pen, baby villagers summoned, time switched between dusk and night): **10,500 `pois_in_range` and 80 `homes_in_range` calls, 0 differences**, with the bed counts following the edits (5, 7, 10 beds). - Unit tests: lists follow in-place edits, replaced sections and chunks, unused palette entries; unloaded sections are pruned. ## Results Fast profile (release optimisations without LTO), integrated-server tick in **thread CPU time**, base/new × 3 interleaved: | Fast profile, same world and settings | `main` | this PR | | --- | ---: | ---: | | Tick CPU time, mean | 7.25 ms | **6.34 ms** (-13 %) | | Tick CPU time, p95 / p99 / max | 11.1 / 13.4 / 18.2 ms | 10.7 / 12.1 / 13.8 ms | | Mob ticks (`tick_mobs`), mean | 6.01 ms | 5.05 ms | | First 200 ticks after the world opens (CPU) | 1380 ms | 1177 ms | | Frame CPU time, mean (wall clock) | 8.84 ms | 3.99 ms | | Average FPS / 1 % low (median of 3) | 151 / 11 | 235 / 12 | FPS and frame times are from a Mac mini at load 40–70 (other agents' builds and harnesses): compare the columns, not the absolute values. GPU time: no rendering change. Profile (`sample`, 20 s, main thread): `pois_in_range` went from 16.4 % to 2.6 % of `tick_world` (its remaining cost is the per-section map lookup and the ticket checks); `villager_brain::B::try_start` from 25.5 % to 14.6 %, now mostly the path search to the POIs found. ### How it was measured - **Machine:** Mac mini M4 Pro (14 cores, Metal), shared with other sessions. Tick time is thread CPU time (`CLOCK_THREAD_CPUTIME_ID` around each tick, a local uncommitted probe), so time spent descheduled doesn't count. - **World:** seed 1234, creative, next to the `village` benchmark scene (-960, 66, 176), night (`time set 18000`, `advance_time false`), a fenced pen with 400 summoned mobs (80 villagers, 80 cows, 80 sheep, 60 zombies, 60 chickens, 40 pigs), about 600 mobs with the village. Saved once and restored before every run. Render distance 12, FPS uncapped, VSync off, offscreen. - **Run:** load, 15 s warm-up, then `bench 40` (about 800 ticks). ## Self-check - `cargo fmt --all --check`: clean. - `cargo clippy -p horizon-client --all-targets -- -D warnings`: clean. - `cargo test -p horizon-client --lib`: 760 passed, 0 failed. - Local parity, `tools/parity.py --changed origin/main`: **84 harnesses affected: 83 passed**, `world_spawn` matched 59/60 and 58/60 seeds in two full runs on the loaded machine; each differing seed (5882138163895760678, 12345, -2080727421395798566) matches when rerun alone, and vanilla itself returned different spawns for them across runs (its own log notes "vanilla itself gave several spawns"). The search order of world spawn doesn't touch this code. `mob_ai` and `dedicated_server` pass. 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:53 +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#2645
No description provided.