Skip to content

perf: single-pass ref classification and flat array-ref cleanups - #116

Merged
trueadm merged 1 commit into
mainfrom
jon/tsrx-runtime-perf
Sep 15, 2026
Merged

trueadm merged 1 commit into
mainfrom
jon/tsrx-runtime-perf

Conversation

@jonkwheeler

@jonkwheeler jonkwheeler commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Object-ref classification and array-ref attach are now ~10-31% faster on the hot paths. Each call to is_ref_object ran is_dom_node plus its own-property and prototype-accessor probes, and apply_ref_value, mergeRefs, and collect_ref_cleanups called it twice per ref (once for current, once for value). A new ref_object_prop helper runs the DOM-node check and all key probes once, preserving the original classification order (current precedence, __v_isRef before own/prototype value). Array-valued refs previously allocated a closure per item; they now collect flat [kind, payload] cleanup pairs and return a single replay closure.

Measurements

Same-process interleaved A/B against a baseline module copy, median ns/call:

Workload Baseline Candidate Delta
apply_ref_value plain non-ref object ~56-58 ~50-52 -8% to -14%
apply_ref_value { value } ref ~32-35 ~29-31 -10% to -18%
Vue-style ref ~27 ~23-24 -10% to -14%
Mixed array refs through apply_ref_value ~145-152 ~102-109 -28% to -31%
mergeRefs with 3 value refs ~105 ~93-95 -10% to -12%

Controls (callback refs, mergeRefs function refs, normalize_spread_props* paths) measured within the demonstrated noise band on the same runs. The cleanup replay loop stays inline at all three call sites deliberately: extracting a shared replay_ref_cleanups helper was benchmarked and regressed callback-ref paths ~12-19%, so each site carries a comment pinning the duplication.

Residual risk

ref_object_prop takes a single snapshot: is_dom_node runs once per classification where baseline ran it once per probed key. An object whose nodeType/nodeName getters or Proxy traps mutate its own shape mid-classification can therefore observe one fewer trap sequence, and a pathological accessor can flip the verdict entirely (both directions verified by PoC). No in-repo emitter or real ref object produces such shapes, no test pins the behavior, and re-probing would defeat the optimization — noted here so the delta is explicit rather than silent.

Test plan

  • packages/tsrx/tests/utils/ref-runtime.test.js — 56 tests, including new pins for current-over-value precedence, primitive-ref TypeError, array cleanup replay order, and the empty-array undefined sentinel
  • Full tsrx-utils project — 627 tests
  • Runtime integration projects (react, preact, solid, vue) — 219 tests
  • pnpm typecheck, pnpm format:check, changeset check — all clean

Note

Medium Risk
Hot-path ref semantics changed for edge cases (objects with both current and value, pathological getters/Proxies during classification); well-tested for normal ref shapes but classification is now a single snapshot vs double-probe.

Overview
Speeds up ref attach/teardown in @tsrx/runtime by classifying object refs once per value instead of probing current and value separately (which duplicated the DOM-node check).

Replaces is_ref_object with ref_object_prop, used from mergeRefs, apply_ref_value, and collect_ref_cleanups, keeping the same resolution order (current over value, Vue __v_isRef, inherited value accessors). Array-valued refs in apply_ref_value now go through apply_ref_array, building flat [kind, payload] cleanup pairs like the merge path instead of one nested closure per item. The cleanup replay loop stays duplicated inline at three call sites on purpose (a shared helper regressed benchmarks).

Types and tests are updated: ref.d.ts points at ref_object_prop, and ref-runtime.test.js pins current-over-value, primitive ref TypeError, array cleanup order, and empty-array undefined behavior, plus extra normalize_spread_props_for_ref_attr coverage.

Reviewed by Cursor Bugbot for commit db860dc. Bugbot is set up for automated code reviews on this repo. Configure here.

is_ref_object ran is_dom_node plus its own-property and prototype-accessor
probes once per key ('current', then 'value'). Replace the two calls with a
single ref_object_prop classification that preserves probe order, and make
apply_ref_value collect array refs as flat [kind, payload] cleanup pairs
instead of allocating a closure per item.

Measured -10% to -31% on object-ref and mixed-array attach paths; callback
and normalize controls within noise.
@vercel

vercel Bot commented Sep 15, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
tsrx-website Ready Ready Preview Sep 15, 2026 7:41pm UTC
tsrx-website-mcp Ready Ready Preview Sep 15, 2026 7:41pm UTC

Request Review

This branch was successfully deployed

2 active deployments
Preview – tsrx-website-mcp — db860dc2 Deployed Sep 15, 2026 by vercel[bot]
Preview – tsrx-website — db860dc2 Deployed Sep 15, 2026 by vercel[bot]
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.

3 participants