Skip to content

Performance: the Rytm's UI task and audio hooks do less per tick (hook-throttle, um-tick, params-delta, DIRTY only while wanted) - #25

Open
twwwy wants to merge 19 commits into
trigcondition:devfrom
twwwy:feat/perf-up
Open

twwwy wants to merge 19 commits into
trigcondition:devfrom
twwwy:feat/perf-up

Conversation

@twwwy

@twwwy twwwy commented Nov 23, 2026 •

Copy link
Copy Markdown

Closes trigcondition/sidecar-firmware#70 (the go is there; its conditions are met).

Topic 1 of the roadmap, trigcondition/sidecar-firmware#66. The host half is trigcondition/sidecar-firmware#67. The first two commits work alone. The 56 and DIRTY changes need the host half to take effect, and with an older host they behave as now.

Why

With 12 tracks of 16ths playing and the Sidecar connected, the Rytm's keys and knobs lag, worst in scene edit. There the Rytm is CPU-bound, and its UI task starves first. In rytm-emu (OS 1.74, the link up, held to 122 M instructions a second, about where the lag shows), I put each hook back to stock bytes one at a time to find what costs most. These commits remove those costs without changing what the host or the user sees.

What

  1. 0025 / 0026: throttle the audio-block hooks
    • 0025's send-FX reporter skips its 13-track scan when the last scan ended with nothing to send, no track is pending, no kit change is under way, and none of the 18 words it reads changed. The skip test compares the words against a 32-bit shadow and stops at the first difference. Otherwise it runs as before: same order, same sends, same block. This scan was the largest single cost.
    • 0026's pin_frame reads the hardware timer only after its early returns.
  2. 0021: us_tick's work once an audio block, its enum-list walk once in 10 ticks
    • us_tick runs on every value text, and a screen update draws its value texts in a burst. The commit, the 1C write-backs and us_tick3 now run at the first value text of an audio block, not at each one.
    • The walk for missing enum lists checks the cache's free list once per slot. After a walk with nothing to ask, the next one waits US_SCAN_GAP (10) ticks. The same asks go out.
  3. 0021: parameter-only reports as 56 PARAMS DELTA, to a host that says it reads them
    • With a track's LFO on a user machine's knob, ext_render sent a 41-byte 03 about 90 times a second. When the host's 16-byte 1D sets caps bit 0, a parameter-only report carries only the changed values, F0 7D 52 54 56 01 <vb> <m0> <m1> <m2> {<hi> <lo>} x n <tag> F7, at 03's rate (every 16 blocks a voice).
    • Kept whole: the first report after the host turns it on or asks a resync (bit 1), a report with 14 or more changed values, a whole 03 again every 2048 blocks (about 1.4 s, staggered by voice) while a voice moves, and the MIDI machine's 43. Trigs, note-offs and 45 are unchanged. A shorter 1D, or the port going down, turns it off. slots=0 builds are unchanged.
  4. 0026: 2D 0B DIRTY's pattern walk only while the host wants it (2E flags bit 3)
    • The walk (85 KB every 21 ticks) was 0026's largest UI-task cost. When the host's 2E sets bit 3 (none wanted), there's no walk at all.
    • When the bit clears, a fresh pass starts, and one 0B goes at the end of that first full pass (about 1.05 s), not at once. Any other 0B waits and goes as that one. So the host reads the pattern only after every chunk has been summed, and every edit after that raises a 0B.
    • The hello's caps2 bit 6 (64) says this build has the rule. The host sends bit 3 only to a Rytm with it, so a host paired with an older Rytm keeps DIRTY on. caps2 is 127 now; the caps byte was already full.
    • A hello or the port going down counts as wanted. The 2D 0A edit reports are unaffected, and an older host never sets the bit.
  5. Tests and tools
    • tools/emu/pd_emu.py (new suite pd): no 1D or an older 1D gives 03s only, caps give fewer 56s with the same end values, and a resync gives one whole 03.
    • sq_emu dirty: not wanted means no walk, and a placed trig's 0B comes only with its edit report. Wanted again: no 0B within 0.5 s, then exactly one after the first pass.
    • rui_emu rehello: the hello's caps2 is 127.
    • tools/emu/knoblag.py (new, not a suite): the knob latency table below, rerunnable. README section in tools/emu/README.md.

