Skip to content

One work pool for the app's background threads - #26

Merged
byRandom merged 1 commit into
mainfrom
refactor/work-pool
Nov 15, 2026
Merged

byRandom merged 1 commit into
mainfrom
refactor/work-pool

Conversation

@byRandom

Copy link
Copy Markdown
Owner

Proposal #5 from the architecture review: one work pool for the app's background threads.

Before

  • TileCache and StructureCache each had their own copy of the same machinery:
    • a Mutex+Condvar queue of pending keys and a set of keys in progress;
    • workers that take the most urgent key and keep per-seed state;
    • the result sent before the key leaves the in-progress set;
    • a call to egui's request_repaint.
  • Spawn and Strongholds each had their own copy of a one-shot background computation.
  • All of it took an egui::Context, so none of it could be tested.

After

New crates/chunkscope-app/src/work.rs:

  • WorkPool<K, V>:
    • new(name, workers, wake, compute(&mut state, key));
    • request(wanted, done), which returns the number of keys still to come;
    • receive().
  • Job<T>: spawn(name, wake, compute), is_loading(), get().
  • The wake is a plain function; the app passes request_repaint.
  • Dropping a pool now also stops its idle workers.

TileCache and StructureCache keep their caches and become thin users of the pool. Spawn and Strongholds wrap a Job. Net change in the existing files: 99 lines added, 261 removed, not counting the new module.

Tests

  • Seven new unit tests, with no egui involved:
    • the most urgent key is computed first;
    • a key in progress is not queued again;
    • a new request drops stale work, even one of the same length;
    • keys already done are skipped;
    • each worker keeps its state;
    • the wake fires after each result;
    • idle workers stop when the pool is dropped;
    • a job delivers its value once.
  • The app's 11 tests passed in 20 runs in a row.
  • Mutation testing: every rule was broken on purpose and a test failed for each.
  • The app shows the same map and markers as before (checked under a virtual display).

No behaviour change, so no release.


Generated by Claude Code

The biome tiles and the structure markers each had their own copy of the
same machinery: a Mutex+Condvar queue of pending keys, a set of keys in
progress, workers that take the most urgent key, keep per-seed state, send
the result before leaving the in-progress set, and call
egui's request_repaint. The spawn and the strongholds each had their own
copy of a one-shot background computation. None of it could be tested
without egui.

crates/chunkscope-app/src/work.rs now holds it once:
- WorkPool<K, V>: new(name, workers, wake, compute(&mut state, key)),
  request(wanted, done) -> keys still to come, receive().
- Job<T>: spawn(name, wake, compute), is_loading(), get().
The wake is a plain function (the app passes request_repaint). Dropping a
pool now also stops its idle workers.

TileCache and StructureCache keep their caches and become thin users of
the pool; Spawn and Strongholds wrap a Job. Seven new unit tests cover the
queueing rules (most urgent first, the key in progress is not queued
again, a new request drops stale work, done keys are skipped, per-worker
state, the wake, idle workers stopping on drop, a job delivering once);
each rule was broken on purpose and a test failed. The app shows the same
map and markers as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SenGxDDRwLejx8nir8GXKG
@byRandom
byRandom merged commit 8a586c8 into main Nov 15, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant