ReviewOS

zig-utils/zig-js

VM: quicken computed numeric TypedArray reads

#1045
Closed chris opened this 9 days ago ·
9 days ago

Parent: #498 Related: #493, #473

Problem

VM get_index specializes present dense Array elements, but computed numeric reads from TypedArrays always call nativeGetIndex, allocate/coerce a property-key string, and re-enter generic [[Get]]. Primitive Number indices have no user-visible coercion, and Integer-Indexed Exotic [[Get]] can resolve an in-bounds element or an out-of-bounds undefined directly.

A pre-fix VM witness covering 0, numeric -0, out-of-bounds, negative/fractional, string "-0", and BigInt elements matches Node, while the new test counter records zero typed-index fast hits. The generic path is therefore semantically correct but misses this general opcode specialization.

Scope

  • Add a VM get_index fast path for primitive numeric TypedArray indices.
  • Preserve numeric -0 → property key "0", string "-0" integer-index rejection, out-of-bounds/detached/resized behavior, BigInt result allocation, and Thread.restrict enforcement.
  • Keep object/Symbol/string key coercion and every exotic fallback on canonical ToPropertyKey + [[Get]].
  • Record the specialization in the source-derived VM quickening inventory and its drift gate.

Acceptance

  • The pre-fix witness changes from zero to a positive typed-index fast-hit count with identical results.
  • Numeric in-bounds and invalid indices, BigInt elements, buffer state changes, and restriction behavior are pinned.
  • The focused VM module test and VM inventory validation/self-test pass.
  • No benchmark-shaped recognizer, key-coercion skip for objects, or unsynchronized buffer access.
9 days ago

Implemented and pushed in 63866b8b.

get_index now resolves every primitive Number key directly for a TypedArray receiver. Valid indices use the existing synchronized element read; numeric -0 maps to element zero; detached, resized-out, invalid, fractional, infinite, NaN, negative, and large out-of-range indices return undefined. Non-Number keys still execute canonical ToPropertyKey and [[Get]], and the fast path checks restriction ownership before reading.

Evidence:

  • pre-fix VM witness: correct result, zero typed-index fast hits
  • after fix: identical result, positive fast hits; Node agrees on the full edge matrix
  • focused VM test: 3/3 pass
  • focused unsuppressed TSan: 3/3 pass
  • quickening inventory validation/self-test: 17 guarded families and five explicit unsupported families
  • benchmark harness validators: 19/19 pass
  • docs: 51 pages with exact manifest; 378 offline links pass

The local Test262 corpus is unavailable, so no Test262 flip count is claimed.

Sign in to comment on this issue.