fix(scenegraph): cross script-scope m as live references, only on owner transfer - #1098
Merged
Conversation
lvcabral
force-pushed
the
fix/xthread-m-live-refs
branch
from
July 28, 2026 22:55
c8822d7 to
e12ebfa
Compare
…er transfer Two device-confirmed corrections to how a node's script-scope `m` crosses threads, both measured with an instrumented app run on hardware. `m` holds a LIVE node reference. Mutating a node stored in `m` from the receiving thread is visible through `m` on a device; here it was a detached snapshot that silently stopped tracking. Node-valued entries now cross as an address and resolve to this thread's own instance, live-reachability first with the cross-thread registry as fallback (the ordering Task.resolveNode documents). An address with no local instance is dropped with a one-shot warning rather than restored as a stale copy. That also removes the dominant cost. A component caching m.scene / m.parentScreen dragged the whole scene tree into the payload: measured 44.4 KB against 0.2 KB for the same node with scalar-only `m`, a 230x blowup paid on every serialization. It is now 0.3 KB. `m` also only needs to travel when the node changes owner. A callFunc on a node owned by another thread rendezvouses to that owner and runs against the owner's `m` (device-confirmed), so the copy shipped everywhere else was never read. `_m_` is now opt-in and enabled only at the task -> render ownership transfer, removing it from callFunc args, rendezvous responses, render-thread-queue posts and nodes nested in associative arrays. Not covered: a node reached through an array or associative array inside `m` still serializes as a copy. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
lvcabral
force-pushed
the
fix/xthread-m-live-refs
branch
from
July 28, 2026 23:05
e12ebfa to
99e30b6
Compare
|
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.



Both corrections come from an instrumented app run on a real device (
out/xthread-m-test/), which answered three questions.mreaches the render threadtask/T1task/T1✅task/T1✅m, after mutating it on the render threadchanged-on-renderset-in-task❌changed-on-render✅callFuncon a render-owned node — whosemran?callCount2)1.
mholds a live reference, not a snapshotOn a device
mis one associative array holding real node references — mutating a stashed node from the other thread is visible throughm. Here it was a deep copy: a detached snapshot that silently stopped tracking.Node-valued entries now cross as an address (
_mref_) and resolve to this thread's own instance — live-reachability first, cross-thread registry as fallback, the orderingTask.resolveNodedocuments after the stale-duplicate regression. An address with no local instance is dropped with a one-shot warning rather than restored as a stale copy.2. That was also the dominant cost
A component caching
m.scene/m.parentScreen— the ordinarym.scene = m.top.getScene()idiom — dragged the entire scene tree into the payload:mcontentsscene+parentScreen44.1 KB of that was the scene tree, from a deliberately small 144-item screen.
3.
_m_only needs to travel on an ownership transferSince a
callFuncon a node owned by another thread rendezvouses to that owner and runs against the owner'sm, the copy shipped everywhere else was never read._m_is now opt-in, enabled only at the task → render ownership transfer — removing it from callFunc args, rendezvous responses, render-thread-queue posts, children, and nodes nested in associative arrays.Not covered
A node reached through an array or associative array inside
m(e.g.m.managers = {a: node}) still serializes as a copy. Emitting references there means threading a mode flag throughjsValueOf, which is very hot and used everywhere; left out to keep this diff reviewable. Worth revisiting if it shows up in profiling.Tests
NodeSerialization.test.js— reference emission and live resolution (a later mutation is visible through the restoredm), the unresolvable-address drop,_m_absent on non-transfer serializations, plus the existing round-trip cases updated for the opt-in flag.Full suite green (2225), lint and prettier clean.
🤖 Generated with Claude Code