perf(signals): mark heap insertions in place instead of invalidating the markHeap memo - #3356
Merged
Merged
Conversation
…the markHeap memo Mounting N rows in one flush was O(N²) when each row created a user effect whose source was written during row creation (the ref-effect pattern). An unmarked node entering an already-marked pure heap set heap._marked = false, so every later mid-tick memo pull re-walked the whole heap: N full walks, N²/2 nodes visited. The insertion now runs markNode(n) in place — the same flags the deferred walk would have produced (#2922 semantics unchanged) — and the mount is linear (8000 rows, one ref effect each, prod: 231 → 12 ms). Adds a mount-scaling bench and a tripwire + mid-flush freshness test. Fixes #3350 Co-authored-by: Claude via Cursor <noreply@cursor.com>
🦋 Changeset detectedLatest commit: 20f3ac0 The changes in this PR will be included in the next version bump. This PR includes changesets to release 11 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
The absolute bound (250 ms at 8000 rows) measured 403 ms on the coverage-instrumented CI job (12× slower than local). Compare 1000 vs 8000 rows within one process instead: ~8-10× fixed, ~50× on next; threshold 24. Co-authored-by: Claude via Cursor <noreply@cursor.com> Co-authored-by: Cursor <cursoragent@cursor.com>
Coverage Report for CI Build 34538775353Coverage remained the same at 71.842%Details
Uncovered ChangesNo uncovered changes found. Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
Merging this PR will not alter performance
Performance Changes
Comparing |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3350
Problem
Mounting N rows in one flush was O(N²) when each row created a
createEffectwhose source was written during row creation — therefeffect pattern (createEffect(() => el(), ...)+<div ref={setEl} />).Writes schedule subscribers by heap insertion alone; DIRTY/CHECK marks are propagated lazily by
markHeap, memoized onheap._markeduntilrunHeapresets it. IninsertIntoHeap, an unmarked node entering an already-marked heap setheap._marked = false, so the next mid-tick pull re-walked the whole heap. During a synchronous mount each row's user effect is inserted unmarked (creation-timeenqueueSub) right after the row's sync render effect has pulled a memo (marking the heap). Instrumented at N=500: 500 full walks, 124,750 nodes visited (N²/2).Fix
The unmarked insertion now runs
markNode(n)in place instead of invalidating the memo. It produces exactly the flags the deferred walk would have (DIRTY on the node, CHECK on its subscribers), so the #2922 semantics (write between two mid-tick pulls must be visible) are preserved — those tests still pass. When_markedis false nothing changes; marking stays lazy for the plain write path.Numbers
Reporter's repro against the built dist, prod, N=8000:
Now ~1.5–2.5 µs/row and linear (was 4× per 2× N).
New bench
tests/mount-rows.bench.ts(CodSpeed): 4000 rows 221 → 16.4 ms; the reference shape without the effect is unchanged (~8 ms).Tests
tests/heap-mark-incremental.test.ts:< 250 ms; fixed 33 ms,next761 ms — fails onnext, follows theoverlay.test.tstripwire convention)Verification
next(238)app: render + one signal10.895 → 10.924 KB andapp: CSR observe + attribution26.598 → 26.652 KB against caps of 10.92 / 26.65 — brotli layout drift on a one-call swap (createStore and isPending scenarios shrank on the same build); caps bumped with dated notesNote: #3337 touches the same
insertIntoHeapcomment block; whichever lands second gets a trivial comment conflict.Made with Cursor