Skip to content

perf(runtime): speed up map_iterable for arrays and sized collections - #67

Merged
leonidaz merged 3 commits into
mainfrom
cursor/map-iterable-dispatch-sized-ca6d
Sep 5, 2026
Merged

leonidaz merged 3 commits into
mainfrom
cursor/map-iterable-dispatch-sized-ca6d

Conversation

@jonkwheeler

Copy link
Copy Markdown
Contributor

Summary

Third-pass runtime audit after #59 and #66. Those PRs own iterable_array_from; this change does not touch it.

map_iterable is the compiler-emitted @for helper on React/Preact. Two costs showed up in measurement:

  • Array bodies that return a single node (the common case) still grew the result with push.
  • Set/Map paid two iterator.next() calls per item just to compute is_last.

The first mapped value now chooses the path: a single node preallocates; an array-valued (fragment) result keeps the previous push-flatten loop. Set and Map use size for is_last the same way arrays already use length. Custom iterators that happen to have size still peek ahead.

Audit notes

Measured on Node v22.14.0, linux x64, isolated perf_hooks processes (warmup 200ms, 400ms × 13 runs, median). Candidates that did not ship:

  • map_iterable always-prealloc / mid-loop hybrid — fragment lists still regress (same mixed result as prior audits)
  • escape replace — slower on clean and dirty short strings
  • escape charCodeAt — +24% to +85% dirty, −73% on clean medium text
  • array_slice explicit start/end vs rest/spread — 0% once compared to the real implementation
  • exclude_prop_from_object via Object.keys + symbols — +10% to +16%, same band previously discarded, and not compiler-emitted

#59/#66 iterable_array_from paths were not revisited.

Benchmarks

Node v22.14.0, linux x64. Isolated processes. After column is the real @tsrx/runtime map_iterable export.

Workload Before After Delta
array 100, vnode (common @for) 1,000,810 ops/s 1,127,693 ops/s +12.7%
array 100, text 447,844 ops/s 505,842 ops/s +13.0%
array 1000, text 45,861 ops/s 54,015 ops/s +17.8%
array 10, text 3,830,606 ops/s 4,013,909 ops/s +4.8%
array 100, fragment 352,690 ops/s 353,058 ops/s +0.1%
array 10, fragment 3,096,886 ops/s 3,171,841 ops/s +2.4%
Set 100, text 405,704 ops/s 555,704 ops/s +37.0%
Map 100, text 89,713 ops/s 94,681 ops/s +5.5%
CONTROL generator 3 1,008,701 ops/s 997,557 ops/s −1.1%

Generators stay on the peek-ahead path (control). Fragment bodies stay on push-flatten.

Test plan

  • Runtime coverage for array scalar/is_last, fragment flatten, mixed later-array flatten, Set/Map size is_last, custom iterator+size, generators/raw iterators, empty/tail, non-iterable reject
  • pnpm exec vitest run --project tsrx-utils (21 files, 509 tests)
  • Re-ran the same isolated microbenchmark against the real export
  • prettier --check on touched files; pnpm changeset:check
Open in Web Open in Cursor 

@jonkwheeler
jonkwheeler marked this pull request as ready for review September 4, 2026 18:14
@vercel

vercel Bot commented Sep 4, 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 5, 2026 9:27pm UTC
tsrx-website-mcp Ready Ready Preview Sep 5, 2026 9:27pm UTC

Request Review

@cursor

cursor Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

READY FOR REVIEW

@leonidaz

leonidaz commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

typechecks are not passing.

@cursor

cursor Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

@leonidaz typecheck is fixed on this branch.

The TS7006 errors were the untyped text_fn callback params in packages/tsrx/tests/utils/iterable-runtime.test.js. Those now have JSDoc types (unknown / number / boolean), matching the other runtime tests.

pnpm typecheck also needed JSDoc on the empty U[] accumulators in packages/tsrx-runtime/src/iterable.js (TS7034/TS7005). No behavior change.

pnpm typecheck is green, and the iterable runtime tests still pass.

cursoragent and others added 2 commits September 4, 2026 22:45
Preallocate the result after the first mapped value so the common single-node
@for body avoids repeated push, and use Set/Map size for is_last instead of
peeking the next iterator result. Fragment bodies and custom iterators keep
the previous push / peek-ahead paths.

Co-authored-by: Jon Wheeler <[email protected]>
Add JSDoc types so checkJs no longer reports implicit any on the
iterable runtime test helper and the empty result accumulators.

Co-authored-by: Jon Wheeler <[email protected]>
@leonidaz
leonidaz merged commit 20ff7d5 into main Sep 5, 2026
11 checks passed
@leonidaz
leonidaz deleted the cursor/map-iterable-dispatch-sized-ca6d branch September 5, 2026 21:49
@github-actions github-actions Bot mentioned this pull request Sep 5, 2026
leonidaz added a commit that referenced this pull request Sep 7, 2026
…erable (#86)

The sized fast path added in #67 cached `size` and read `iterator.next().value`
without checking `done`, so a callback that cleared, deleted from, or added to
the Set or Map being iterated received `undefined` items, missed added entries,
and saw `is_last` on the wrong item. Route Set and Map back through the
peek-ahead iterator path and keep the array preallocation.

Reported downstream as octanejs/octane#977.

Co-authored-by: Claude Fable 5.1 <[email protected]>

This branch was successfully deployed

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