T-063: Performance pass 1: waste outside the physics
Goal
The first routine optimisation pass (docs/DESIGN.md section 12, "It never finishes").
Take the wins that are already on the list in docs/perf/IDEAS.md and that do not depend
on the physics, which is being replaced by T-009 right now. Every change is measured with
the check from T-050 (scripts/perf.sh --check), before and after.
Scope
Judge every idea in docs/perf/IDEAS.md by measurement, pick by measured gain, and do the
ones that fit this ticket. Leads known today, as examples and not as a list to tick off:
- Rayon's idle spinning: at 10,000 cells, 12 threads execute 36% more instructions than one thread. Small worlds on many threads waste cores that other work could use.
World::newstarts an all-cores thread pool even when the caller wants 4, andWorld::loadbuilds terrain and currents only to throw them away.- Long runs on a busy machine: the same build shows a median of 39 against a best of 89
steps per second on one thread. Would a higher scheduling class for
runner runhelp, and what does it cost the agents sharing the machine? Measure both sides. - The check itself judges whole steps only. A small stage can double unseen. Let it judge phases, with planted slowdowns in stages other than physics to prove it.
- Anything the profiler (
scripts/profile.sh) shows that nobody has listed.
Out of scope here, because other agents are in those files: the physics (T-009), the metabolism (T-017), the lineage log (T-019), and shrinking the per-cell development columns (wait until T-017 has merged, then it is fair game for the next pass).
Done when
- ☐ Each change has a
scripts/perf.sh --checkresult before and after, indocs/perf/LOG.md, and the suite's history shows the gain. - ☐ No result changes: the same world, bit for bit, on any thread count, native and web.
The existing determinism tests pass, and
scripts/check-unsafe.sh --quickpasses. - ☐
docs/perf/IDEAS.mdis brought up to date: what was done, what was measured and dropped, what is new. - ☐ The next pass is easier: say what you would look at next and why.
Notes
- Keep to 4 threads for anything long, except short measurements that need every core.
- The machine is busy. Trust only paired comparisons.
Result
Branch t-063-performance-pass-1. Numbers and the full tables are in the T-063 entry of
docs/perf/LOG.md; ideas are updated in docs/perf/IDEAS.md.
Against the main it started from (scripts/perf.sh --check, load about 10): every
case faster or the same. 100k cells: 1 thread -6.9% time, 4 threads -3.5%, 12 threads
-15.5%. 10k: 1 thread -6.1%, 4 threads -16.2%, 12 threads same speed with -25.7% CPU
cycles. 1M: 4 threads -8.2%, 12 threads -15.6%. Cycles -6 to -26% everywhere.
What changed, one commit each:
- The all-cores pool starts on first use, and
World::loadno longer makes terrain it throws away. Load of a 1024 x 128 x 1024 world 423 to 59 ms; no idle threads left. - Our own fork-join pool (
crates/sim/src/exec/pool.rs) replaces rayon. Idle threads spin 5 us, then sleep. CPU wasted against one thread at 10k cells: 12 threads +81% to about +40%, 4 threads +23% to +14%. rayon is no longer a dependency ofsimorrunner. perf checkjudges each stage on its own (25%, stages over 0.005 ms per step). Proved with plants in index, fields and bookkeeping; no false alarm on same code.runner run/resume --qos CLASS. Default unchanged;initiatedmeasured 1.1 to 2 times faster for the run, about 20% slower for competing work.#[inline(always)]on the index's grid coordinates: -4% on every case (in the contact queries; no physics file changed).#[inline]on the per-organism column views: -2 to -3.6%, instructions -4.4%.
Measured and dropped: parallel index build from 50,000 cells (faster alone, not inside a step: cycles +5 to +9.5%, time same). Spin lengths 0 to 100 us and pieces per thread 2 to 32 (chosen 5 us and 8).
Results unchanged: checkpoints byte-identical between main and the branch at 1, 4 and
12 threads (30k cells with churn, 400 steps); determinism tests pass; native and
WebAssembly identical for 600 steps (wasm_probe.sh 20000 60 600); full
scripts/check-unsafe.sh and --plant pass.
Where the tools got in the way (T-050's tools, first real use):
- A tunable that is not a parameter (the pool's spin, pieces per thread) cannot be A/B'd
with
--check: an environment variable reaches both workers. Sweeps had to be alternatedrunner perf suiteruns read back from scratch CSV files. A per-side environment or--setfor the new build would make this one command. - Microbenchmarks outside the check were nearly useless: the same build's index build took 0.40 or 0.55 ms depending on the core it landed on. Only the paired check, and cycles, gave usable answers.
- The suite prints instructions per cell but not cycles per cell, which is the number that shows wasted CPU; it had to be read from the CSV.
- The check judged whole steps only (fixed here). The development stage, at 0.005 to 0.03 ms per step, is mostly "can't tell" when judged alone; harmless, a little cluttered.
- Miri ran the pool's tests at full size for many minutes; they are now sized down under
cfg(miri).
What I would look at next: the index build is now the largest serial piece (12% of a 4-thread step at 100k); a parallel build with one loop and one key pass. Then measure the pool's wake-up cost once T-009 adds loops. Details in the LOG entry.