[PR #864] [MERGED] Server: check each entity's range for every client from one compact pass #2868

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

📋 Pull Request Information

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

Base: main ← Head: perf/tracking-sections


📝 Commits (1)

  • b171508 Server: check each entity's range for every client from one compact pass

📊 Changes

1 file changed (+97 additions, -23 deletions)

View changed files

📝 crates/horizon-client/src/lan/entity.rs (+97 -23)

📄 Description

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

What

Entity tracking's per-pair range check (TrackedEntity.updatePlayer), the biggest part of track_entities after #841, is cheaper:

  • Each entity's horizontal position, id, tracking range and chunk are read once a tick into a compact list, not from its EntityState once per client.
  • Each client looks up each of the tick's chunks once in the chunks it holds (remembered per client for the tick), not once per entity standing in it.
  • An entity with no changes and nobody starting to track it this tick skips the loop over the clients.

Which client tracks which entity, and every packet and its order, are unchanged.

Why not vanilla's "re-check on section changes" shape

The brief asked to re-check only when something crosses a section. Reading 26.3, that is only part of vanilla's shape: ChunkMap.move(player) runs updatePlayer(player) for every tracked entity, and it is called from handlePlayerPositionChange on every accepted move packet (plus handleMoveVehicle, the spectator camera in ServerPlayer.tick and setCamera). The section-change checks in ChunkMap.tick and addEntity come on top of that. So for players who move (and the bots move every tick) vanilla checks every pair every tick, as Horizon does; it is only lazier for players who send no move that tick. Porting that laziness would change when idle players get entities paired (towards vanilla's timing) and save nothing for moving players, so this PR keeps today's decisions and makes each check cheap instead. The lazy re-check is left as a possible follow-up (see below).

Measured

Mac mini M4 Pro, busy and shared (80–90 % CPU from other sessions), so runs alternate. Flat world, 40 horizon-bots --behaviour walk around spawn, 570 entities summoned within 40 blocks of spawn (cows, sheep, pigs, chickens, villagers, wolves, husks, armor stands, minecarts, boats, items, arrows), 20 s warm-up, /tick query every 5 s for 60 s, sample of the engine for 20 s.

run MSPT avg P50 P95 tick_lan samples track_entities its own code (callees excluded) track_entities / tick_lan
main #1 46.5 33.1 135.9 2180 503 ~343 23 %
this #1 36.2 24.8 105.7 1612 307 ~200 19 %
main #2 28.5 19.5 82.6 811 206 — 25 %
this #2 45.5 32.4 141.8 1722 316 — 18 %