Each stub.s is regenerated with its mod's build.sh. The design notes are in each mod's design.md: "hook-throttle", "params-delta", "2D 0B DIRTY only when wanted".

Tests

OS 1.74, tools/build_rui.sh, elekloader 2bcafa1, hash_expect.py --check PASS. tools/emu/suites.py <tag> --changed upstream/dev, each image with its own host (firmware dev for dev, #67 for this branch), on analog-emu's sidecar-upstream (951dcbc).

  • Every suite has the same passes and failures as dev, check by check, except:
suite dev pass / fail this branch pass / fail
pd (new) — 18 / 0
sq 408 / 6 417 / 5 (pin passes here)
  • The failures shared with dev (in ar, ms, pj, rled, rui, sa44, sfu, sq, sv and us) come from my emulator setup, not this series: they fail the same way on dev's image.
  • Elekloader RAM spare: 22,760 -> 21,808 B (−952).
  • Knob latency (tools/emu/knoblag.py: 12 tracks of 16ths, knobs E / F turning, 45 detents every 113 ms each, the emulator held to 122 M instructions a second), mean / max:
image scene edit open no scene edit
stock 1.74 + Overbridge 46.0 / 89 ms 40.9 / 85 ms
dev (0.5.2) 292.7 / 626 ms 48.4 / 97 ms
this branch 57.0 / 113 ms 46.1 / 97 ms

All 90 detents were dispatched on every image. This exact image hasn't been on hardware yet: the emulator is not the unit, so the latency numbers are a guide.

🤖 Generated with Claude Code

@trigcondition

Copy link
Copy Markdown
Owner

Review for both halves is on trigcondition/sidecar-firmware#67 (the cu fm check change, the sq / smpswitch test changes and the report rate apply here).

twwwy and others added 18 commits November 29, 2026 21:39
- 0025: the send-FX reporter skips its 13-track scan when the last scan
  ended with nothing to send, no track is pending, no kit change is under
  way and none of the 18 words it reads changed. Otherwise it runs as
  before: same order, same sends, same block.
- 0026: pin_frame reads the hardware timer only after its early returns.

Why: with 12 tracks of 16ths and the Sidecar on, keys and knobs lag,
worst in scene edit; the Rytm is CPU-bound there and the UI task starves
first. Reverting each audio-interrupt hook to stock bytes one at a time
in rytm-emu (1.74, link up, held to 122 M instructions a second) showed
the reporter's scan as the largest single cost (0.53 M instructions a
second for values that change only at a lock, a turn or a kit change).

Measured there on an image with more of the Sidecar's mods: scene-edit
knob latency 521 ms -> 37 ms (stock with Overbridge: 32 ms); the reports
the host gets are the same. Design notes: 0025 and 0026 design.md
"hook-throttle".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…10 ticks (um-tick)

us_tick runs on every value text, and a screen update draws its value
texts in a burst:

- The commit, the 1C write-backs and us_tick3 run at the first value
  text of an audio block (0.67 ms) and are skipped by the rest of that
  block's.
- The walk for missing enum lists checks the cache's free list once per
  slot, before its knobs, and after a walk with nothing to ask the next
  one waits US_SCAN_GAP (10) ticks, as an ask already did. The same asks
  go out.

Why: with every slot READY the walk found nothing to ask and ran again on
the next value text, and with 8 user-slot tracks the knobs lagged (rytm-emu
1.74 at 122 M instructions a second). The ask throttles still count every
value text.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…o (params-delta)

With a track's LFO on a user machine's knob, ext_render sent a 41-byte
03 for the voice every 16 blocks (~90 a second), all 16 values for the
one that moved; on a Rytm that USB MIDI traffic made the UI lag.

- A host's 1D SLOTS may now be 16 bytes, <slot> <result> <caps> (slot 0:
  no answer; an older 0021 reads it so). Caps bit 0: the host reads 56;
  bit 1: resync. A shorter 1D, or the port going down, turns it off.
