[PR #748] [MERGED] LAN: compress a large packet once for every connection #2776

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

📋 Pull Request Information

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

Base: main ← Head: perf/shared-compressed-frames


📝 Commits (1)

  • 0605ee6 LAN: compress a large packet once for every connection

📊 Changes

3 files changed (+133 additions, -6 deletions)

View changed files

📝 crates/horizon-client/src/lan/conn.rs (+112 -1)
📝 crates/horizon-protocol/src/connection.rs (+6 -1)
📝 crates/horizon-protocol/src/frame.rs (+15 -4)

📄 Description

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

What: each player's writer thread (horizon-lan-out) compressed every packet it sent, so a chunk column sent to 50 players was deflated 50 times. In server-host-4's profile (#683) and in mine, compression was most of the writer threads' time and about half of the server's CPU. Now bodies of 8 KiB or more (chunk columns, mostly) go through one cache shared by every connection, SharedFrames:

  • the first writer to send a body compresses it; the others reuse the compressed bytes;
  • the key is the body's length, two independently keyed 64-bit hashes of it (RandomState), and the compression threshold;
  • it keeps the newest 32 MiB of compressed frames and drops the oldest first.

The bytes on the wire are the same. frame::encode is split into frame::contents (the compressed contents, same MAX_FRAME check) and frame::prefixed (the length prefix), and ConnWriter::send_contents sends a frame from its contents through the same encryption and counters as send. Smaller packets, and connections without compression, take the old path. If two writers miss on the same body at the same time, both compress it and send identical bytes. A hash collision would need both 64-bit hashes and the length to match, odds about 2^-128 per pair of bodies.

New tests:

  • shared_frames_are_the_frames_compressed_here: the shared frame equals frame::contents for repeated bodies, a body differing in one byte, and two thresholds;
  • shared_frames_keep_the_newest_within_their_limit: the cache stays within its byte limit and its counts stay consistent.

Measured (dedicated server, 50 walking bots, seed 12345, view distance 10, Mac mini M4 Pro). Base is main dc1a8a4f; this PR is the same commit plus this change. Both ran at the same time, sides swapped each round. CPU is the server's whole process (all its threads) over the measured window:

base r1 this PR r1 base r2 this PR r2 base r3 this PR r3
server CPU 66.8 % 32.4 % 70.0 % 32.9 % 65.9 % 31.2 %
chunks sent a second 496 495 502 497 503 504
MSPT 6.84 ms 6.86 ms 11.10 ms 11.46 ms 8.06 ms 7.22 ms

An earlier set of three pairs (base main 7bbbf6e9, this change on 502222d2) gave the same result: 67.1 / 32.3 %, 68.5 / 32.4 %, 71.9 / 34.4 %.

The writer threads in macOS sample (20 s in the measured window, one run per build). The numbers are samples summed over every writer thread; the server thread's own samples are shown for scale:

base this PR
writer threads, active 158 755 31 507
of which deflate 122 677 177
of which socket send 31 067 26 323
server thread 7 078 6 485
process CPU in that run 67.9 % 30.6 %
resident memory 1246 MB 1176 MB

In this benchmark, deflate in the writers dropped by 99.9 %. The bots spawn together and walk close to each other, so nearly every chunk goes to many players, which is the best case. A server whose players are spread out shares fewer chunks and gains less. Each player still costs at most one compression per chunk, as before.

The server thread's own work and the tick (MSPT) don't change: compression ran on the writer threads. What changes is the CPU left for everything else on the machine, and how many players a host can feed before the writers fall behind.

Not in this PR: flate2's zlib-rs backend (server-host-4's other suggestion) would speed up the compressions that are left: packets under 8 KiB, and chunks that only one player receives. It changes the compressed bytes (not what they decompress to), so it is a separate decision.

How it was measured

  • Mac mini (M4 Pro, 14 threads, 64 GB), shared with other agents' builds and parity runs: ~/fleet/load showed the CPU 84 to 96 % busy with a load average of 75 to 114 during these runs.
  • tools/server_bench.py --servers horizon --behaviour walk --bots 50 --warmup 30 --seconds 45 --gamerule locator_bar=false: seed 12345, view and simulation distance 10, bots on the same machine, fast profile. Server CPU is server_bench's cpu_percent (the server process tree, 100 % = one core).
  • Paired runs: the base and the new build ran at the same time (two servers, 50 bots each), so both saw the same load; they swapped worktrees between rounds.
  • Writer threads: macOS sample of the engine for 20 s inside the measured window of a separate run per build. "Active" is the samples in write_loop that aren't waiting on the channel.
  • MSPT (/tick query average) moves with the machine's load, and this change doesn't touch the server thread.

Checks

  • cargo fmt --all --check: ok
  • cargo clippy --workspace --all-targets -- -D warnings: ok
  • cargo test -p horizon-client --lib: 829 passed, 0 failed, 11 ignored
  • cargo test -p horizon-protocol: 55 passed
  • Local parity (tools/parity.py --changed origin/main, one run on #737, #738, #739 and #748 stacked together, plus the chunk-occupancy commit on perf/chunk-occupied-columns): 87 passed, 0 failed (every harness that depends on the 9 changed files).

Nothing here needs Ludovic.

🤖 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/748 **Author:** [@ludovic111](https://github.com/ludovic111) **Created:** 10/17/2026 **Status:** ✅ Merged **Merged:** 10/17/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `perf/shared-compressed-frames` --- ### 📝 Commits (1) - [`0605ee6`](https://github.com/horizon-rust/horizon/commit/0605ee689f05152bd9a53980bc84675eb8bc8f09) LAN: compress a large packet once for every connection ### 📊 Changes **3 files changed** (+133 additions, -6 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/lan/conn.rs` (+112 -1) 📝 `crates/horizon-protocol/src/connection.rs` (+6 -1) 📝 `crates/horizon-protocol/src/frame.rs` (+15 -4) </details> ### 📄 Description > 🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself. **What:** each player's writer thread (`horizon-lan-out`) compressed every packet it sent, so a chunk column sent to 50 players was deflated 50 times. In server-host-4's profile (#683) and in mine, compression was most of the writer threads' time and about half of the server's CPU. Now bodies of 8 KiB or more (chunk columns, mostly) go through one cache shared by every connection, `SharedFrames`: - the first writer to send a body compresses it; the others reuse the compressed bytes; - the key is the body's length, two independently keyed 64-bit hashes of it (`RandomState`), and the compression threshold; - it keeps the newest 32 MiB of compressed frames and drops the oldest first. **The bytes on the wire are the same.** `frame::encode` is split into `frame::contents` (the compressed contents, same `MAX_FRAME` check) and `frame::prefixed` (the length prefix), and `ConnWriter::send_contents` sends a frame from its contents through the same encryption and counters as `send`. Smaller packets, and connections without compression, take the old path. If two writers miss on the same body at the same time, both compress it and send identical bytes. A hash collision would need both 64-bit hashes and the length to match, odds about 2^-128 per pair of bodies. New tests: - `shared_frames_are_the_frames_compressed_here`: the shared frame equals `frame::contents` for repeated bodies, a body differing in one byte, and two thresholds; - `shared_frames_keep_the_newest_within_their_limit`: the cache stays within its byte limit and its counts stay consistent. **Measured** (dedicated server, 50 walking bots, seed 12345, view distance 10, Mac mini M4 Pro). Base is main dc1a8a4f; this PR is the same commit plus this change. Both ran at the same time, sides swapped each round. CPU is the server's whole process (all its threads) over the measured window: | | base r1 | this PR r1 | base r2 | this PR r2 | base r3 | this PR r3 | |---|---:|---:|---:|---:|---:|---:| | server CPU | 66.8 % | 32.4 % | 70.0 % | 32.9 % | 65.9 % | 31.2 % | | chunks sent a second | 496 | 495 | 502 | 497 | 503 | 504 | | MSPT | 6.84 ms | 6.86 ms | 11.10 ms | 11.46 ms | 8.06 ms | 7.22 ms | An earlier set of three pairs (base main 7bbbf6e9, this change on 502222d2) gave the same result: 67.1 / 32.3 %, 68.5 / 32.4 %, 71.9 / 34.4 %. The writer threads in macOS `sample` (20 s in the measured window, one run per build). The numbers are samples summed over every writer thread; the server thread's own samples are shown for scale: | | base | this PR | |---|---:|---:| | writer threads, active | 158 755 | 31 507 | | of which deflate | 122 677 | 177 | | of which socket send | 31 067 | 26 323 | | server thread | 7 078 | 6 485 | | process CPU in that run | 67.9 % | 30.6 % | | resident memory | 1246 MB | 1176 MB | In this benchmark, deflate in the writers dropped by 99.9 %. The bots spawn together and walk close to each other, so nearly every chunk goes to many players, which is the best case. A server whose players are spread out shares fewer chunks and gains less. Each player still costs at most one compression per chunk, as before. The server thread's own work and the tick (MSPT) don't change: compression ran on the writer threads. What changes is the CPU left for everything else on the machine, and how many players a host can feed before the writers fall behind. Not in this PR: flate2's zlib-rs backend (server-host-4's other suggestion) would speed up the compressions that are left: packets under 8 KiB, and chunks that only one player receives. It changes the compressed bytes (not what they decompress to), so it is a separate decision. **How it was measured** - Mac mini (M4 Pro, 14 threads, 64 GB), shared with other agents' builds and parity runs: `~/fleet/load` showed the CPU 84 to 96 % busy with a load average of 75 to 114 during these runs. - `tools/server_bench.py --servers horizon --behaviour walk --bots 50 --warmup 30 --seconds 45 --gamerule locator_bar=false`: seed 12345, view and simulation distance 10, bots on the same machine, `fast` profile. Server CPU is server_bench's `cpu_percent` (the server process tree, 100 % = one core). - **Paired runs:** the base and the new build ran at the same time (two servers, 50 bots each), so both saw the same load; they swapped worktrees between rounds. - **Writer threads:** macOS `sample` of the engine for 20 s inside the measured window of a separate run per build. "Active" is the samples in `write_loop` that aren't waiting on the channel. - MSPT (`/tick query` average) moves with the machine's load, and this change doesn't touch the server thread. **Checks** - `cargo fmt --all --check`: ok - `cargo clippy --workspace --all-targets -- -D warnings`: ok - `cargo test -p horizon-client --lib`: 829 passed, 0 failed, 11 ignored - `cargo test -p horizon-protocol`: 55 passed - Local parity (`tools/parity.py --changed origin/main`, one run on #737, #738, #739 and #748 stacked together, plus the chunk-occupancy commit on `perf/chunk-occupied-columns`): 87 passed, 0 failed (every harness that depends on the 9 changed files). Nothing here needs Ludovic. 🤖 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:58:58 +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#2776
No description provided.