The machine's load swung a lot between runs (main #2 ran while it was quiet), so MSPT isn't comparable run to run here; the share of tick_lan is. The inlined range check and send loop (track_entities' own samples) went from ~343 to ~200.

In-process timing of Entities::tick alone (40 clients walking within 48 blocks, 640 entities, 2000 ticks, median per tick, same machine, a temporary test not in this PR): main 112–116 µs, this 71–75 µs (−37 %), with the same number of packets sent (14,159,154).

How tested

On this commit before its rebase onto e508a5ce (the rebase touched nothing in lan/):

cargo fmt --all --check                                          ok
cargo clippy -p horizon-client --all-targets -- -D warnings      ok
cargo test -p horizon-client --lib                               868 passed, 0 failed
cargo test -p horizon-client --test lan_bot                      2 passed
PARITY_LOCAL_JOBS=2 python3 tools/parity.py server_entities lan dedicated_server \
  server_dimension_players locator_bar entity_components entity_model entity_death_loot entity_particles
                                                                 9 passed, 0 failed (25.7 min)

parity.py --changed origin/main selects 95 harnesses for any change in lan/; the ones above cover entity tracking, and CI parity covers the rest.

New unit test: entities in a chunk the client doesn't hold wait for it, are paired once it arrives, and are dropped when they leave range while the one that walks into a held chunk stays.

Needs Ludovic

Nothing to merge this. A decision for later, if wanted: port vanilla's lazy re-check for players who sent no move that tick (pairs re-checked only on a move packet, a section change of either side, or a new entity). It would save work on servers with many idle players and match vanilla's pairing timing for them, but it changes when those players see entities appear.

🤖 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/864 **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/tracking-sections` --- ### 📝 Commits (1) - [`b171508`](https://github.com/horizon-rust/horizon/commit/b171508c0f5e0211f023aac37c4f27be80bcf1d2) Server: check each entity's range for every client from one compact pass ### 📊 Changes **1 file changed** (+97 additions, -23 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/lan/entity.rs` (+97 -23) </details> ### 📄 Description > 🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself. ## What Entity tracking's per-pair range check (`TrackedEntity.updatePlayer`), the biggest part of `track_entities` after #841, is cheaper: - Each entity's horizontal position, id, tracking range and chunk are read **once a tick** into a compact list, not from its `EntityState` once per client. - Each client looks up **each of the tick's chunks once** in the chunks it holds (remembered per client for the tick), not once per entity standing in it. - An entity with no changes and nobody starting to track it this tick skips the loop over the clients. Which client tracks which entity, and every packet and its order, are unchanged. ## Why not vanilla's "re-check on section changes" shape The brief asked to re-check only when something crosses a section. Reading 26.3, that is only part of vanilla's shape: `ChunkMap.move(player)` runs `updatePlayer(player)` for **every** tracked entity, and it is called from `handlePlayerPositionChange` on every accepted move packet (plus `handleMoveVehicle`, the spectator camera in `ServerPlayer.tick` and `setCamera`). The section-change checks in `ChunkMap.tick` and `addEntity` come on top of that. So for players who move (and the bots move every tick) vanilla checks every pair every tick, as Horizon does; it is only lazier for players who send no move that tick. Porting that laziness would change when idle players get entities paired (towards vanilla's timing) and save nothing for moving players, so this PR keeps today's decisions and makes each check cheap instead. The lazy re-check is left as a possible follow-up (see below). ## Measured Mac mini M4 Pro, busy and shared (80–90 % CPU from other sessions), so runs alternate. Flat world, 40 `horizon-bots --behaviour walk` around spawn, 570 entities summoned within 40 blocks of spawn (cows, sheep, pigs, chickens, villagers, wolves, husks, armor stands, minecarts, boats, items, arrows), 20 s warm-up, `/tick query` every 5 s for 60 s, `sample` of the engine for 20 s. | run | MSPT avg | P50 | P95 | `tick_lan` samples | `track_entities` | its own code (callees excluded) | `track_entities` / `tick_lan` | | --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: | | main #1 | 46.5 | 33.1 | 135.9 | 2180 | 503 | ~343 | 23 % | | this #1 | 36.2 | 24.8 | 105.7 | 1612 | 307 | ~200 | 19 % | | main #2 | 28.5 | 19.5 | 82.6 | 811 | 206 | — | 25 % | | this #2 | 45.5 | 32.4 | 141.8 | 1722 | 316 | — | 18 % | The machine's load swung a lot between runs (main #2 ran while it was quiet), so MSPT isn't comparable run to run here; the share of `tick_lan` is. The inlined range check and send loop (`track_entities`' own samples) went from ~343 to ~200. In-process timing of `Entities::tick` alone (40 clients walking within 48 blocks, 640 entities, 2000 ticks, median per tick, same machine, a temporary test not in this PR): **main 112–116 µs, this 71–75 µs** (−37 %), with the same number of packets sent (14,159,154). ## How tested On this commit before its rebase onto e508a5ce (the rebase touched nothing in `lan/`): ``` cargo fmt --all --check ok cargo clippy -p horizon-client --all-targets -- -D warnings ok cargo test -p horizon-client --lib 868 passed, 0 failed cargo test -p horizon-client --test lan_bot 2 passed PARITY_LOCAL_JOBS=2 python3 tools/parity.py server_entities lan dedicated_server \ server_dimension_players locator_bar entity_components entity_model entity_death_loot entity_particles 9 passed, 0 failed (25.7 min) ``` `parity.py --changed origin/main` selects 95 harnesses for any change in `lan/`; the ones above cover entity tracking, and CI parity covers the rest. New unit test: entities in a chunk the client doesn't hold wait for it, are paired once it arrives, and are dropped when they leave range while the one that walks into a held chunk stays. ## Needs Ludovic Nothing to merge this. A decision for later, if wanted: port vanilla's lazy re-check for players who sent no move that tick (pairs re-checked only on a move packet, a section change of either side, or a new entity). It would save work on servers with many idle players and match vanilla's pairing timing for them, but it changes when those players see entities appear. 🤖 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:43 +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#2868
No description provided.