- Then a parameter-only report is
    F0 7D 52 54 56 01 <vb> <m0> <m1> <m2> {<hi> <lo>} x n <tag> F7
  (13 + 2n B; one value 15 B), at most every 32 blocks (~21 ms) a
  voice; a value that jumped (a whole step between two blocks) after
  16, as before. The voice's first parameter-only report after the host
  turned it on or asked a resync, 14+ changed values and the MIDI
  machine's 43 stay whole. Trigs (04 / 44), note-offs and 45 unchanged;
  a value that moved and stopped is still reported once at the end.
- slots=0 unchanged.
- tools/emu/pd_emu.py (suite pd): no 1D / an older 1D -> 03s only; caps
  -> 56s, fewer, the same end values; resync -> one whole 03.

The host side is sidecar-firmware's "host_link: read 56 PARAMS DELTA,
say so in the 1D". Design notes: 0021 design.md "params-delta".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…gs bit 3)

The Rytm walks its pattern (dk_poll, unchanged: 4 KB a tick, a pass in
1.05 s) only while the host needs the 0Bs. The host's 2E flags bit 3 =
none wanted (the Sidecar's touch view not fetching the pattern, no
pattern write running); then there is no walk at all. Clear (an older
host never sets it): as before. Wanted again: a fresh pass and one 0B at
once, so the host fetches the pattern as it is. A hello or the port
down: wanted. The 2D 0A edit reports are unaffected.

Why: on a CPU-starved Rytm (12 tracks playing, scene edit) the walk,
85 KB every 21 ticks, was 0026's largest UI-task cost.

tools/emu/sq_emu.py dirty: not wanted - a placed trig gives no 0B and
the walk's done mask stays 0; wanted again - one 0B and the walk.

The host side is sidecar-firmware's "seq: DIRTY wanted in the 2E (flags
bit 3 = none)". Design notes: 0026 design.md "2D 0B DIRTY only when
wanted".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…a placed trig's 0B only with its edit report

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ispatch them

- sq "mode: 32 encoder A +6 while T1 is held": any split of 32s, all
  encoder A, +6 in all, between T1's down and up.
- cu "fm: knob E": 24 turns of +5 store 4 entries, as stock's own list
  knob does with the same turn (stock 1.74, BD's FLTR TYPE: 0 -> 4).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The check read the machine byte 2 blocks after the switch its first run
found, in a second run with a BD trig 2M before it. With the audio-block
hooks throttled, the machine list's last preview can land a block later
there, so the read saw the previous machine (43). Both builds take every
DOWN at the same times and reach 44. At 0.5M both read 44 with the
sample silent.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Wanted while the Sidecar's pattern mirror fetches (the touch view's PAT
or VOICE page, a fetch asked for) or a pattern write runs (FILL's fills
and undo, from the job's start to its end); not wanted otherwise. The
rule and its tests are sidecar-firmware docs/SEQUENCER.md's "When the
host wants the DIRTYs".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review of trigcondition/sidecar-firmware#67:

- A parameter-only report goes at most every REPORT_GAP = 16 blocks, as
  an 03 did, with or without the host's caps. The first version held a
  56 for 32 blocks, which was not behaviour-preserving for slowly moving
  values. REPORT_GAP_D, DELTA_JUMP and the jump test are gone.
- Refresh: a voice whose last report was a 56 (x->dlast, the old jump
  byte: no struct change) sends a whole 03 in its slot of every 2048
  blocks (~1.37 s, staggered by voice), changed or not. So a 56 the host
  lost is not stale until the next trig.

tools/emu/pd_emu.py: delta expects 03's rate (62 vs 62 reports, 956 B vs
2,542 B) and allows the refresh's whole 03s. New `refresh`: after the
last 56, exactly one whole 03 with the 56s' values. modes allows a
refresh inside a 56 run. On the previous build, delta and refresh fail.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review of trigcondition/sidecar-firmware#67. When the host turns DIRTYs
back on, the walk starts afresh and takes about 1.05 s (21 UI ticks) to
sum the whole pattern. An edit without its own 0A (a p-lock, a parameter
change) in a chunk not yet summed raised no 0B at all. A host that read
the pattern at the first 0B could then write over it, and still verify.

