[PR #5774] [MERGED] Mob ticks: no copy of every mob each tick; FxHash for the per-mob sets #6488

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

📋 Pull Request Information

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

Base: main ← Head: perf/mob-tick-no-copy


📝 Commits (1)

  • ab376df Mob ticks: no copy of every mob each tick; a fast hasher for the per-mob sets

📊 Changes

3 files changed (+16 additions, -7 deletions)

View changed files

📝 crates/horizon-client/src/game.rs (+3 -2)
📝 crates/horizon-client/src/game/creatures.rs (+9 -2)
📝 crates/horizon-client/src/game/lan.rs (+4 -3)

📄 Description

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

A profile of a 20-bot server (macOS sample, 40 s) showed waste in tick_creature_entities that vanilla doesn't have:

  • Every mob copied each tick. The function takes the mob list (std::mem::take(&mut self.mobs)), ticks it, then ran self.mobs.append(&mut mobs). With self.mobs empty, append allocates a new buffer and copies every Mob into it. The old buffer is then freed, which costs a madvise purge. In the profile: a huge-page allocation from RawVec::reserve, memmove and madvise, about 10 % of the function's samples. When no mob was added during the tick, the ticked list now goes back as it is. Otherwise append runs as before, so the order is unchanged: mobs added during the tick come first.
  • SipHash on every mob. Game.died (the mobs whose death already ran) and LanHost.mob_players (the guest each mob sees) are looked up 2 to 4 times per mob per tick, and they used std's SipHash. They now use FxHash. Neither is ever iterated, so nothing depends on their order.

Which mobs tick, in what order, and with which RNG draws is unchanged.

Measurement

playhorizon (EPYC, CPUs 0-3, exclusive), server_bench.py horizon, 20 mixed bots, 3 alternated rounds. Base is main 3bc251ea5, test is this PR.

ms/tick round 1 round 2 round 3 median
tick_creature_entities, main 5.08 4.95 4.82 4.95
tick_creature_entities, this PR 4.26 4.32 4.33 4.32 (−13 %)
tick_mobs, main 9.39 9.27 9.11 9.27
tick_mobs, this PR 8.39 8.46 8.48 8.46
MSPT, main 26.94 26.59 26.50 26.59
MSPT, this PR 25.38 25.78 25.85 25.78 (−3 %)

Checks

pr_ready (tools/pr_ready.py --quick on ludovics-mac-mini, against origin/main at 3bc251ea5, 8 min 9 s)

Check Result
Scope ✅ 3 files, +16 −7
Cargo.lock ✅ unchanged
Public tree ✅
Python tests ✅ 511 passed (1 min 47 s)
rustfmt ✅ (5 s)
clippy ✅ (1 min 22 s)
tests: horizon-client ✅ 1655 passed (4 min 44 s)
doc tests ✅ 0 passed (1 s)
content root tests ➖ content root untouched
tokens.css up to date ➖ theme untouched
launcher rustfmt ➖ launcher untouched
launcher clippy ➖ launcher untouched
launcher tests ➖ launcher untouched
Parity ➖ 34 affected, to run in CI (--quick): block_removal, command_exec, composter, crafter, cursor_death, dedicated_server, dispenser, forced_end, friends_integration, hardcore_respawn, lan_integration, locator_bar and 22 more
Label player-facing: feature if players get something new, no label for a fix (release-now if urgent)

🤖 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/5774 **Author:** [@ludovic111](https://github.com/ludovic111) **Created:** 11/1/2026 **Status:** ✅ Merged **Merged:** 11/1/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `perf/mob-tick-no-copy` --- ### 📝 Commits (1) - [`ab376df`](https://github.com/horizon-rust/horizon/commit/ab376df4e03907f53956de1b610163b097d417ab) Mob ticks: no copy of every mob each tick; a fast hasher for the per-mob sets ### 📊 Changes **3 files changed** (+16 additions, -7 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/game.rs` (+3 -2) 📝 `crates/horizon-client/src/game/creatures.rs` (+9 -2) 📝 `crates/horizon-client/src/game/lan.rs` (+4 -3) </details> ### 📄 Description > 🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself. A profile of a 20-bot server (macOS `sample`, 40 s) showed waste in `tick_creature_entities` that vanilla doesn't have: - **Every mob copied each tick.** The function takes the mob list (`std::mem::take(&mut self.mobs)`), ticks it, then ran `self.mobs.append(&mut mobs)`. With `self.mobs` empty, `append` allocates a new buffer and copies every `Mob` into it. The old buffer is then freed, which costs a `madvise` purge. In the profile: a huge-page allocation from `RawVec::reserve`, `memmove` and `madvise`, about 10 % of the function's samples. When no mob was added during the tick, the ticked list now goes back as it is. Otherwise `append` runs as before, so the order is unchanged: mobs added during the tick come first. - **SipHash on every mob.** `Game.died` (the mobs whose death already ran) and `LanHost.mob_players` (the guest each mob sees) are looked up 2 to 4 times per mob per tick, and they used std's SipHash. They now use FxHash. Neither is ever iterated, so nothing depends on their order. Which mobs tick, in what order, and with which RNG draws is unchanged. ### Measurement playhorizon (EPYC, CPUs 0-3, exclusive), `server_bench.py` horizon, 20 mixed bots, 3 alternated rounds. Base is main 3bc251ea5, test is this PR. | ms/tick | round 1 | round 2 | round 3 | median | |---|---|---|---|---| | `tick_creature_entities`, main | 5.08 | 4.95 | 4.82 | 4.95 | | `tick_creature_entities`, this PR | 4.26 | 4.32 | 4.33 | **4.32 (−13 %)** | | `tick_mobs`, main | 9.39 | 9.27 | 9.11 | 9.27 | | `tick_mobs`, this PR | 8.39 | 8.46 | 8.48 | 8.46 | | MSPT, main | 26.94 | 26.59 | 26.50 | 26.59 | | MSPT, this PR | 25.38 | 25.78 | 25.85 | **25.78 (−3 %)** | ### Checks **pr_ready** (`tools/pr_ready.py --quick` on ludovics-mac-mini, against origin/main at 3bc251ea5, 8 min 9 s) | Check | Result | | --- | --- | | Scope | ✅ 3 files, +16 −7 | | Cargo.lock | ✅ unchanged | | Public tree | ✅ | | Python tests | ✅ 511 passed (1 min 47 s) | | rustfmt | ✅ (5 s) | | clippy | ✅ (1 min 22 s) | | tests: horizon-client | ✅ 1655 passed (4 min 44 s) | | doc tests | ✅ 0 passed (1 s) | | content root tests | ➖ content root untouched | | tokens.css up to date | ➖ theme untouched | | launcher rustfmt | ➖ launcher untouched | | launcher clippy | ➖ launcher untouched | | launcher tests | ➖ launcher untouched | | Parity | ➖ 34 affected, to run in CI (--quick): block_removal, command_exec, composter, crafter, cursor_death, dedicated_server, dispenser, forced_end, friends_integration, hardcore_respawn, lan_integration, locator_bar and 22 more | | Label | player-facing: `feature` if players get something new, no label for a fix (`release-now` if urgent) | 🤖 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:21:24 +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#6488
No description provided.