[PR #7094] [MERGED] Saving goes through the entity trait #7332

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

📋 Pull Request Information

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

Base: main ← Head: refactor/entity-walk-saves


📝 Commits (3)

  • bc250b4 Save entities through the Entity trait
  • f3dd3a3 Merge remote-tracking branch 'origin/main' into refactor/entity-walk-saves
  • 61a8cb8 Put strip_finalize's doc comment back on it

📊 Changes

2 files changed (+345 additions, -180 deletions)

View changed files

📝 crates/horizon-client/src/game/entity.rs (+121 -0)
📝 crates/horizon-client/src/game/persist.rs (+224 -180)

📄 Description

🤖 Opened by Valkyrie's AI agent (Claude Code, Opus 5.5), not by Valkyrie herself.

What and why

Slice 10 of #6509, part of #6545 (save half): the per-kind save loops in entity_chunks (persist.rs) now go through the Entity trait.

  • New required trait method Entity::save_nbt(game, index, keys) -> Option<(chunk, Tag)>, implemented for the 11 kinds in entity_lists! (mobs, items, boats, carts, armor stands, TNT, falling blocks, orbs, projectiles, rockets, tridents). The bodies moved unchanged into Game::save_* methods in persist.rs; the trait impl calls them.
  • One macro-generated loop per list (save_lists!, same pattern as for_each_entity): static dispatch, no dyn, no Vec per walk. The Vec<usize> index lists the old mob, item, boat and cart loops built are gone.
  • save_nbt has no default, so a kind added to entity_lists! cannot be left out of saving (that was the cause of #6424 and #5641).
  • Dragon, end crystals, ominous item spawners, item frames, cushions, paintings and unknown compounds are not in entity_lists! and keep their loops.
  • About 345 lines added, 180 removed (net +165); most of the diff is moved code.

Not in this PR: the load side (spawn_loaded_entities / load_mob / load_other, about 340 lines matching on the id string). It is a dispatch by id, not a walk, and does not fit in this slice. Suggested split: a follow-up slice 10b with Entity::load_nbt per kind.

One change you will see: entities within one chunk are now written in entity_lists! order (mobs, items, boats, carts, armor stands, TNT, falling blocks, orbs, projectiles, rockets, tridents, then the unlisted kinds) instead of the old loop order (mobs, items, orbs, projectiles, falling blocks, rockets, tridents, TNT, boats, dragon, crystals, spawners, carts, armor stands, frames, cushions, paintings, others). Order inside one kind is unchanged. No field changes. Vanilla's own order is different from both. Loading assigns entity ids in file order, so ids of different kinds can differ from before; kinds' own tick order is unchanged.

Part of #6545

Kind

  • parity: Horizon now does what vanilla 26.3 does
  • bug: a crash or a breakage
  • perf: faster, or less memory
  • feature: something new for players (listed under New in the release notes)
  • extra: Horizon-only, off by default
  • tooling: tools, CI, tests, harnesses
  • docs

Vanilla reference

Real 26.3 server.jar as the oracle, same console script on both: forceload, tick freeze, summon, tick step 40, save-all flush, stop, read the entity region files, restart, save-all flush, stop, read again. Vanilla's chunk entity list order is its own and is not asserted here.

Proof

World with every kind Horizon simulates plus the unsimulated ones (98 summons x 2 groups): 20 plain mobs, tamed wolf, cat and horse (with a rider), a leashed pig and zombie, a leashed boat, a cow as lead holder, 5 boat types, a boat with two passengers, 7 cart types, a cart with a rider, portal cooldowns on pig, zombie, boat, cart, item, TNT and falling block, armor stand, item frames, end crystal, item, orb, TNT, falling block, 19 projectile and thrown kinds, area effect cloud, spawner, marker, interaction and displays. One group is forceloaded and one is unloaded (forceload removed, chunk unloads, entity file written). Painting summon is refused by Horizon, so those 2 are not in the Horizon worlds. Summon NBT carries fixed UUIDs.

Round trip (save, restart, save), field by field (3,356 fields compared, entities matched by id and position):

comparison entities differing fields
Horizon before vs Horizon after, first save 157 / 157 14, all random UUIDs
Horizon before vs a second run of Horizon before (noise) 157 / 157 14, same random UUIDs
Horizon before vs after, after the reload save 157 / 157 23: the 14 UUIDs, plus Fire, Health, HurtTime of sunburnt zombies and skeletons
Horizon before vs a second run of before (noise) 157 / 157 26: the same kinds of fields, plus boat passenger Rotation

So the change writes the same NBT as before; every difference also differs between two runs of the old build. The random UUIDs are on item, bee, trident, splash potion, arrow and pig entries whose summoned UUID Horizon does not keep; they change between two runs of the old build as well, so this is existing behaviour, cause not investigated. Raw dumps and diffs: ~/horizon/.scout/e10/proof/.

Vanilla vs Horizon (not changed by this PR, listed so nobody reads it as new): vanilla saved 166 entities, Horizon 157 to 159 (item frames, wind charges and breeze wind charges among the missing kinds). 517 fields differ in the first-save comparison (for example movement speed in attributes, random mob fields); the old and new build show the same differences. Cause of each not investigated.

Vanilla save loaded in Horizon, saved, loaded in vanilla (old and new build give the same result): of 166 entities, 8 do not survive the Horizon step: 2 wind charges, 2 breeze wind charges, 2 lingering potions (come back as splash potions), 1 firework rocket, 1 ominous item spawner; plus one arrow changes. All other entities, including passengers, leashed and tamed mobs, portal cooldowns and the unloaded chunks, are still there after vanilla loads the Horizon-saved world. Two ender eyes were missing in the vanilla reload of the old build's world, but also in a rerun on the new build's world; the saved eye NBT is byte-for-byte equal in both, and vanilla removes an eye after 80 ticks, so it is a timing effect of the harness, not of the save.

Order change: see above (1 chunk, 32 entities, listed in both orders in the proof folder).

Save time

Server with 1,655 entities on disk in 192 loaded chunks, save-all flush, under flock, machine load average 10 to 20 from other jobs:

build first flush after generation second and third flush
before 0.30, 0.40, 59.25 s 0.05, 0.05, 0.30, 0.30 s
after 3.16, 0.40, 0.60 s 0.10, 0.10, 0.05, 0.10 s

I ran more than the planned single round because the first pair (0.30 s before, 3.16 s after) looked like a 10x slowdown. The extra runs show the first flush swings from 0.3 to 59 s for the old build under load, so I cannot claim a speed difference either way; resolution of the timer is 0.05 s. A tick-cost number is not relevant here (the save does not run in the tick).

Harnesses

Run locally in the build fence with DISPLAY=:0 HORIZON_E2E_HEADLESS=1. CI runs the full --changed set.

PASS arrow 151s
PASS world_reopen 231s
PASS remote_fireworks 256s
PASS save_roundtrip 402s
PASS server_entities 480s
PASS fishing_hook_entities 524s
PASS armor_stand_frost 584s
PASS boat_submerged 587s
PASS boat_authority 461s
PASS mob_ai 2137s

lan_e2e.py has no save check (only check_sleep_reload), so none was run. pr_ready --quick: public tree, Python tests (611), rustfmt, clippy and horizon-client tests (653) all pass.

A refactor with no behaviour change; proved by script, no human played this change.

Other projects' code

none

Self-check

pr_ready (tools/pr_ready.py --quick on Valhalla, against origin/main at 185ebe0a6, 6 min 39 s)

Check Result
Scope ⚠️ 2 files, +345 −180; 525 lines of code: over 500, split it if it can ship in parts
Cargo.lock ✅ unchanged
Public tree ✅
Python tests ✅ 612 passed (33 s)
rustfmt ✅ (8 s)
Rust ⚠️ 1.98.0 is older than CI's stable (1.99): clippy may flag lints CI doesn't, update with rustup update
clippy ✅ (2 min 57 s)
tests: horizon-client ✅ 662 passed (2 min 35 s)
doc tests ✅ 0 passed (2 s)
content root tests ➖ content root untouched
mod API up to date ✅ (23 s)
tokens.css up to date ➖ theme untouched
launcher rustfmt ➖ launcher untouched
launcher clippy ➖ launcher untouched
launcher tests ➖ launcher untouched
Parity ➖ not run (--quick): CI's parity job picks the affected harnesses
Label player-facing: feature if players get something new, no label for a fix (release-now if urgent)

🤖 Generated with Claude Code

https://claude.ai/code/session_018zuDTsnyHkP28bzn3u7S3m


🔄 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/7094 **Author:** [@HrafnaValkyrja](https://github.com/HrafnaValkyrja) **Created:** 11/2/2026 **Status:** ✅ Merged **Merged:** 11/2/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `refactor/entity-walk-saves` --- ### 📝 Commits (3) - [`bc250b4`](https://github.com/horizon-rust/horizon/commit/bc250b47dab404797512d06edbd023075a1c3886) Save entities through the Entity trait - [`f3dd3a3`](https://github.com/horizon-rust/horizon/commit/f3dd3a374719c89725c7884be8982fcccf665cf5) Merge remote-tracking branch 'origin/main' into refactor/entity-walk-saves - [`61a8cb8`](https://github.com/horizon-rust/horizon/commit/61a8cb83cc762e609e42f66632150ff3c7ac58d6) Put strip_finalize's doc comment back on it ### 📊 Changes **2 files changed** (+345 additions, -180 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/game/entity.rs` (+121 -0) 📝 `crates/horizon-client/src/game/persist.rs` (+224 -180) </details> ### 📄 Description > 🤖 Opened by Valkyrie's AI agent (Claude Code, Opus 5.5), not by Valkyrie herself. ## What and why Slice 10 of #6509, part of #6545 (save half): the per-kind save loops in `entity_chunks` (`persist.rs`) now go through the `Entity` trait. - New required trait method `Entity::save_nbt(game, index, keys) -> Option<(chunk, Tag)>`, implemented for the 11 kinds in `entity_lists!` (mobs, items, boats, carts, armor stands, TNT, falling blocks, orbs, projectiles, rockets, tridents). The bodies moved unchanged into `Game::save_*` methods in `persist.rs`; the trait impl calls them. - One macro-generated loop per list (`save_lists!`, same pattern as `for_each_entity`): static dispatch, no `dyn`, no `Vec` per walk. The `Vec<usize>` index lists the old mob, item, boat and cart loops built are gone. - `save_nbt` has no default, so a kind added to `entity_lists!` cannot be left out of saving (that was the cause of #6424 and #5641). - Dragon, end crystals, ominous item spawners, item frames, cushions, paintings and unknown compounds are not in `entity_lists!` and keep their loops. - About 345 lines added, 180 removed (net +165); most of the diff is moved code. Not in this PR: the load side (`spawn_loaded_entities` / `load_mob` / `load_other`, about 340 lines matching on the id string). It is a dispatch by id, not a walk, and does not fit in this slice. Suggested split: a follow-up slice 10b with `Entity::load_nbt` per kind. **One change you will see:** entities within one chunk are now written in `entity_lists!` order (mobs, items, boats, carts, armor stands, TNT, falling blocks, orbs, projectiles, rockets, tridents, then the unlisted kinds) instead of the old loop order (mobs, items, orbs, projectiles, falling blocks, rockets, tridents, TNT, boats, dragon, crystals, spawners, carts, armor stands, frames, cushions, paintings, others). Order inside one kind is unchanged. No field changes. Vanilla's own order is different from both. Loading assigns entity ids in file order, so ids of different kinds can differ from before; kinds' own tick order is unchanged. Part of #6545 ## Kind - [ ] parity: Horizon now does what vanilla 26.3 does - [ ] bug: a crash or a breakage - [ ] perf: faster, or less memory - [ ] feature: something new for players (listed under New in the release notes) - [ ] extra: Horizon-only, off by default - [x] tooling: tools, CI, tests, harnesses - [ ] docs ## Vanilla reference Real 26.3 `server.jar` as the oracle, same console script on both: forceload, `tick freeze`, summon, `tick step 40`, `save-all flush`, stop, read the entity region files, restart, `save-all flush`, stop, read again. Vanilla's chunk entity list order is its own and is not asserted here. ## Proof World with every kind Horizon simulates plus the unsimulated ones (98 summons x 2 groups): 20 plain mobs, tamed wolf, cat and horse (with a rider), a leashed pig and zombie, a leashed boat, a cow as lead holder, 5 boat types, a boat with two passengers, 7 cart types, a cart with a rider, portal cooldowns on pig, zombie, boat, cart, item, TNT and falling block, armor stand, item frames, end crystal, item, orb, TNT, falling block, 19 projectile and thrown kinds, area effect cloud, spawner, marker, interaction and displays. One group is forceloaded and one is unloaded (forceload removed, chunk unloads, entity file written). Painting `summon` is refused by Horizon, so those 2 are not in the Horizon worlds. Summon NBT carries fixed UUIDs. Round trip (save, restart, save), field by field (3,356 fields compared, entities matched by id and position): | comparison | entities | differing fields | |---|---|---| | Horizon before vs Horizon after, first save | 157 / 157 | 14, all random UUIDs | | Horizon before vs a second run of Horizon before (noise) | 157 / 157 | 14, same random UUIDs | | Horizon before vs after, after the reload save | 157 / 157 | 23: the 14 UUIDs, plus Fire, Health, HurtTime of sunburnt zombies and skeletons | | Horizon before vs a second run of before (noise) | 157 / 157 | 26: the same kinds of fields, plus boat passenger Rotation | So the change writes the same NBT as before; every difference also differs between two runs of the old build. The random UUIDs are on item, bee, trident, splash potion, arrow and pig entries whose summoned UUID Horizon does not keep; they change between two runs of the old build as well, so this is existing behaviour, cause not investigated. Raw dumps and diffs: `~/horizon/.scout/e10/proof/`. Vanilla vs Horizon (not changed by this PR, listed so nobody reads it as new): vanilla saved 166 entities, Horizon 157 to 159 (item frames, wind charges and breeze wind charges among the missing kinds). 517 fields differ in the first-save comparison (for example movement speed in `attributes`, random mob fields); the old and new build show the same differences. Cause of each not investigated. Vanilla save loaded in Horizon, saved, loaded in vanilla (old and new build give the same result): of 166 entities, 8 do not survive the Horizon step: 2 wind charges, 2 breeze wind charges, 2 lingering potions (come back as splash potions), 1 firework rocket, 1 ominous item spawner; plus one arrow changes. All other entities, including passengers, leashed and tamed mobs, portal cooldowns and the unloaded chunks, are still there after vanilla loads the Horizon-saved world. Two ender eyes were missing in the vanilla reload of the old build's world, but also in a rerun on the new build's world; the saved eye NBT is byte-for-byte equal in both, and vanilla removes an eye after 80 ticks, so it is a timing effect of the harness, not of the save. Order change: see above (1 chunk, 32 entities, listed in both orders in the proof folder). ## Save time Server with 1,655 entities on disk in 192 loaded chunks, `save-all flush`, under `flock`, machine load average 10 to 20 from other jobs: | build | first flush after generation | second and third flush | |---|---|---| | before | 0.30, 0.40, 59.25 s | 0.05, 0.05, 0.30, 0.30 s | | after | 3.16, 0.40, 0.60 s | 0.10, 0.10, 0.05, 0.10 s | I ran more than the planned single round because the first pair (0.30 s before, 3.16 s after) looked like a 10x slowdown. The extra runs show the first flush swings from 0.3 to 59 s for the old build under load, so I cannot claim a speed difference either way; resolution of the timer is 0.05 s. A tick-cost number is not relevant here (the save does not run in the tick). ## Harnesses Run locally in the build fence with `DISPLAY=:0 HORIZON_E2E_HEADLESS=1`. CI runs the full `--changed` set. ``` PASS arrow 151s PASS world_reopen 231s PASS remote_fireworks 256s PASS save_roundtrip 402s PASS server_entities 480s PASS fishing_hook_entities 524s PASS armor_stand_frost 584s PASS boat_submerged 587s PASS boat_authority 461s PASS mob_ai 2137s ``` `lan_e2e.py` has no save check (only `check_sleep_reload`), so none was run. `pr_ready --quick`: public tree, Python tests (611), rustfmt, clippy and horizon-client tests (653) all pass. A refactor with no behaviour change; proved by script, no human played this change. ## Other projects' code none ## Self-check **pr_ready** (`tools/pr_ready.py --quick` on Valhalla, against origin/main at 185ebe0a6, 6 min 39 s) | Check | Result | | --- | --- | | Scope | ⚠️ 2 files, +345 −180; 525 lines of code: over 500, split it if it can ship in parts | | Cargo.lock | ✅ unchanged | | Public tree | ✅ | | Python tests | ✅ 612 passed (33 s) | | rustfmt | ✅ (8 s) | | Rust | ⚠️ 1.98.0 is older than CI's stable (1.99): clippy may flag lints CI doesn't, update with `rustup update` | | clippy | ✅ (2 min 57 s) | | tests: horizon-client | ✅ 662 passed (2 min 35 s) | | doc tests | ✅ 0 passed (2 s) | | content root tests | ➖ content root untouched | | mod API up to date | ✅ (23 s) | | tokens.css up to date | ➖ theme untouched | | launcher rustfmt | ➖ launcher untouched | | launcher clippy | ➖ launcher untouched | | launcher tests | ➖ launcher untouched | | Parity | ➖ not run (--quick): CI's parity job picks the affected harnesses | | 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) https://claude.ai/code/session_018zuDTsnyHkP28bzn3u7S3m --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-03 06:37:51 +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#7332
No description provided.