Now (dk_arm) the Rytm sends that 0B when the first pass has summed every
chunk, not at once. While armed, every other 0B (an edit report's, a
pattern switch's) waits and goes as that one. So the first 0B after the
host turns them on is the pass's; the host reads after it (patwrite's
kArming), and every later edit is in a summed chunk and raises a 0B.
Turned off again while armed: nothing held.

tools/emu/sq_emu.py dirty: wanted again with a trig placed during the
pass: no 0B in the first 0.5 s (its 0A goes), then the pass's 0B, at
most one more. On the previous build the 0B came at once (fails).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…able, rerunnable

Review of trigcondition/sidecar-firmware#67 ("please add the latency
harness").
- What it plays: 12 tracks of 16ths, and two knobs turning (200
  detents, 25 ms apart), with and without scene edit.
- What it runs on: stock (this tree's stock/ OS, booted as us_base.sh
  boots a build), a baseline tag and a candidate tag. The builds talk to
  the Sidecar's real core (RYTM_TEENSY's sim/rui_link) on the
  emulator's Overbridge link. The emulator is held to IPS M instructions
  per emulated second.
- What it prints: mean / max knob latency (a detent to the OS's encoder
  dispatch), CPU busy, key latency, and the gate the PRs used (exit 1).
  The gate fails on a lost detent, a mean > BASE + 10 ms or > 2x stock,
  or a max > 2x BASE + 20 ms.

Everything it writes is firmware-derived and git-ignored: build/knoblag/
here, sections/us_stock.* in rytm-emu. Run it in a port tree; its
addresses are 1.73's, as every suite's. Not a suite (it needs the core
and takes minutes): tools/emu/README.md "Knob latency under load".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ult)

On analog-emu's sidecar-upstream branch the panel takes at most about 30
detents a second. The first table's 100 detents every 50 ms a knob lost
a quarter of them on every image there, stock included, at 122 and at
200 M alike: it measured the panel, not the Rytm. Now --detents N
--gap MS, default 50 every 100 ms (inside the 5.5 s scene hold). The
same command on the older emulator binary took 186 of 200 detents; on
sidecar-upstream it took 151.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With the scan skipped on ~98 % of blocks, the skip test itself was most of
the reporter's cost: 18 words read in two loops, each zero-extended,
compared and stored, about 250 instructions every audio block (1,500 a
second). moved() compares them unrolled against a zero-extended shadow,
first difference out: about 60 instructions a block. The same words decide
the same skip; when the scan runs, raw is refreshed from the words (the
same end state as the word-by-word update before).

Measured with tools/emu/knoblag.py on analog-emu sidecar-upstream (122 M,
link up): scene edit 68.7 / 116 -> 64.0 / 107 ms, without 56.8 / 101 ->
53.0 / 96 ms, CPU busy -0.21 %. +272 B of area code, +36 B of state.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…k's 50 ms)

Two knobs every 100 ms, half a gap apart, is a detent every 50 ms: the UI
tick's period, so each image's boot phase moved its mean by several ms.
113 ms keeps the detents off the tick; the ordering of the images is the
same at 97 and 113 ms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@twwwy

twwwy commented Nov 29, 2026

Copy link
Copy Markdown
Author

Rebased on 0.5.2's dev (8e64f22). The only conflicts were additions on both sides (0026 design.md's new sections, suites.py's new suites, and the regenerated stub.s files); both sides are kept. One new commit: knoblag.py goes in suites.py's NONE_PATHS, since it's a measurement script.

