Files
Lyrebird/docs/benchmarks/gp-baseline.md
T

129 lines
6.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# GP Performance Baseline
Reference numbers for the Generator Consolidation + RT Performance group
(GP-0 … GP-8, see `docs/00-working-order.md`). Each perf phase diffs its
before/after against this table.
**Harness:** `Sources/LyrebirdBenchmark/main.swift`, GP section.
**Command:** `swift run -c release LyrebirdBenchmark` (release build mandatory).
**Graph:** 8 voices per target, each `target → Pan2 → Out`, driven via
`OfflineDriver`; engine advances segment serial per block (no fan-out cache
artifact). Swept at `cbs=512` (1 control tick/callback) and `cbs=64`
(8 sub-blocks/callback).
> Absolute ns vary by machine/thermal state. What matters is the **delta** on
> the same machine before vs after a change. Re-run the baseline on the working
> machine if comparing across hardware.
## Baseline (captured on `gp-0-benchmark-baseline`)
| Target | Phase | cbs=512 ns/block | cbs=512 ns/sample | cbs=64 ns/block |
|---|---|---|---|---|
| BinaryOp gen×gen / gen+gen | GP-5 | 141716.7 | 276.79 | 141195.9 |
| BinaryOp gen×scalar / +scalar | GP-5 | 79927.6 | 156.11 | 79395.5 |
| Unary math Exp(Sqrt(Abs(Sin))) | GP-4 | 93944.7 | 183.49 | 94131.2 |
| LinLin.Audio | GP-6 | 27768.1 | 54.23 | 27561.8 |
| SVF.Audio gen-driven freq | GP-2 | 80005.9 | 156.26 | 78595.2 |
| Chorus.Audio | GP-7 | 93432.5 | 182.49 | 93420.9 |
`blockSize=512 voices=8 warmup=500 iterations=10000`
Note: control block size has negligible effect on these targets — all are
`.Audio`-rate generators whose inner loop runs at audio rate regardless of the
control sub-block split. The cbs=64 column is retained as a regression guard
(a change must not introduce sub-block sensitivity).
## How each phase reports
Each GP perf phase appends an "After" row (or a side-by-side block) under its
section here with the same command, same machine, and states the delta as a
percentage. Keep the change only if the delta is a real win with no
`swift test --no-parallel` regression.
## Results
### GP-2 — SVF helper extraction + block-boundary cutoff interpolation
| Target | Before ns/sample | After ns/sample | Delta |
|---|---|---|---|
| SVF.Audio gen-driven freq (cbs=512) | 156.26 | 147.10 | −5.9% |
Graph-level number; the SVF inner-loop saving is larger but diluted by the
constant OscSin-input / OscSin-LFO / Pan2 / Out overhead in the bench voice.
Faithful: per-sample `freq > 0 → silence` guard preserved exactly; slow cutoff
motion is numerically ≈ identical to per-sample `sin()`. Full suite 4882/4882.
### GP-4 — unary math vForce/vDSP fast path
| Target | Before ns/sample | After ns/sample | Delta |
|---|---|---|---|
| Unary math Exp(Sqrt(Abs(Sin))) (cbs=512) | 183.49 | 60.78 | −66.9% |
Split-path vectorisation of Sin/Cos/Exp/Log/Sqrt/Floor/Ceil (`vvsin`/`vvcos`/
`vvexp`/`vvlog`/`vvsqrt`/`vvfloor`/`vvceil`) and Abs (`vDSP_vabsD`) via the shared
`unaryVector*` helpers (`Generators/Math/UnaryMathVectorHelpers.swift`). The
bench chains 4 of these ops, so the ~3× speedup reflects the stacked saving.
vForce transcendentals are within tolerance of `Foundation.*` (suite asserts
`< 1e-10`); floor/ceil/abs are exact. Full suite 4883/4883.
### GP-5 — BinaryOpGenerator kind dispatch + vDSP
| Target | Before ns/sample | After ns/sample | Delta |
|---|---|---|---|
| BinaryOp gen×gen / gen+gen (cbs=512) | 276.79 | 120.97 | −56.3% |
| BinaryOp gen×scalar / +scalar (cbs=512) | 156.11 | 44.16 | −71.7% |
`op` closure replaced by a `BinaryOpKind` enum classified at init (operator
overloads pass `.add/.subtract/.multiply/.divide`; the closure init still exists
for arbitrary ops → `kind=nil` → closure fallback). Aligned generator rows
dispatch to vDSP (`vDSP_vaddD/vmulD/vsubD/vdivD`, `vsaddD/vsmulD/vsdivD`,
`vsmsaD/svdivD`). Subtract via `+(-s)` / `vsmsaD`, divide via true-division
routines (never reciprocal-multiply) → **bit-identical** to the scalar loop, so
no tolerance concern; only arg-order risk, covered by the full suite + new
order-sensitive tests. Highest graph-wide win (BinaryOp is ubiquitous). Full
suite 4883→4887/4887 (4 new tests).
### GP-6 — LinLin.Audio vDSP_vsmsaD
| Target | Before ns/sample | After ns/sample | Delta |
|---|---|---|---|
| LinLin.Audio (cbs=512) | 54.23 | 39.66 | −26.9% |
The range params are always block-scalar (`floatValue`), so the affine map
`f(x) = a·x + b` (`a = outRange/inRange`, `b = outLo − inLo·a`) applies
unconditionally — full block in one `vDSP_vsmsaD`. Short upstream blocks keep
the original exact per-sample formula (byte-identical fallback). The affine
reorder is within the suite's `< 1e-8` tolerance. LinLin is a cheap op so the
graph-level win is moderate (Osc/Pan2/Out dominate the bench voice). Full suite
4887/4887.
### GP-7 — power-of-two delay-line wrap (Chorus / Flanger)
| Target | Before ns/sample | After ns/sample | Delta |
|---|---|---|---|
| Chorus.Audio (cbs=512) | 182.49 | 176.45 | −3.3% |
Delay buffers rounded up to a power of two (shared `lyrebirdNextPowerOfTwo`);
the 5 (Chorus) / 3 (Flanger) per-sample `% delayLineCount` index wraps become
`& delayMask`. The semantic max delay (`_maxDelayFrac`) is kept separate from
the over-allocated buffer length, so the audible delay range and the delayed
content are unchanged (larger ring, identical relative reads → bit-identical
output; Chorus/Flanger tests pass). Modest win — the two per-sample LFO `sin()`
calls dominate, not the modulo. Aligns Chorus/Flanger with the pow2-delay
convention already used by CombFilter / AllpassFilter / DelayLine. Full suite
4887/4887.
### GP-3 / GP-8 — Butterworth cascade engine extraction (dedup + capability)
No isolated benchmark — these are structural. GP-3 extracted the shared LP/HP
cascade machinery into `_ButterworthCascadeBase`; GP-8 extracted a reusable
non-Generator `ButterworthSectionEngine` so BandPass (HP→LP series) and
BandReject (LP+HP parallel sum, now `vDSP_vaddD`) compose it — gaining the same
background-thread `vDSP_biquad_SetupD` path LP/HP already had, and removing their
duplicated scalar-cascade code. The vDSP biquad acceleration was already
characterised on LP/HP; for BP/BR it only materially helps at high order
(default order 2 = 1 biquad/section, where scalar ≈ vDSP). Net effect is large
dedup (GP-3 −278 LOC; GP-8 removes BP/BR cascade duplication) + vDSP capability
for high-order band filters. Behavior-identical (full Butterworth suite + 4887
total pass).