Skip to content

fix(runtime): tolerate Set/Map mutation during map_iterable - #86

Merged
leonidaz merged 1 commit into
mainfrom
fix/map-iterable-set-map-mutation
Sep 7, 2026
Merged

leonidaz merged 1 commit into
mainfrom
fix/map-iterable-set-map-mutation

Conversation

@leonidaz

@leonidaz leonidaz commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes the @tsrx/runtime 0.1.5 regression reported downstream in octanejs/octane#977.

PR #67 added a map_sized fast path for Set/Map that cached size and read iterator.next().value without checking done. When a @for callback mutates the collection it is iterating, that path:

  • passes undefined as the item once the collection shrinks (Map destructuring then throws)
  • never visits entries added during iteration
  • reports is_last on the wrong item

This restores the 0.1.2 behaviour by sending Set and Map through the existing peek-ahead iterator loop. The array preallocation path from #67 is unchanged.

Tests

New cases in iterable-runtime.test.js cover clear, delete-before-visit, and add-during-iteration for both Set and Map, asserting values, indices, is_last, plus tail handling after mutation.

Performance

Isolated microbenchmark, Node 24, String(item) body, 100 entries:

Source pre-#67 #67 (broken) this PR
Set 100 364k ops/s 750k ops/s 370k ops/s
Map 100 39k ops/s 42k ops/s 35k ops/s

A hybrid that keeps peek-ahead but preallocates the result from size measured ~505k ops/s on Set and no change on Map. It was left out to keep this a minimal correctness fix; it can be a follow-up if the Set number matters.

Validation

  • pnpm exec vitest run --project tsrx-utils (23 files, 605 tests)
  • pnpm typecheck
  • pnpm exec prettier --check on touched files, pnpm changeset:check

🤖 Generated with Claude Code


Note

Low Risk
Targeted runtime bugfix with broad new tests; behavior matches pre-#67 semantics aside from intentional Set/Map perf regression.

Overview
Reverts the Set/Map size-based fast path in map_iterable (is_sized_iterable / map_sized) so those collections use the same peek-ahead iterator loop as other iterables. That fixes a 0.1.5 regression where mutating the collection inside the callback could yield undefined items, skip newly added entries, and set is_last too early.

Arrays still use the preallocated map_array path from #67; only default Set/Map iteration behavior changes (with a perf tradeoff on large sets).

Tests in iterable-runtime.test.js now cover clear, add during iteration, delete before visit, and tail handling for mutated sets/maps.

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

…erable

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]>
@vercel

vercel Bot commented Sep 7, 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 7, 2026 12:28am UTC
tsrx-website-mcp Ready Ready Preview Sep 7, 2026 12:28am UTC

Request Review

This branch was successfully deployed

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

1 participant