Skip to content

perf: copy plain host results and JSON directly, and skip conversion arrays for primitive string and Math arguments - #107

Merged
rekram1-node merged 1 commit into
devfrom
perf-quick-wins
Nov 27, 2026
Merged

rekram1-node merged 1 commit into
devfrom
perf-quick-wins

Conversation

@rekram1-node

Copy link
Copy Markdown
Member

Low-risk fast paths in the host boundary, string/Math built-ins, cycle checks and property enumeration. No opcode, compiler or prelude change, so the build id and snapshots are unaffected.

Changes, each measured with a targeted program (fresh process per run, median of 5 runs, best of 3 alternating base/new runs; the machine was loaded, load average ~10–16):

change where probe before after
Plain host arrays and records (Array.prototype / Object.prototype / null prototype) copy without the instanceof chain, per-value closures and Object.entries, straight into fresh slots runtime/extensions.ts copyPlain 600 host calls returning 50 objects 22.5 ms 13.5 ms (-40%)
one 5k-row result, grouped 5.8 ms 3.6 ms (-37%)
String methods with a string receiver and primitive arguments skip withPrimitives and its array copies (simple, includes/startsWith/endsWith); containsOpaqueReference/containsRuntimeReference return early for primitives stdlib/string.ts, runtime/references.ts 300k indexOf/slice/includes calls 63.7 ms 25.1 ms (-61%)
100k padStart + split 62.4 ms 40.4 ms (-35%)
Unary and binary Math functions take number arguments directly stdlib/math.ts 600k Math.abs/Math.max 64.7 ms 50.4 ms (-22%)
Cycle check walks slots and elements with an explicit stack instead of key lists, iterators and closures runtime/references.ts hasCircularReference assign a 2k-object array 200 times 72.9 ms 14.2 ms (-81%)
filter 20k objects ×5 22.4 ms 15.8 ms (-29%)
PUT_ELEM append/overwrite fast path also for object values, after the cycle check vm.ts out[i] = { i } 200k 48.9 ms 25.5 ms (-48%)
Object.keys/Object.entries in one pass over the slots when no key is an index; parseArrayIndex tests the first character before the regex runtime/objects.ts 50k keys + entries on 5 keys 32.1 ms 22.4 ms (-30%)
JSON.parse results (fromJson) written straight into fresh slots, primitives without a recursive call data.ts parse a 4k-row string ×5 16.1 ms 8.6 ms (-47%)
Object literal keys written straight into the fresh object's slot vm.ts execDefineField 200k 4-key literals 27.0 ms 26.4 ms (within noise)
Whether a native wants its call site, computed once per native runtime/objects.ts, vm.ts 1M Number.isInteger 65.5 ms 64.0 ms (within noise)
300k push({ i }) 35.9 ms 31.7 ms (-12%)

script/bench.ts (best of 6 alternating runs, ms; --mixed is one process, all cases):

case fresh before fresh after mixed before mixed after
JSON round-trip 29.2 24.1 (-17%) 24.3 23.6 (-3%)
property-heavy 45.0 38.5 (-14%) 44.9 40.3 (-10%)
destructuring and spread 16.3 18.6 13.4 14.1
everything else within noise (±15% run to run on this machine)

Behaviour kept the same (new test/fast-paths.test.ts, which also passes on dev):

  • The host-result fast path still fails a primitive past the depth limit, as fromHost does; the first cut took primitives one level down without the check.
  • fromJson keeps holes in bound arrays (kept map instead of a for…of that filled them with undefined).
  • Key order with index-like keys, __proto__ as an own data key (host results, JSON.parse, object literals), duplicate and accessor keys in literals, spread, non-enumerable and symbol keys, cycle errors through arrays, and string/Math arguments with toString/valueOf are covered.
  • The direct slot writes are only used on objects made in the same step (a fresh Obj with no guard and intrinsic 0), where they match what define does. Class fields still go through defineOwnProperty.

bun run check passes.

…arrays for primitive string and Math arguments
@rekram1-node
rekram1-node merged commit e8380b6 into dev Nov 27, 2026
1 check passed
@rekram1-node
rekram1-node deleted the perf-quick-wins branch November 27, 2026 18:52
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