[PR #6719] [MERGED] horizon-threads: one shared worker pool for the process #7051

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

📋 Pull Request Information

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

Base: main ← Head: perf-jobs/pool


📝 Commits (1)

  • 8a6024f horizon-threads: one shared worker pool for the process (jobs)

📊 Changes

2 files changed (+701 additions, -0 deletions)

View changed files

➕ crates/horizon-threads/src/jobs.rs (+700 -0)
📝 crates/horizon-threads/src/lib.rs (+1 -0)

📄 Description

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

First step of the "one job system instead of four pools" workstream (perf-20x baseline #5): a single pool of worker threads for the whole process, in horizon-threads (std only). Nothing uses it yet; the next PRs move the pools onto it one at a time.

Why

A single-player world runs several pools of compute threads, each sized for the whole machine (N = logical CPUs):

pool threads
server thread: one Streaming per opened level N-1 each
the window's Streaming (light + mesh of received chunks) N-1
horizon-chunk-encode-* (server tick's chunk encoding) N-1
far terrain (opt-in): sampling, meshing, decoration, coordinator up to 8 + 16 + 12 + 1

That is 15 compute threads on a 4-thread laptop and ~94 on a 32-thread desktop with far terrain off (overworld only; +N-1 per dimension visited), ~129 with it on. On macOS and Windows the helpers aren't lowered (behind_server is Linux-only), so they compete with each other and the render and server threads. Measured on the Mac mini (M4 Pro, 14 threads, RD 32): 61-66 threads at peak on main.

Design (crates/horizon-threads/src/jobs.rs)

  • One pool, jobs::pool(), sized before first use by jobs::configure(n) (the game will pass Graphics::worker_threads(): N-1, half on battery, or the workers setting); HORIZON_JOB_THREADS overrides it for benches and small-CPU emulation. Threads keep the name horizon-worker-<i> (the crash report recognises workers by it) and go behind the server and render threads as before.
  • Sources keep their own queues (Source: next() -> Option<Rank>, run(worker)), so a streamer can still rank its jobs again when the player moves and drop the ones that left the view. A free worker asks every source for its next rank and runs the most urgent job: Class first (Urgent: saves, freed meshes, the tick's encode; Near: the chunks around the player; Far: far terrain), then the source's key (the streamers' existing column distance × 4 + stage bias).
  • Far work never takes every worker (at most threads-1 run at once), so near work that arrives doesn't wait behind a long far job.
  • No per-worker deques. Jobs are coarse (a section mesh, a proto chunk: 0.2-15 ms) and nearest-first matters more than locality. Every idle worker takes the next job the moment it is free, which gives the load balancing that work stealing is for, without LIFO deques breaking nearest-first.
  • A notify never waits for a worker. While every worker is busy, notify is two atomic operations (epoch, sleepers). It takes the sleep mutex, which no source code ever holds, only to wake a worker. So a frame or a tick queuing thousands of jobs never blocks behind a worker's choice (in a first version a worker choosing under the pool lock could hold up the render thread's submit).
  • Per-worker scratch (PerWorker<T>) belongs to the source, slot i used only by worker i. Dropping a Registration takes the source out and waits for its running jobs, which is what joining the threads did. Idle workers free their buffers after 5 s, and when all are idle, release_free_memory() runs (the streamers' existing logic). A panicking job is still reported by the panic hook, and the worker survives.

Tests (half of the 701 lines): rank order across sources, the far cap, no lost wake-ups (20 000 jobs queued while 4 workers look), idle release, dropping a registration waits for its running jobs, a panic keeps the worker.

Design notes and measurements: agent-state/perf-jobs.md (horizon-parity).

pr_ready

pr_ready (tools/pr_ready.py --quick on ludovics-mac-mini, against origin/main at 10d255143, 5 min 39 s)

Check Result
Scope ⚠️ 2 files, +701 −0; 701 lines of code: over 500, split it if it can ship in parts
Cargo.lock ✅ unchanged
Public tree ✅ (1 s)
Python tests ✅ 602 passed (3 min 34 s)
rustfmt ✅ (6 s)
clippy ✅ (1 min 28 s)
tests: horizon-threads ✅ 16 passed (1 s)
doc tests ✅ 0 passed
content root tests ➖ content root untouched
mod API up to date ➖ native hooks untouched
tokens.css up to date ➖ theme untouched
launcher rustfmt ➖ launcher untouched
launcher clippy ➖ launcher untouched
launcher tests ➖ launcher untouched
Parity ➖ 132 affected, to run in CI (--quick): ambient_sound, armor_stand_frost, arrow, arrow_wind_charge, audio_attributes, auto_jump, block_entity_anim, block_entity_render, block_entity_tickers, block_particles, block_removal, block_tint and 120 more
Label player-facing: feature if players get something new, no label for a fix (release-now if urgent)

🤖 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/6719 **Author:** [@ludovic111](https://github.com/ludovic111) **Created:** 11/2/2026 **Status:** ✅ Merged **Merged:** 11/2/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `perf-jobs/pool` --- ### 📝 Commits (1) - [`8a6024f`](https://github.com/horizon-rust/horizon/commit/8a6024f8e177c9064ee05e70e91e76ff89d7e0a5) horizon-threads: one shared worker pool for the process (jobs) ### 📊 Changes **2 files changed** (+701 additions, -0 deletions) <details> <summary>View changed files</summary> ➕ `crates/horizon-threads/src/jobs.rs` (+700 -0) 📝 `crates/horizon-threads/src/lib.rs` (+1 -0) </details> ### 📄 Description > 🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself. First step of the "one job system instead of four pools" workstream (perf-20x baseline #5): a single pool of worker threads for the whole process, in `horizon-threads` (std only). Nothing uses it yet; the next PRs move the pools onto it one at a time. ## Why A single-player world runs several pools of compute threads, each sized for the whole machine (N = logical CPUs): | pool | threads | |---|---| | server thread: one `Streaming` per opened level | N-1 each | | the window's `Streaming` (light + mesh of received chunks) | N-1 | | `horizon-chunk-encode-*` (server tick's chunk encoding) | N-1 | | far terrain (opt-in): sampling, meshing, decoration, coordinator | up to 8 + 16 + 12 + 1 | That is 15 compute threads on a 4-thread laptop and ~94 on a 32-thread desktop with far terrain off (overworld only; +N-1 per dimension visited), ~129 with it on. On macOS and Windows the helpers aren't lowered (`behind_server` is Linux-only), so they compete with each other and the render and server threads. Measured on the Mac mini (M4 Pro, 14 threads, RD 32): 61-66 threads at peak on main. ## Design (`crates/horizon-threads/src/jobs.rs`) - **One pool**, `jobs::pool()`, sized before first use by `jobs::configure(n)` (the game will pass `Graphics::worker_threads()`: N-1, half on battery, or the `workers` setting); `HORIZON_JOB_THREADS` overrides it for benches and small-CPU emulation. Threads keep the name `horizon-worker-<i>` (the crash report recognises workers by it) and go behind the server and render threads as before. - **Sources keep their own queues** (`Source`: `next() -> Option<Rank>`, `run(worker)`), so a streamer can still rank its jobs again when the player moves and drop the ones that left the view. A free worker asks every source for its next rank and runs the most urgent job: `Class` first (Urgent: saves, freed meshes, the tick's encode; Near: the chunks around the player; Far: far terrain), then the source's key (the streamers' existing column distance × 4 + stage bias). - **Far work never takes every worker** (at most threads-1 run at once), so near work that arrives doesn't wait behind a long far job. - **No per-worker deques.** Jobs are coarse (a section mesh, a proto chunk: 0.2-15 ms) and nearest-first matters more than locality. Every idle worker takes the next job the moment it is free, which gives the load balancing that work stealing is for, without LIFO deques breaking nearest-first. - **A notify never waits for a worker.** While every worker is busy, `notify` is two atomic operations (epoch, sleepers). It takes the sleep mutex, which no source code ever holds, only to wake a worker. So a frame or a tick queuing thousands of jobs never blocks behind a worker's choice (in a first version a worker choosing under the pool lock could hold up the render thread's `submit`). - **Per-worker scratch** (`PerWorker<T>`) belongs to the source, slot i used only by worker i. Dropping a `Registration` takes the source out and waits for its running jobs, which is what joining the threads did. Idle workers free their buffers after 5 s, and when all are idle, `release_free_memory()` runs (the streamers' existing logic). A panicking job is still reported by the panic hook, and the worker survives. Tests (half of the 701 lines): rank order across sources, the far cap, no lost wake-ups (20 000 jobs queued while 4 workers look), idle release, dropping a registration waits for its running jobs, a panic keeps the worker. Design notes and measurements: `agent-state/perf-jobs.md` (horizon-parity). ## pr_ready **pr_ready** (`tools/pr_ready.py --quick` on ludovics-mac-mini, against origin/main at 10d255143, 5 min 39 s) | Check | Result | | --- | --- | | Scope | ⚠️ 2 files, +701 −0; 701 lines of code: over 500, split it if it can ship in parts | | Cargo.lock | ✅ unchanged | | Public tree | ✅ (1 s) | | Python tests | ✅ 602 passed (3 min 34 s) | | rustfmt | ✅ (6 s) | | clippy | ✅ (1 min 28 s) | | tests: horizon-threads | ✅ 16 passed (1 s) | | doc tests | ✅ 0 passed | | content root tests | ➖ content root untouched | | mod API up to date | ➖ native hooks untouched | | tokens.css up to date | ➖ theme untouched | | launcher rustfmt | ➖ launcher untouched | | launcher clippy | ➖ launcher untouched | | launcher tests | ➖ launcher untouched | | Parity | ➖ 132 affected, to run in CI (--quick): ambient_sound, armor_stand_frost, arrow, arrow_wind_charge, audio_attributes, auto_jump, block_entity_anim, block_entity_render, block_entity_tickers, block_particles, block_removal, block_tint and 120 more | | 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) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-03 06:32:53 +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#7051
No description provided.