Skip to content

perf(runtime): skip empty voice slots with a per-block bitmap - #981

Draft
m-dwyer wants to merge 7 commits into
mainfrom
perf/runtime-dispatch-empty-slots
Draft

m-dwyer wants to merge 7 commits into
mainfrom
perf/runtime-dispatch-empty-slots

Conversation

@m-dwyer

@m-dwyer m-dwyer commented Dec 5, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

With any machine type registered the runtime's _dv_run dispatch runs for every voice slot. An empty slot cost 58 instructions on DT2 (stock 8) and 61 on DN2 (stock 10): 1,740 of the 3,978 per block with one Pluck voice on DT2. The output guard scan costs 3 instructions per word.

Changes

  1. _dv_active: the first slot of a block builds a bitmap of the slots that need the full path (a type with a row; on DT2 also a type of 7 or more, which keeps the stage 3 breadcrumb). Other slots test their bit and return to the stock renderer. A clear DT2 slot still writes the stage 1 breadcrumb, so the watch's last-step word is unchanged. The word is a new PKDATA of the two stubs plus a dyndsp import row on each device; DN2's _dv_q2, _dv_q4, _dv_ql4 are removed (no longer used).
  2. _dv_gcheck (and the DN2 guard): the load of word i+1 shares the instruction of the AND of word i, so 2 instructions per word. This is the bound of the single-ALU pipeline; a third instruction per word cannot be removed without SIMD.

Both live in sdk/dsp/contract/dk_dispatch.inc (macros), used by dispatch-dt2.s and dispatch-dn2.s. The unpack hook does not use the bitmap: the frame consumer runs before the voices, so the bitmap would be a block old when a track's type changes (finding 188).

Why it is exact

A set bit runs the unchanged full path (it reads the type again); a clear bit means the type has no row and, on DT2, is below 7, which the full path sent to the stock renderer after the stage 1 breadcrumb. The bitmap is rebuilt each block while a row exists. The guard computes the same maximum magnitude. The type words are written only by the frame consumer's copy loops, all of which run before the voice loop is called on both devices (addresses in finding 188), so slot 0's build sees what every later slot sees; a type change between blocks is seen by the next block's build (rig tests).

Rig counts (instructions per 32-voice DT2 / 16-slot DN2 block, steady; one script, main c84d9cc vs this branch, digikit/pluck, Python MachineRig)

case DT2 main DT2 branch DN2 main DN2 branch
slot installed, no row 677 679 523 525
1 Pluck voice (track/slot 0) 3,978 3,199 (-779) 3,020 2,640 (-380)
4 Pluck voices 10,218 9,553 (-665) 9,092 8,625 (-467)
empty slot, row registered 58 29 61 31
slot 0 (builds the bitmap) 1,939 2,025 (+86) 2,105 2,175 (+70)

fadedlimes/analog conformance, worst block over its cases (steady / note-on), main vs branch: DT2 5,562 / 5,613 vs 5,648 / 5,699 (budget 12,140); DN2 5,728 / 5,797 vs 5,798 / 5,867 (steady budget 5,799, headroom 1). Delta +86 (DT2) and +70 (DN2): the slot 0 build.

Tests

  • dsp-active.rig.test.ts (new, both devices): the bitmap marks exactly the slots with a row (none, one, several, a row freed between blocks); a clear slot is the stock renderer plus the stage 1 breadcrumb; no breadcrumb without a row.
  • dsp-guard.rig.test.ts and dsp-guard-dn2.rig.test.ts: the cases of guard-cases.ts (none, 8.0 and -8.0 exactly, one over at word 0/31, negative, Inf, NaN, several; stereo 64-word block on DT2) through spot.s (DT2) and spot_contract.s (DN2 MachineRig). They pass on the old and the new loop.
  • dsp-active.rig.test.ts also changes a slot's type between blocks and checks the next block runs the new path, on both devices.
  • Rigs poke _dv_active as every slot (the full path decides itself): sharc-rig-kit (MachineRig.setActive, VoiceRig), dsp-quality, and the two Python rigs.

Review findings and fixes

  • The writer-order caveat is settled statically on DT2 1.17 and DN2 1.12 image bytes (finding 188): the type words are written only by the frame consumer's copy loops (DT2 FUN_1c2b24 at 0x1c2c79 and 0x1c3069, before the voice-loop call at 0x1c3083; DN2 FUN_1c2712 at 0x1c287a and 0x1c302d, before the call at 0x1c3044). No fix and no cost: the bitmap is exact.
  • The DN2 guard cases now run on a MachineRig (dsp-guard-dn2.rig.test.ts), and a rig test changes a slot's type between blocks on both devices.
  • Finding 48's "about 21 instructions" is reconciled with finding 188 (29 on DT2, 21 over the stock path's 8).

Gate

  • mise run test:changed: unit and static tasks passed (29 tasks), rig preflight 67 halves, 0 problems.
  • Device proof (DN2, the printed command): 125 ok, 1 failed. The failure was test-device--reload-fx--dn2: wb transfer install ... --restart was killed with SIGTERM (a load timeout at load average 50 to 90, not an assertion). The same target rerun alone passed (nx 79 ok, 0 failed).
  • Emulator parity (sharc_cells, one fixture state per tree: A = merge-base 769f409, B = branch; the same coupled-run binary and core, 20/24 s, one emulator at a time): PCM equal at both lengths, DT2 dt2-sample-trig and DN2 trig1-hold.
state DSP insns per audio s, A B B/A CF thread B/A
DT2 dt2-sample-trig 25.527 G 25.154 G 0.9854 0.9904
DN2 trig1-hold 26.720 G 26.719 G 0.9999 0.9976

