[PR #859] [MERGED] Paletted containers: build palettes without hashing #2864

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

📋 Pull Request Information

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

Base: main ← Head: perf/palette-ids-table


📝 Commits (1)

  • fdee657 Paletted containers: build palettes without hashing

📊 Changes

1 file changed (+198 additions, -15 deletions)

View changed files

📝 crates/horizon-core/src/paletted.rs (+198 -15)

📄 Description

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

What

palette_ids builds a section's palette (the distinct values in first-occurrence order) and each entry's index into it. It runs for every section that is saved or sent (PalettedContainer::pack) and every section that is generated (from_values).

Before:

  • for each of the 4,096 values, a linear scan of the palette while it had up to 16 entries;
  • after that, a std HashMap (SipHash) lookup per value.

Now:

  • A run of one value, which covers most of a section (stone, air, water), is matched once.
  • Up to 16 entries, the scan is unchanged.
  • Past 16 entries, a thread-local table indexed by value replaces the hash map. Every block state and biome id of 26.3 is below 65,536. Only the palette's entries are set, and they are cleared before returning, so each call costs O(entries + palette).
  • Values of 65,536 and above keep the hash map.

The output is unchanged: the same palette in the same order, and the same ids. Vanilla's PalettedContainer.pack builds a fresh HashMapPalette in storage order, and this matches it.

Why

perf-ticks-8's profile while exploring (macOS sample, Mac mini M4 Pro):

  • palette_ids was about a third of the region writer's encoding time: 1,597 of 4,827 samples in Record::tag, and 602 of 1,784 at quit.
  • On the server thread it showed up again when protos were saved.

Measured

cargo test --release -p horizon-core --lib benchmark_palette_ids -- --ignored --nocapture. The test keeps the old implementation as baseline_palette_ids. The 25 rounds alternate the two implementations, 500 calls each, and the table gives the median µs per call (Mac mini M4 Pro, shared machine):

section before after speedup
terrain (stone, ores, caves, dirt, grass, air: 9 values) 6.42 µs 2.91 µs 2.2x
surface (24 values in short runs) 22.85 µs 4.00 µs 5.7x
17 values, no runs 21.59 µs 4.00 µs 5.4x
global palette (600 values) 57.05 µs 7.77 µs 7.3x
ids above 65,535 (hash map kept) 22.46 µs 22.67 µs 1.0x
one value / 64 biomes 2.28 / 0.09 µs 2.18 / 0.08 µs 1.0x

End to end, save_bench (from #860) re-encodes 3,433 real chunks on one thread in 1.25-1.68 s with this change and 1.37-1.55 s without it (best of 7, alternating). That difference is within the noise of the shared Mac mini, which was at load average 100-114. Encoding also writes light nibbles, heightmaps and the NBT tree, which this PR doesn't touch.

Checks

  • cargo fmt --all --check and cargo clippy --workspace --all-targets -- -D warnings: clean (on the integration of this PR with #860, on main 256f7c20).
  • New test palette_ids_match_first_occurrence_order: seven section shapes (runs, 17 values, a global palette, ids past the table, biomes, a single value), each run twice against the old implementation. It also checks that the table is left all zero.
  • cargo test -p horizon-core --lib: 85 passed.
  • Parity, on the integration with #860: 17 of 17 pass: paletted_container, bulk_section, chunk_generation, chunk_stage, save_roundtrip, world_reopen, disk, world, server_world_gen, datafix ×6, lan, dedicated_server. save_roundtrip has a vanilla server load the chunks Horizon rewrote.
  • CI parity covers the rest of --changed.

Needs Ludovic

Nothing.

🤖 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/859 **Author:** [@ludovic111](https://github.com/ludovic111) **Created:** 10/18/2026 **Status:** ✅ Merged **Merged:** 10/18/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `perf/palette-ids-table` --- ### 📝 Commits (1) - [`fdee657`](https://github.com/horizon-rust/horizon/commit/fdee6576c62731d3a00d70e4649c790e55f308f6) Paletted containers: build palettes without hashing ### 📊 Changes **1 file changed** (+198 additions, -15 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-core/src/paletted.rs` (+198 -15) </details> ### 📄 Description > 🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself. ## What `palette_ids` builds a section's palette (the distinct values in first-occurrence order) and each entry's index into it. It runs for every section that is saved or sent (`PalettedContainer::pack`) and every section that is generated (`from_values`). **Before:** - for each of the 4,096 values, a linear scan of the palette while it had up to 16 entries; - after that, a std `HashMap` (SipHash) lookup per value. **Now:** - A run of one value, which covers most of a section (stone, air, water), is matched once. - Up to 16 entries, the scan is unchanged. - Past 16 entries, a thread-local table indexed by value replaces the hash map. Every block state and biome id of 26.3 is below 65,536. Only the palette's entries are set, and they are cleared before returning, so each call costs O(entries + palette). - Values of 65,536 and above keep the hash map. The output is unchanged: the same palette in the same order, and the same ids. Vanilla's `PalettedContainer.pack` builds a fresh `HashMapPalette` in storage order, and this matches it. ## Why perf-ticks-8's profile while exploring (macOS `sample`, Mac mini M4 Pro): - `palette_ids` was about a third of the region writer's encoding time: 1,597 of 4,827 samples in `Record::tag`, and 602 of 1,784 at quit. - On the server thread it showed up again when protos were saved. ## Measured `cargo test --release -p horizon-core --lib benchmark_palette_ids -- --ignored --nocapture`. The test keeps the old implementation as `baseline_palette_ids`. The 25 rounds alternate the two implementations, 500 calls each, and the table gives the median µs per call (Mac mini M4 Pro, shared machine): | section | before | after | speedup | |---|---|---|---| | terrain (stone, ores, caves, dirt, grass, air: 9 values) | 6.42 µs | 2.91 µs | 2.2x | | surface (24 values in short runs) | 22.85 µs | 4.00 µs | 5.7x | | 17 values, no runs | 21.59 µs | 4.00 µs | 5.4x | | global palette (600 values) | 57.05 µs | 7.77 µs | 7.3x | | ids above 65,535 (hash map kept) | 22.46 µs | 22.67 µs | 1.0x | | one value / 64 biomes | 2.28 / 0.09 µs | 2.18 / 0.08 µs | 1.0x | End to end, `save_bench` (from #860) re-encodes 3,433 real chunks on one thread in 1.25-1.68 s with this change and 1.37-1.55 s without it (best of 7, alternating). That difference is within the noise of the shared Mac mini, which was at load average 100-114. Encoding also writes light nibbles, heightmaps and the NBT tree, which this PR doesn't touch. ## Checks - `cargo fmt --all --check` and `cargo clippy --workspace --all-targets -- -D warnings`: clean (on the integration of this PR with #860, on main 256f7c20). - New test `palette_ids_match_first_occurrence_order`: seven section shapes (runs, 17 values, a global palette, ids past the table, biomes, a single value), each run twice against the old implementation. It also checks that the table is left all zero. - `cargo test -p horizon-core --lib`: 85 passed. - Parity, on the integration with #860: 17 of 17 pass: `paletted_container`, `bulk_section`, `chunk_generation`, `chunk_stage`, `save_roundtrip`, `world_reopen`, `disk`, `world`, `server_world_gen`, `datafix` ×6, `lan`, `dedicated_server`. `save_roundtrip` has a vanilla server load the chunks Horizon rewrote. - CI parity covers the rest of `--changed`. ## Needs Ludovic Nothing. 🤖 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-02 15:59:41 +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#2864
No description provided.