Skip to content

Auto white balance: one prop-reading akarin Expr instead of a compile per frame (#253) - #289

Merged
StuartCameronCode merged 3 commits into
mainfrom
stuart/136-investigate-github-issue-253
Nov 28, 2026
Merged

StuartCameronCode merged 3 commits into
mainfrom
stuart/136-investigate-github-issue-253

Conversation

@StuartCameronCode

Copy link
Copy Markdown
Owner

Fixes #253.

Colour Correction's automatic white balance (_auto_white) returned a new expression from a std.FrameEval on every frame, with the offsets written into the expression text. _expr sends that to akarin wherever akarin is installed, which is every bundle except macOS x64. So each frame was an LLVM compile (~17–20 ms) in a Python callback holding the GIL, and the pass ran serially no matter how many cores the machine had. That is why the reporter's 48-core EPYC ran slower than a desktop.

Change

  • Automatic white balance: with akarin, two PlaneStats calls stamp the chroma averages on each frame as properties (_AwbU* / _AwbV*, removed afterwards), and one akarin expression reads them (x._AwbUAverage). The FrameEval is kept only as the fallback where akarin is absent (macOS x64), where one std.Expr per frame costs ~0.35 ms.
    • The filter alone at 706x480 went from ~60 fps to ~8,000 fps.
    • Output is identical at 8–12 bits and on the fallback. At 16 bits, 0.16% of chroma planes are one 16-bit step apart, because akarin reads a frame property as a 32-bit float.
  • Automatic levels is unchanged, apart from a comment explaining why. Its per-frame std.Levels is cheap (~5,000 fps). An akarin version is not bit-exact: akarin compiles with fast-math, and its division misses .5 ties in ~2% of (min, max) pairs.
  • CLAUDE.md: a new rule never to build an akarin expression per frame, with the fast-math caveat.
  • Report: docs/reports/issue-253-auto-colour-per-frame-expr, with its probes, timings and scripts. Also an ENGINEERING_NOTES entry and a row in the reports index.

Testing

  • test_271 (Rust) checks both scripts: the akarin branch reads properties and builds nothing per frame, the fallback keeps the FrameEval, and auto levels stays std.Levels. The full cargo test passes.
  • The Flutter script test in integration_filter_parameters_test.dart passes. The push gate passed (1967 pass, 8 skipped).
  • New heavy test integration_auto_colour_test.dart: each filter changes the picture, and the preview matches the lossless encode exactly (max difference 0). Passes 2/2.
  • The format sweep's automatic white balance rows passed 69/69, and the colour-correction smoke rows 7/7.
  • Run on macOS arm64. Windows, Linux and macOS x64 are left to CI.
  • Not measured: the whole job's speed on an idle machine. The chain timing (10.8 → 10.9 fps) was taken while other heavy tests were running, where the other passes were slower than the old filter's serial ceiling. The report says so.

🤖 Generated with Claude Code

StuartCameronCode and others added 3 commits November 28, 2026 20:23
…perties (issue #253)

_auto_white returned a new _expr from a FrameEval on every frame, with the
offsets written into the expression. _expr goes to akarin wherever it is
installed (every bundle but macOS x64), so every frame was an LLVM compile in
a Python callback holding the GIL: ~17 ms a frame on an M1, serial whatever
the core count (the issue: 20% CPU, a 48-core EPYC slower than a desktop).

With akarin, PlaneStats now stamps each chroma plane's mean on the frame and
one expression, compiled once, reads it; the FrameEval stays only as the
fallback without akarin, where a std.Expr per frame is cheap. The filter
alone at 706x480: ~60 -> ~8,000 fps. Identical output at 8-12 bits and on the
fallback; at 16 bits 0.16% of chroma planes are one step apart (akarin reads
a property as a 32-bit float).

Automatic levels is unchanged: a std.Levels per frame is a lookup table and
cheap, and an akarin version is not bit-exact (fast-math division misses .5
ties in 2% of (min, max) pairs). A comment says so.

Tests: test_271 and a Flutter script test check the akarin branch reads
properties and evaluates nothing per frame; integration_auto_colour_test
(heavy) checks both filters act and preview = lossless encode. CLAUDE.md:
never build an akarin expression per frame, with the fast-math caveat.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ile)

docs/reports/issue-253-auto-colour-per-frame-expr: the cause, the fix, the
exactness probes (identical at 8-12 bits and on the fallback; 0.16% of 16-bit
chroma planes one step apart), why automatic levels stays a std.Levels
(akarin's fast-math division misses .5 ties in 2% of (min, max) pairs), the
filter's speed (~60 -> ~8,000 fps alone) and the whole chain's (unchanged on
a heavily loaded M1, explained), with every script. ENGINEERING_NOTES entry
and reports index row.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts:
#	docs/ENGINEERING_NOTES.md
#	docs/reports/README.md
@StuartCameronCode
StuartCameronCode merged commit ee1b99b into main Nov 28, 2026
8 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.

Auto white balance rebuilds its filter graph every frame (~3x slower whole pipeline)

1 participant