[PR #6755] [MERGED] Find a multiplayer client's mobs by id through an index #7074

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

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon/pull/6755
Author: @ludovic111
Created: 11/2/2026
Status: ✅ Merged
Merged: 11/2/2026
Merged by: @ludovic111

Base: main ← Head: perf/remote-mob-index


📝 Commits (3)

  • 20e429c Find a multiplayer client's mobs by id through an index
  • 79a60de Merge remote-tracking branch 'origin/main' into perf/remote-mob-index
  • c4f9b6f tests: the remote motion fixture looks mobs up through MobIndex

📊 Changes

6 files changed (+129 additions, -28 deletions)

View changed files

📝 crates/horizon-client/src/game.rs (+4 -0)
➕ crates/horizon-client/src/game/mob_index.rs (+90 -0)
📝 crates/horizon-client/src/game/remote.rs (+23 -24)
📝 crates/horizon-client/src/game/remote_mob_data.rs (+1 -1)
📝 crates/horizon-client/src/game/remote_vehicles.rs (+2 -2)
📝 tests/test_remote_motion.py (+9 -1)

📄 Description

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

What

On a multiplayer client (servers, LAN guests, and single player on its server thread), every movement, entity data, motion, attribute, equipment and entity event packet looked its mob up with self.mobs.iter_mut().find(|m| m.id == id). A Mob is about 3.3 KB, so each compared id costs a cache miss. The cost grows with mobs × packets, which is quadratic in farm size.

game/mob_index.rs keeps an id → position map, and every hit is checked against the list. A miss, or a stale slot, falls back to the linear search and rebuilds the map. The ~35 places that add, remove or reorder mobs need no change, and every answer equals the linear search's. Ids are unique because spawn_entity drops an old mob with the same id first. About 25 lookups in remote.rs, remote_mob_data.rs and remote_vehicles.rs now go through it.

  • finds_what_a_linear_search_finds_after_any_change: after pushes, removals, swaps, reorders and id reuse, it always returns what position returns.

Measured: lookups per tick, linear vs index

Reported by perf-net-client. Measured on playhorizon with thread CPU time, one movement packet per three mobs per tick (vanilla sends movement to moving mobs only):

Mobs Lookups per tick Linear search Index Change
100 33 0.002 ms 0.0005 ms ≈ (both negligible)
1000 333 0.110 ms 0.002 ms −98%
3000 1000 5.505 ms 0.0125 ms −99.8%

At 3000 mobs, the search alone took 5.5 ms of every 50 ms client tick; the index takes 0.0125 ms.

Checks

These ran on playhorizon instead of pr_ready.py (the slot-limited box):

head c4f9b6fc7f (79a60dea88 + tests/test_remote_motion.py's fixture given a MobIndex stub)
cargo fmt --all -- --check                                   ok
cargo clippy --profile fast -p horizon-client -p horizon-client-lib -p horizon-client-render --lib --bins --tests -- -D warnings   ok, 0 diagnostics
cargo test --workspace --lib --bins --tests (CI verify)     pass, including mob_index's test
python3 -m unittest discover -s tests                        602 ok, 1 skipped

🤖 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/6755 **Author:** [@ludovic111](https://github.com/ludovic111) **Created:** 11/2/2026 **Status:** ✅ Merged **Merged:** 11/2/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `perf/remote-mob-index` --- ### 📝 Commits (3) - [`20e429c`](https://github.com/horizon-rust/horizon/commit/20e429c3f8f225a38c9793e86488a7860c6cb1f0) Find a multiplayer client's mobs by id through an index - [`79a60de`](https://github.com/horizon-rust/horizon/commit/79a60dea88808ebfe5609252eba371b1e2b8cee1) Merge remote-tracking branch 'origin/main' into perf/remote-mob-index - [`c4f9b6f`](https://github.com/horizon-rust/horizon/commit/c4f9b6fc7f2938ab41613a7c061f133442592252) tests: the remote motion fixture looks mobs up through MobIndex ### 📊 Changes **6 files changed** (+129 additions, -28 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/game.rs` (+4 -0) ➕ `crates/horizon-client/src/game/mob_index.rs` (+90 -0) 📝 `crates/horizon-client/src/game/remote.rs` (+23 -24) 📝 `crates/horizon-client/src/game/remote_mob_data.rs` (+1 -1) 📝 `crates/horizon-client/src/game/remote_vehicles.rs` (+2 -2) 📝 `tests/test_remote_motion.py` (+9 -1) </details> ### 📄 Description > 🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself. ## What On a multiplayer client (servers, LAN guests, and single player on its server thread), every movement, entity data, motion, attribute, equipment and entity event packet looked its mob up with `self.mobs.iter_mut().find(|m| m.id == id)`. A `Mob` is about 3.3 KB, so each compared id costs a cache miss. The cost grows with mobs × packets, which is quadratic in farm size. `game/mob_index.rs` keeps an id → position map, and every hit is checked against the list. A miss, or a stale slot, falls back to the linear search and rebuilds the map. The ~35 places that add, remove or reorder `mobs` need no change, and every answer equals the linear search's. Ids are unique because `spawn_entity` drops an old mob with the same id first. About 25 lookups in `remote.rs`, `remote_mob_data.rs` and `remote_vehicles.rs` now go through it. - `finds_what_a_linear_search_finds_after_any_change`: after pushes, removals, swaps, reorders and id reuse, it always returns what `position` returns. ## Measured: lookups per tick, linear vs index Reported by perf-net-client. Measured on playhorizon with thread CPU time, one movement packet per three mobs per tick (vanilla sends movement to moving mobs only): | Mobs | Lookups per tick | Linear search | Index | Change | |---|---|---|---|---| | 100 | 33 | 0.002 ms | 0.0005 ms | ≈ (both negligible) | | 1000 | 333 | 0.110 ms | 0.002 ms | −98% | | 3000 | 1000 | 5.505 ms | 0.0125 ms | −99.8% | At 3000 mobs, the search alone took 5.5 ms of every 50 ms client tick; the index takes 0.0125 ms. ## Checks These ran on playhorizon instead of `pr_ready.py` (the slot-limited box): ``` head c4f9b6fc7f (79a60dea88 + tests/test_remote_motion.py's fixture given a MobIndex stub) cargo fmt --all -- --check ok cargo clippy --profile fast -p horizon-client -p horizon-client-lib -p horizon-client-render --lib --bins --tests -- -D warnings ok, 0 diagnostics cargo test --workspace --lib --bins --tests (CI verify) pass, including mob_index's test python3 -m unittest discover -s tests 602 ok, 1 skipped ``` 🤖 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 06:33: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#7074
No description provided.