[PR #1794] [MERGED] PalettedContainer: look up a word's filled bits, not divide for them #3765

Closed
opened 2026-11-03 04:12:09 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

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

Base: main ← Head: perf/paletted-locate-no-div


📝 Commits (1)

  • 40c77c0 PalettedContainer: look up a word's filled bits, not divide for them

📊 Changes

1 file changed (+21 additions, -8 deletions)

View changed files

📝 crates/horizon-core/src/paletted.rs (+21 -8)

📄 Description

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

PalettedContainer's locate already found an entry's word by multiplication (the MAGIC table). It still computed 64 / bits at run time for the entry's shift. That is a division on every block read and write of a section stored in 5, 6 or 7 bits, which is most terrain sections. The table now also holds the bits a word's entries fill (64 / bits * bits), and the shift is i * bits - word * filled. A new test checks locate against the division for every width from 1 to 32 bits and every entry of a section.

Why

I profiled the dedicated server's tick on the ThinkPad (i5-7200U, server_bench --bots 10 --behaviour walk, perf record --call-graph lbr on the server process):

  • PalettedContainer::get is the server thread's top function, at about 20% of its samples. The calls come from random ticks in tick_blocks and from the block-collision scans of mobs and dropped items.
  • Most of that time is cache misses.
  • 12% of the function's samples sit on the div.

The x86 division costs tens of cycles, and the compiler cannot remove it because bits is only known at run time.

Measured

ThinkPad, a throwaway microbench (not committed): 4 M random gets, best of 7, two alternated runs of each build. The 1-minute load was 2.7–3.3.

palette bits main, 2,000 sections this branch main, one section this branch
4 (shift path, unchanged) 22.0–23.0 ns 21.1–21.5 ns 6.0 ns 5.2 ns
5 28.5–28.7 24.6–24.7 6.8–6.9 5.4
6 31.9–32.2 27.9–28.2 6.9 4.9–5.4
7 36.0–36.9 31.4–32.4 6.7–7.1 5.4–5.8
8 (shift path, unchanged) 47.6–48.2 47.8–48.2 6.3–7.0 5.7–6.5

That is −12 to −14% per read for 5–7-bit sections when the read misses the cache, and −20% when it hits. Both builds return the same sums.

A whole-tick A/B with 10 bots was noise: CPU per tick was 17.1 and 12.2 ms on main, 16.9 and 15.3 ms here, with the load at 1–4.6 and the bots on the same 4 threads. From the profile share, the expected gain is about 2% of the server thread. Meshing, lighting and chunk encoding read the same containers.

Self-check

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

Check Result
Scope ✅ 1 files, +21 −8
Cargo.lock ✅ unchanged
Public tree ✅ (1 s)
Python tests ➖ no Python change
rustfmt ✅ (43 s)
clippy ✅ (1 min 6 s)
tests: horizon-core ✅ 113 passed (30 s)
doc tests ✅ 0 passed (1 s)
tokens.css up to date ➖ theme untouched
launcher rustfmt ➖ launcher untouched
launcher clippy ➖ launcher untouched
launcher tests ➖ launcher untouched
Parity ➖ 329 affected, to run in CI (--quick): active_effect_tick, active_effects, ambient_sound, anchor, arrow, attribute_health, attribute_map_queries, attribute, attribute_updates, audio_attributes, auto_jump, bamboo and 317 more
Label player-facing: feature if players get something new, no label for a fix (release-now if urgent)

Named harnesses for what reads the container (tools/parity.py paletted_container bulk_section save_roundtrip random_tick random_tick_sections light section_mesh lan chunk_generation): 9 passed, 0 failed (5.2 min, on the previous base, before the rebase over #1789). CI's parity job runs the rest.

🤖 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/1794 **Author:** [@ludovic111](https://github.com/ludovic111) **Created:** 10/25/2026 **Status:** ✅ Merged **Merged:** 10/25/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `perf/paletted-locate-no-div` --- ### 📝 Commits (1) - [`40c77c0`](https://github.com/horizon-rust/horizon/commit/40c77c02f0332188ae7622dd7b2ae842df5becb7) PalettedContainer: look up a word's filled bits, not divide for them ### 📊 Changes **1 file changed** (+21 additions, -8 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-core/src/paletted.rs` (+21 -8) </details> ### 📄 Description > 🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself. `PalettedContainer`'s `locate` already found an entry's word by multiplication (the MAGIC table). It still computed `64 / bits` at run time for the entry's shift. That is a division on every block read and write of a section stored in 5, 6 or 7 bits, which is most terrain sections. The table now also holds the bits a word's entries fill (`64 / bits * bits`), and the shift is `i * bits - word * filled`. A new test checks `locate` against the division for every width from 1 to 32 bits and every entry of a section. ## Why I profiled the dedicated server's tick on the ThinkPad (i5-7200U, `server_bench --bots 10 --behaviour walk`, `perf record --call-graph lbr` on the server process): - `PalettedContainer::get` is the server thread's top function, at about 20% of its samples. The calls come from random ticks in `tick_blocks` and from the block-collision scans of mobs and dropped items. - Most of that time is cache misses. - 12% of the function's samples sit on the `div`. The x86 division costs tens of cycles, and the compiler cannot remove it because `bits` is only known at run time. ## Measured ThinkPad, a throwaway microbench (not committed): 4 M random `get`s, best of 7, two alternated runs of each build. The 1-minute load was 2.7–3.3. | palette bits | main, 2,000 sections | this branch | main, one section | this branch | |---|---|---|---|---| | 4 (shift path, unchanged) | 22.0–23.0 ns | 21.1–21.5 ns | 6.0 ns | 5.2 ns | | 5 | 28.5–28.7 | 24.6–24.7 | 6.8–6.9 | 5.4 | | 6 | 31.9–32.2 | 27.9–28.2 | 6.9 | 4.9–5.4 | | 7 | 36.0–36.9 | 31.4–32.4 | 6.7–7.1 | 5.4–5.8 | | 8 (shift path, unchanged) | 47.6–48.2 | 47.8–48.2 | 6.3–7.0 | 5.7–6.5 | That is −12 to −14% per read for 5–7-bit sections when the read misses the cache, and −20% when it hits. Both builds return the same sums. A whole-tick A/B with 10 bots was noise: CPU per tick was 17.1 and 12.2 ms on main, 16.9 and 15.3 ms here, with the load at 1–4.6 and the bots on the same 4 threads. From the profile share, the expected gain is about 2% of the server thread. Meshing, lighting and chunk encoding read the same containers. ## Self-check **pr_ready** (`tools/pr_ready.py --quick` on ludovics-mac-mini, against origin/main at d2c96af14, 3 min 26 s) | Check | Result | | --- | --- | | Scope | ✅ 1 files, +21 −8 | | Cargo.lock | ✅ unchanged | | Public tree | ✅ (1 s) | | Python tests | ➖ no Python change | | rustfmt | ✅ (43 s) | | clippy | ✅ (1 min 6 s) | | tests: horizon-core | ✅ 113 passed (30 s) | | doc tests | ✅ 0 passed (1 s) | | tokens.css up to date | ➖ theme untouched | | launcher rustfmt | ➖ launcher untouched | | launcher clippy | ➖ launcher untouched | | launcher tests | ➖ launcher untouched | | Parity | ➖ 329 affected, to run in CI (--quick): active_effect_tick, active_effects, ambient_sound, anchor, arrow, attribute_health, attribute_map_queries, attribute, attribute_updates, audio_attributes, auto_jump, bamboo and 317 more | | Label | player-facing: `feature` if players get something new, no label for a fix (`release-now` if urgent) | Named harnesses for what reads the container (`tools/parity.py paletted_container bulk_section save_roundtrip random_tick random_tick_sections light section_mesh lan chunk_generation`): 9 passed, 0 failed (5.2 min, on the previous base, before the rebase over #1789). CI's parity job runs the rest. 🤖 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 04:12:09 +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#3765
No description provided.