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 17 commits into
trigcondition:devfrom
twwwy:feat/perf-up
Open

twwwy wants to merge 17 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. 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 most every 32 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, 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 with one 0B at once, so the host fetches the pattern as it is.
    • 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
    • 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.
    • sq_emu mode / cu_emu fm: the encoder checks accept the turn splits that the builds and stock dispatch.
    • us_emu smpswitch reads 0.5 M after the switch instead of 0.3 M (see below).

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".

The host's rule for when it wants 2D 0B is written down on trigcondition/sidecar-firmware#67 and in 0026's design.md.

Tests

OS 1.74, the Makefile's release set. tools/emu/suites.py --full, base dev (95886ee) vs this branch:

  • The new build matches or beats dev in every suite. 13 suites already fail on dev with the same counts, except these:
suite dev pass / fail this branch pass / fail
cu 244 / 1 245 / 0
sq 403 / 7 411 / 5
us 497 / 2 499 / 0 (smpnoise fixed; smpswitch's window, below)
pd (new) — 15 / 0
  • smpswitch: The test switched BD's machine and read the machine byte 0.3 M (two blocks) after the switch that its first run found, with a BD trig added 2 M before. With the throttled hooks, the machine list's last preview can land one block later, so the read saw the previous machine. Both builds take every DOWN at the same times and reach the same machine. At 0.5 M, both read the new machine with the sample silent, so the check now reads there.
  • Elekloader RAM spare: 13,432 -> 12,704 B (-728), re-measured after the rebase onto dev 292ab6c (0.5.1.1 plus doc commits), unchanged.
  • Knob latency: 12 tracks of 16ths, knobs turning, the emulator held to 122 M instructions a second, with this branch's host half (mean / max):
image scene edit open no scene edit
stock 1.74 + Overbridge 32.4 / 63 ms 25.3 / 53 ms
dev 460.5 / 941 ms 26.8 / 48 ms
this branch 30.7 / 53 ms 21.1 / 43 ms

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

twwwy and others added 8 commits November 24, 2026 03:34
- 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>
@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 9 commits November 24, 2026 11:30
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>
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