Skip to content

perf: one reactive flush per focus change, States without construction per remove, Row/Column/Grid leftovers (1.8 streams F, P) - #90

Merged
chiefcll merged 8 commits into
mainfrom
perf/streams-p-f
Nov 20, 2026
Merged

chiefcll merged 8 commits into
mainfrom
perf/streams-p-f

Conversation

@chiefcll

Copy link
Copy Markdown
Contributor

Ports what is left of 1.8's Stream P (primitives) and Stream F (focus) onto main. This follows #89 (Streams L, T and N). The 8 commits are cherry-picked with -x, so each names its 1.8 original.

What's in it

Stream P

  • Row and Column cache the merged directional transition (4a044c3, d911c85, 058070a). A key press reuses the merged object for its direction instead of allocating one. The cache is per element, so editing one Row's transition in place never affects another. It is rebuilt when the element's base transition object changes.
  • Grid creates its key and focus handlers once (1f25922). Before, they were rebuilt on every vertical move. Each handler still runs the app's own handler of the same name first, read at call time.
  • Grid writes y from its own effect (728fcb5). A vertical move no longer re-runs every other prop of the spread.

Stream F

  • One reactive flush per focus change, decision 5.1 (560717f):
    • The whole focus change (onFocus, onBlur, onFocusChanged, focusPath, activeElement) runs in one Solid batch.
    • The focus-path diff is O(depth): generation stamps instead of indexOf, and a reused buffer with one slice per change.
    • A focus change allocates no closures; _signalWrapper is gone.
  • setActiveElementCore has no local (3ebfa37), so terser inlines it without wrapping it in a function.
  • States: no construction on every remove; fixes B2 (14e84c7).
    • States[Symbol.species] is Array.
    • remove and merge shift items in place.
    • remove('focus') and toggle('focus') now match '$focus', as has('focus') already did.

Behaviour changes for apps

  • Focus callbacks (5.1):
    • onFocus, onBlur and onFocusChanged run in the same order as before.
    • An effect or JSX binding driven by a signal those callbacks write now runs once, after the last callback of the change, instead of right after each write.
    • Effects on focusPath run in the same flush as those on activeElement.
    • A callback that writes a signal and then reads a node prop an effect sets from it sees the old value until the change ends.
    • A focus change made from inside a focus callback no longer shows the intermediate activeElement or focusPath to effects.
    • If a callback throws, Solid's batch drops the effects queued by earlier callbacks of that change. The error still propagates.
  • states array methods (2.5): states.slice(), filter(), map(), splice() and the other Array methods return a plain Array, not a States. States methods on node.states itself are unchanged.
  • B2: states.remove('focus') and toggle('focus') act on '$focus'. Before, they did nothing when only '$focus' was in the list.
  • Row, Column and Grid: none expected. Editing a Row's transition object in place is not picked up; assign a new object, as with other cached objects.

Measured gains (from 1.8's docs/perf/log.md)

  • States and the focus path, single 6x runs on desktop V8 against the 1.6.4 lockstep port. Memory per press:

    • rows-lr-noshift: 3.46 → 2.27 KiB (−34%)
    • navdrawer-toggle: 22.08 → 14.15 KiB (−36%)
    • portal-focus-text reactivity: 1.80 → 1.08 KiB
    • updateFocusPath's 148 bytes per press are gone.
    • Counts are unchanged.

    Those figures also include 1.8's key-dispatch rewrite, which main already has from perf: remove per-press allocations (no functional change) #71. The gain over current main is the States, focus-path and batch part, so it will be smaller.

  • Row, Column and Grid: not measurable in a single run, since the merged transition was one small object per press. No bench scenario covers Grid. The Grid contract test shows an unrelated prop getter read once (at mount) instead of 4 times over three vertical moves.

Not ported, on purpose

Conflicts resolved

  • Grid.tsx: main's strictBounds={false} is kept next to the handlers that are now created once and the y effect.
  • focusManager.ts: main's comment and clearing of _dispatchEvent are kept. 560717f's publishActiveElement is added, and a comment that still named the removed ownerContext is reworded.

Testing

  • npm run tsc, npm run lint and npm run build pass.
  • npm test: 751 passed, 1 skipped, with 4 workers, with 1 worker, and with shuffled file orders (seeds 3, 4, 5). One skip fewer than main, because 14e84c7 turns on the B2 test.
  • npm run test:webgl: 38 of 38 pass.
  • Not checked on a device or in the demo app.

🤖 Generated with Claude Code

chiefcll and others added 8 commits November 20, 2026 10:11
handleNavigation merged transitionLeft/Right/Up/Down into `transition`
with a new object (and a `{}` when unset) on every press. The merged
objects are now cached by base transition, then by directional
transition (WeakMaps), so a press allocates nothing and the node keeps
the same `transition` object while the direction and the base do. A new
base from the app still gives new merged objects.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 4a044c3)
…sition

The merged-transition cache keyed every Row without a `transition` object
by one shared sentinel, so those Rows shared one merged object per
direction and an app mutating `row.transition` in place would have changed
it for all of them. Without a base object the cache is now keyed by the
element.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit d911c85)
Rows given the same base `transition` object shared the merged objects,
so an in-place edit of one Row's `transition` reached the others. The
cache is now per element, rebuilt when the element's base `transition`
object changes. An in-place edit of the base after a press in that
direction is not picked up (it was re-spread on every press): assign a
new object.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 058070a)
Grid built onUp/onDown/onLeft/onRight/onFocus inline in its spread, which
re-runs on every vertical move (for `y`), so each move made five new
functions and wrote them to the node. They are now created once; each
reads the app's handler of the same name when called, so a reactive one
still runs first and still stops the Grid's move by returning true.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 1f25922)
`y={scrollY()}` sat in Grid's spread, so every vertical move (a
focusedIndex change) re-ran every prop of the spread. The spread now
takes the initial y once and an effect of its own writes the changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 728fcb5)
- static get [Symbol.species]() returns Array: Array methods (slice,
  splice, filter, ...) build a plain Array instead of running the States
  constructor with a length as its callback (MIGRATION 2.5).
- remove() shifts in place instead of splice; so does merge().
- B2: remove('focus') removes '$focus', as has('focus') matches it, so
  toggle('focus') while '$focus' is on turns it off.
- onChange is a declared field assigned in the constructor (no class
  field emit), and the object form no longer uses Object.entries
  (Chrome 47 floor).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 14e84c7)
…hange

- updateFocusPath stamps each node of the new path with a generation;
  a node of the previous path without the stamp lost focus (no indexOf
  diff). The path is built in a reused buffer and published as a copy,
  since focusPath readers keep the array. A focus callback that calls
  setActiveElementCore re-entrantly gets its own array, and the outer
  change falls back to the scan, as before (pinned by a new test).
- 5.1: setActiveElementCore runs the focus phase (callbacks,
  setFocusPath, Config.setActiveElement) in one batch: callback order is
  unchanged; effects triggered by their signal writes run once, after the
  last callback (new contract test).
- No closure per focus change: the batch, and Config.setActiveElement as
  useFocusManager installs it, read module variables.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 560717f)
…without an IIFE

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 3ebfa37)
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