The host half (trigcondition/sidecar-firmware#67) is rebased on firmware dev (c4176bf) too. 0x56 is still free on 0.5.2's side, and the 1D caps byte is separate from the hello's caps2 table.

Build: OS 1.74, tools/build_rui.sh, elekloader 2bcafa1, hash_expect.py --check PASS.

  • dev: d052, sha256 1cf7dc5d126c00c92ec51a62cdcac2ad23a0f147c04610d3f8550a08f4c332c0
  • this branch: p052, sha256 a126ba31b3ad24fa537b35890964020d0d73ccfb38440398e5db7a2d03651ca3
  • Elekloader RAM spare: 22,760 -> 21,808 B (−952).

Suites: tools/emu/suites.py <tag> --changed upstream/dev -j 3, each image with its own host (dev's firmware for d052, #67 for p052), on analog-emu's sidecar-upstream (951dcbc). Every suite has the same passes and failures as dev, check by check, except:

suite dev pass / fail this branch pass / fail
pd (new) — 18 / 0
sq 408 / 6 417 / 5 (pin passes here)

The failures shared with dev (in ar, ms, pj, rled, rui, sa44, sfu, sq, sv and us) come from my emulator setup, not this series: they fail the same way on d052.

Knob latency (tools/emu/knoblag.py p052 d052 122: 12 tracks of 16ths, knobs E / F turning, 45 detents every 113 ms each, the emulator held to 122 M instructions a second), mean / max:

image scene edit open no scene edit
stock 1.74 + Overbridge 46.0 / 89 ms 40.9 / 85 ms
dev (0.5.2) 292.7 / 626 ms 48.4 / 97 ms
this branch 57.0 / 113 ms 46.1 / 97 ms

All 90 detents were dispatched on every image. The key latency for three page presses: dev 251.1 ms with scene edit open, 87.2 ms without; this branch 8.0 / 2.5 ms; stock 31.2 / 20.3 ms. The emulator isn't the unit, so the numbers are a guide.

@trigcondition

Copy link
Copy Markdown
Owner

Thanks for the rebase and the numbers. The knob and page-key latency with scene edit open is a real improvement.

One change for the pair, asked on trigcondition/sidecar-firmware#67 (trigcondition/sidecar-firmware#67 (comment)), and it touches this half too. With a Rytm still on 0.5.2's mods, the host sends 2E bit 3, gets no early 0B, and every FILL / CLEAR / UNDO waits in kArming for 2.5 s. Could 0026 set the next free bit in its hello's caps byte (the one where bit 2 = 4B reports)? The host would then use kArming and bit 3 only when that bit is there, and keep DIRTY on as now for an older Rytm. A test on each side for both hellos would cover it.

Once both halves have it, we run our emulator suites on the pair and merge it for 0.6.

…d the end-of-pass 0B

The host (trigcondition/sidecar-firmware#67) sends 2E bit 3, and a pattern
write waits for the end-of-pass 0B, only to a Rytm whose hello has this bit,
so a Rytm on 0.5.2's mods keeps DIRTY on and FILL does not wait 2.5 s. caps2
is 127 now (bits 0..6); the caps byte was full already. rui_emu rehello
expects caps2 127.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@twwwy

twwwy commented Dec 1, 2026

Copy link
Copy Markdown
Author

Done: 0026's hello now sends caps2 bit 6 (64), so caps2 is 127. The caps byte was already full (127, bit 6 being param modulation), so I used caps2's next free bit; it's caps2's last one. The host side and its tests are on trigcondition/sidecar-firmware#67. 0026's design.md has a paragraph on the bit, and rui_emu rehello checks caps2 127.

Build p052b (OS 1.74, tools/build_rui.sh, elekloader 2bcafa1): PASS, sha256 cadb562609765b4eaea6938124eeb9ee0cf978a394d04672f91cf1a2923cd5ee, elekloader RAM spare unchanged at 21,808 B. hash_expect.py --check PASS.

suites.py p052b --changed upstream/dev with #67 as the host: the same passes and failures as the previous run on this PR, suite by suite (pd 18 / 0, sq 417 / 5, the rest as dev). fxin 56 / 0, frz 16 / 0, td 12 / 0.

The PR body is updated to match (the end-of-pass 0B, 56 at 03's rate, the caps2 bit, the 0.5.2 numbers).

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.

2 participants