refactor(highway): carve the 2D drawing layer into highway-draw.js (R3c) - #917
Conversation
18 functions, 1,245 lines. highway.js 3,972 -> 2,727 (-31%). The biggest R3c slice: notes,
sustains, chords, strum groups, unison bends and lyrics — everything the default renderer
paints each frame.
━━━ MUTABILITY, NOT LOCATION, DECIDES WHERE A THING BELONGS ━━━
Three per-instance caches came out with this slice, and they are why it needed care:
_frameMismatchWarned a warn-once Set of chord ids (feedBack#88)
_chordRenderInfo a WeakMap of chord -> chain info
_lyricMeasureCache Map<fontSize, Map<text, width>>
All three are MUTATED. Left at module scope they would be SHARED ACROSS PANELS — one
highway's lyric widths and chord chains stomping another's, silently, with nothing throwing.
createHighway() is a factory (the constitution publishes window.createHighway so a plugin can
build a second highway), so they are lifted onto hwState, which is exactly what hwState is for.
The shimmer LUT went the OTHER way — to MODULE scope in highway-geometry.js. It is a
deterministic xorshift table, byte-for-byte identical for every instance, so sharing it is not
merely safe but BETTER: built once for the page rather than once per panel.
Same slice, opposite directions, decided entirely by whether the thing mutates.
━━━ MY SCRIPT WAS WRONG TWICE. THE GATES CAUGHT BOTH. ━━━
1. HAND-LISTED THE MOVE SET. I listed 10 functions and missed six that drawChords needs
(_ensureChordRenderCache, bsearchChords, getChordTemplateInfo, _computeChordBox,
_updateFretLinePreview, _drawFretLineChordPreview). The no-undef gate named every one. The
set is now DERIVED from the dependency closure — 18, not 10.
2. JUDGED PURITY TOO EARLY, and this one is subtle. I classified _computeChordBox as pure
because its ORIGINAL body never mentions hwState. Then the call-site rewriter injected
`fretX(hwState, …)` INTO it — fretX takes hwState now (#916) — leaving a function that
references an hwState it was never given. Purity has to be judged from the body AS IT WILL
BE, so the classifier iterates to a fixed point: a function needs hwState if it mentions it,
OR calls anything that now takes it. That moved _computeChordBox to the stateful side.
VERIFIED. A/B against origin/main: IDENTICAL, zero page errors. The PLUGIN BUNDLE contract is
byte-identical (b.fretX arity 3, b.getNoteState arity 2, both stable references, both correct
under the old calling convention). PERF GATE PASSES AT 1.92ms against its 12ms budget — and
this is the slice that could really have cost something: the ENTIRE per-frame drawing path is
now cross-module. It costs nothing measurable.
TESTS. highway_teaching_marks follows strumGroupBuckets to the new module. The two source-shape
harnesses now read highway.js AND every static/js/highway-*.js, rather than being re-pinned at
whichever file currently holds a function — re-pinning breaks again next time, and a shape
assertion that silently stops finding its target is indistinguishable from one that passes.
node 1045, pytest 2416, ESLint 0, no-undef 0, Codex 0.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
📝 WalkthroughWalkthroughThe highway renderer is split across geometry and drawing modules. Rendering helpers now use ChangesHighway renderer modularization
Estimated code review effort: 4 (Complex) | ~45 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
static/js/highway-draw.js (1)
1076-1094: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueOptional:
bsearchandbsearchChordsare identical.Both are lower-bound searches keyed on
.t;bsearchChordsadds nothing overbsearch. If the separate export exists only for a stable bundle reference (lowerBoundT = bsearch), a re-export alias would remove the duplicate implementation while preserving the name.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@static/js/highway-draw.js` around lines 1076 - 1094, Remove the duplicate search implementation from bsearchChords and make it reuse bsearch while preserving bsearchChords as a named export. Keep the existing lower-bound behavior and stable exported symbol unchanged.tests/js/highway_note_state.test.js (1)
44-52: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚖️ Poor tradeoffOptional:
highwaySources()is duplicated verbatim intests/js/highway_chord_render_cache.test.js.Consider extracting this loader into a shared test helper so future source-shape changes only touch one place.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@tests/js/highway_note_state.test.js` around lines 44 - 52, Extract the duplicated highway source-loading logic from highwaySources() into a shared test helper, then update both highway_note_state.test.js and highway_chord_render_cache.test.js to use it. Preserve the existing file discovery, sorting, and concatenation behavior.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@static/js/highway-draw.js`:
- Around line 1076-1094: Remove the duplicate search implementation from
bsearchChords and make it reuse bsearch while preserving bsearchChords as a
named export. Keep the existing lower-bound behavior and stable exported symbol
unchanged.
In `@tests/js/highway_note_state.test.js`:
- Around line 44-52: Extract the duplicated highway source-loading logic from
highwaySources() into a shared test helper, then update both
highway_note_state.test.js and highway_chord_render_cache.test.js to use it.
Preserve the existing file discovery, sorting, and concatenation behavior.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: f101cb4f-03f8-4cb4-8476-8484b5e28fb0
📒 Files selected for processing (6)
static/highway.jsstatic/js/highway-draw.jsstatic/js/highway-geometry.jstests/js/highway_chord_render_cache.test.jstests/js/highway_note_state.test.jstests/js/highway_teaching_marks.test.js
18 functions, 1,245 lines.
highway.js3,972 → 2,727 (−31%). The biggest R3c slice: notes, sustains, chords, strum groups, unison bends and lyrics — everything the default renderer paints each frame.Mutability, not location, decides where a thing belongs
Three per-instance caches came out with this slice, and they're why it needed care:
_frameMismatchWarnedSetof chord ids (feedBack#88)_chordRenderInfoWeakMapof chord → chain info_lyricMeasureCacheMap<fontSize, Map<text, width>>All three are mutated. Left at module scope they would be shared across panels — one highway's lyric widths and chord chains stomping another's, silently, with nothing throwing.
createHighway()is a factory, so they're lifted ontohwState, which is exactly whathwStateis for.The shimmer LUT went the other way — to module scope in
highway-geometry.js. It's a deterministic xorshift table, byte-for-byte identical for every instance, so sharing it isn't merely safe but better: built once for the page rather than once per panel.Same slice, opposite directions, decided entirely by whether the thing mutates.
My script was wrong twice. The gates caught both.
1. I hand-listed the move set. I listed 10 functions and missed six that
drawChordsneeds (_ensureChordRenderCache,bsearchChords,getChordTemplateInfo,_computeChordBox,_updateFretLinePreview,_drawFretLineChordPreview). Theno-undefgate named every one. The set is now derived from the dependency closure — 18, not 10.2. I judged purity too early, and this one is subtle. I classified
_computeChordBoxas pure because its original body never mentionshwState. Then the call-site rewriter injectedfretX(hwState, …)into it —fretXtakeshwStatenow (#916) — leaving a function referencing anhwStateit was never given.Purity has to be judged from the body as it will be. The classifier now iterates to a fixed point: a function needs
hwStateif it mentions it, or calls anything that now takes it. That correctly moved_computeChordBoxto the stateful side.Verification
origin/main: identical, zero page errors.b.fretXarity 3,b.getNoteStatearity 2, both stable references, both correct under the old calling convention.Tests
highway_teaching_marksfollowsstrumGroupBucketsto the new module. The two source-shape harnesses now read highway.js and everystatic/js/highway-*.js, rather than being re-pinned at whichever file currently holds a function — re-pinning breaks again next time, and a shape assertion that silently stops finding its target is indistinguishable from one that passes.node 1045 · pytest 2416 · ESLint 0 · no-undef 0 · Codex 0.
🤖 Generated with Claude Code
Summary by CodeRabbit