The DSP and ColdFire state hashes differ at both lengths on both devices, as they must: the two trees give different runtime images (the new _dv_active word, DN2's three removed _dv_q* words). Attribution of the DSP dumps (24 s; static analysis of the two dumps, tools/emulator/dspdiff.py reader, no link map of the runtime builds was kept, so the layout delta was found from the dumps): each differing word of A was matched to the word at the same place shifted by the layout delta in B, equal or with a pointer or opcode immediate relocated by that delta (a multiple of 4 bytes).

device differing words equal after the shift pointer or immediate relocated left
DN2 trig1-hold 1,499 874 625 0
DT2 dt2-sample-trig 211 122 69 20

DN2's shift is -8 bytes (one word added, three removed, 8-byte alignment) for 1,278 words and 0 for the rest. The _dv_active word and the _dv_q* words are inside the matched clusters (no isolated word is left). The DT2 residue, 20 words, is run-phase state: the five counters at 0x2662d8, 0x266328, 0x282ca3ec, 0x4801b3d0 and 0x4809b858 differ by 2 or 3 ticks (B is ahead), the timestamp words at 0x2826f7a8 and 0x28269a10/1c differ by a few ticks, two ring pointers (0x261a48, 0x261ab8) point at neighbouring slots, 0x25f780 is a flag, and 9 SDRAM float words (0x80058770/74, 0x8045a490..4ac) are running state that also differs at 20 s by the same phase. Taken from the DSP at a different tick when the fixed emulated time ends, as B runs fewer instructions per block. This is an explanation from the 20 s and 24 s dumps, not a proof word by word, and the relocation test allows a field delta of 4 to 24 bytes (so a coincidence is possible). PCM equality is the exactness check. The ColdFire state digest is a single hash: its differing words were not attributed.

Hardware check

This ships to the device. Listen to stock tracks and a module voice on DT2 and DN2, trigger a note on the first beat of a block-aligned step, and check fault reporting (a guard fault of a module must still zero the block and report).

🤖 Generated with Claude Code

Merge-gate failures and fixes (DN2)

  1. test_sine.py::...unregistered_type_goes_back_to_the_stock_code_without_the_table_walk[dn2]: 147 instructions, bound 70. Cause: tests/support/machine_rig.py set _dv_active to all ones for every block (a full-path shortcut), so the slot of an unregistered type took the full path and the table walk. Fix: the rig sets the bitmap slot 0 leaves, from the slot types and the mask (_active_bitmap). The test and its bound are unchanged. DT2 did not fail: its clear slot returns after the breadcrumb either way, and the DT2 case sets the type below 7.
  2. fadedlimes/analog:test-dsp-conformance on DN2: 5,814 instructions, budget 5,799. Cause: slot 0 of the measured block pays the bitmap build (6 instructions a slot, 16 slots). Fix: with one bit per slot the mask is shifted down by the type and the low bit deposited (DV_ACTIVE_ONE_BIT, 5 instructions a slot): 16 fewer, 5,798. DT2 keeps the 2-bit form and has headroom in its budget.
  3. digikit/shot:test-host-integration (peer-voice-host, 5,000 ms): load; the rerun passed (22 s).

Rerun: the two failing tests, core/dsp-voice:test-dsp (dsp-active, dsp-guard, dsp-guard-dn2; 443 tests), fadedlimes/analog:test-dsp-conformance (both devices), mise run test:changed (29 tasks, preflight 0 problems). The two counts that move (1 voice, 4 voices on DN2) are the old counts minus 16.

Rig honesty (review round)

The Python MachineRig no longer sets _dv_active from a Python copy of the build. A block of a slot above 0 first runs the real slot 0 call (the assembly builds _dv_mask and _dv_active from the table and the frame's types, as in a real block); only the measured call is counted and read. _active_bitmap and its device-name branch are gone (device-literals baseline back to 48). dsp-active.rig.test.ts covers the one-bit macro on DN2 and the 2-bit form on DT2 with types 0, 6, 7, 30, 31 and 40, and rows of 8, 30 and 31 (a row of 31 or more names every type from 31 up). The TS MachineRig.setActive default (all slots) is unchanged.

Checks: test_sine.py 2 passed; core/dsp-voice:test-dsp ok; fadedlimes/analog:test-dsp-conformance ok on both devices; mise run test:changed 29 tasks ok, preflight 0 problems.

m-dwyer and others added 6 commits December 6, 2026 19:15
WIP: bitmap and guard scan in the dispatch code; rig tests; generated objects follow.

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

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

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…cks; prove the writer order

The type words are written by copy loops that run before the voice loop on both
devices (finding 188), so the bitmap needs no re-check. Reconcile finding 48.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The slot bitmap build shifted the mask by 31 - type and sign-extended the
bit. With one bit per slot the mask can instead be shifted down by the type
and the low bit deposited: 5 instructions a slot, not 6 (16 fewer on DN2).
This keeps the analog machine's slot 0 block within its budget.

The Python MachineRig set every slot active, so a block of an unregistered
type took the full path and the table walk. It now sets the bitmap slot 0
leaves, from the slot types and the mask.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@m-dwyer
m-dwyer force-pushed the perf/runtime-dispatch-empty-slots branch from d8c92ca to 501250e Compare December 6, 2026 09:40
…Python rig

The rig no longer re-implements the active-slot bitmap in Python: a call
of a slot above 0 first runs the assembly's slot 0 on the same frame, which
builds _dv_mask and _dv_active, and only the measured call is counted. This
drops the device-name branch (device-literals baseline back to 48). The
dsp-active rig test now covers types 0, 6, 7, 30, 31 and above 31 and rows
of 30 and 31 on both devices.

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

This branch has not been deployed

No deployments
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.

1 participant