[PR #761] [MERGED] Paletted container access: one palette probe per write, dispatched unpack #2788

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

📋 Pull Request Information

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

Base: main ← Head: t3code/9960489f


📝 Commits (2)

  • 56a251e Paletted container access: one palette probe per write, dispatched unpack
  • 5c55e7d Paletted container access: skip the storage revalidation a section already did

📊 Changes

4 files changed (+502 additions, -46 deletions)

View changed files

📝 crates/horizon-core/src/paletted.rs (+101 -11)
📝 crates/horizon-core/src/storage.rs (+22 -0)
📝 crates/horizon-world/src/chunk.rs (+306 -35)
➕ docs/performance/paletted-container-access-results.md (+73 -0)

📄 Description

Optimises the unpack/get cell reader and BlockSection::get/set named in the paletted-container-access hotspot.

BlockSection::set

The old path resolved the old cell through get, then probed the section's palette map three times for one non-trivial write: keying the old state by id (local_ids[&old]), testing the incoming state (contains_key), and finding it (get). The slot at the written index already is the old state's id, so it is now read straight from storage and the incoming state is resolved with a single map probe. Palette reuse, free-slot and growth paths are unchanged, so the palette, counts, free list and NBT output are identical.

BlockSection::get/set and PackedStorage::read/write

get/set validate the section index and state id once, then read and write through new PackedStorage::read/write. These skip the storage-level bounds and value checks and the Result that PackedStorage::get/set keep for external callers; a section index is below 4,096 and a palette id always fits the width bits_for_palette chose. Both debug_assert their contract and are used for the hot read, the hot write and the palette-growth re-encode.

PalettedContainer::unpack / unpack_u16

The palette variant was matched inside the per-cell loop; Single/List/Global are now dispatched once before it, leaving each arm a branch-free indexed loop.

Results

Compared against copies of the pre-optimization code kept in the test modules, checked for identical palette/counts/free/NBT before timing (best round of alternating-order interleaved rounds, AMD Ryzen 5 5600X, fast profile, no LTO):

Operation main new speedup
BlockSection::set 13.15 ns/write 9.12 ns/write 1.44x
BlockSection::get 2.33 ns/read 1.45 ns/read 1.61x
unpack, list palette 1.910 us/call 1.834 us/call 1.04x
unpack, global palette 2.270 us/call 2.179 us/call 1.04x

Reproduce:

cargo test -p horizon-world --lib --profile fast benchmark_section_get_set -- --ignored --nocapture
cargo test -p horizon-core  --lib --profile fast benchmark_unpack            -- --ignored --nocapture

Tests

set_matches_baseline_under_random_edits drives the optimised and original set/get over the same 50k-edit streams for 1/2/16/17/24/64 distinct states, with periodic whole-section collapses that clear the storage and reclaim palette slots, and asserts equal results, bits, palette, counts, free list, local_ids and NBT throughout. No allocation, layout or wire-format changes. cargo test --workspace is green apart from the 7 horizon-mods sandbox tests that need rustup target add wasm32-unknown-unknown.

Details in docs/performance/paletted-container-access-results.md.


🔄 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/761 **Author:** [@rdbtCVS](https://github.com/rdbtCVS) **Created:** 10/17/2026 **Status:** ✅ Merged **Merged:** 10/18/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `t3code/9960489f` --- ### 📝 Commits (2) - [`56a251e`](https://github.com/horizon-rust/horizon/commit/56a251eb130f76f6f9638f6b9dd09f1fe19ada80) Paletted container access: one palette probe per write, dispatched unpack - [`5c55e7d`](https://github.com/horizon-rust/horizon/commit/5c55e7d855dc999d19ebd9f1e85e3c06c8a59606) Paletted container access: skip the storage revalidation a section already did ### 📊 Changes **4 files changed** (+502 additions, -46 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-core/src/paletted.rs` (+101 -11) 📝 `crates/horizon-core/src/storage.rs` (+22 -0) 📝 `crates/horizon-world/src/chunk.rs` (+306 -35) ➕ `docs/performance/paletted-container-access-results.md` (+73 -0) </details> ### 📄 Description Optimises the `unpack`/`get` cell reader and `BlockSection::get`/`set` named in the paletted-container-access hotspot. ## `BlockSection::set` The old path resolved the old cell through `get`, then probed the section's palette map three times for one non-trivial write: keying the old state by id (`local_ids[&old]`), testing the incoming state (`contains_key`), and finding it (`get`). The slot at the written index already *is* the old state's id, so it is now read straight from storage and the incoming state is resolved with a single map probe. Palette reuse, free-slot and growth paths are unchanged, so the palette, counts, free list and NBT output are identical. ## `BlockSection::get`/`set` and `PackedStorage::read`/`write` `get`/`set` validate the section index and state id once, then read and write through new `PackedStorage::read`/`write`. These skip the storage-level bounds and value checks and the `Result` that `PackedStorage::get`/`set` keep for external callers; a section index is below 4,096 and a palette id always fits the width `bits_for_palette` chose. Both `debug_assert` their contract and are used for the hot read, the hot write and the palette-growth re-encode. ## `PalettedContainer::unpack` / `unpack_u16` The palette variant was matched inside the per-cell loop; `Single`/`List`/`Global` are now dispatched once before it, leaving each arm a branch-free indexed loop. ## Results Compared against copies of the pre-optimization code kept in the test modules, checked for identical palette/counts/free/NBT before timing (best round of alternating-order interleaved rounds, AMD Ryzen 5 5600X, `fast` profile, no LTO): | Operation | main | new | speedup | | --- | ---: | ---: | ---: | | `BlockSection::set` | 13.15 ns/write | 9.12 ns/write | 1.44x | | `BlockSection::get` | 2.33 ns/read | 1.45 ns/read | 1.61x | | `unpack`, list palette | 1.910 us/call | 1.834 us/call | 1.04x | | `unpack`, global palette | 2.270 us/call | 2.179 us/call | 1.04x | Reproduce: ``` cargo test -p horizon-world --lib --profile fast benchmark_section_get_set -- --ignored --nocapture cargo test -p horizon-core --lib --profile fast benchmark_unpack -- --ignored --nocapture ``` ## Tests `set_matches_baseline_under_random_edits` drives the optimised and original `set`/`get` over the same 50k-edit streams for 1/2/16/17/24/64 distinct states, with periodic whole-section collapses that clear the storage and reclaim palette slots, and asserts equal results, bits, palette, counts, free list, `local_ids` and NBT throughout. No allocation, layout or wire-format changes. `cargo test --workspace` is green apart from the 7 `horizon-mods` sandbox tests that need `rustup target add wasm32-unknown-unknown`. Details in `docs/performance/paletted-container-access-results.md`. --- <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:03 +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#2788
No description provided.