ReviewOS

zig-utils/zig-js

Intl.NumberFormat: reuse static cells for validated resolved options

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

Parents: #475, #479 Related: #461, #493, #603

Problem

IntlNumberFormatData retains several resolved choices as byte slices even though constructor processing has already validated each against a finite ECMA-402 set. Intl.NumberFormat.prototype.resolvedOptions() currently allocates a managed StringCell again for each of those fields on every call.

The four existing NumberFormat constructor rows each execute 3,200 constructor + resolvedOptions() operations per 100 jobs. Current-main (10ac2f3d) exact invocation baselines are:

  • default: 41,607 managed cells / 73,623 backing allocations / checksum 56,000
  • fraction: 44,808 / 73,624 / checksum 59,200
  • locale: 44,809 / 73,625 / checksum 59,200
  • uncommon: 80,016 / 131,230 / checksum 107,200

Scope

  • Project only constructor-validated finite fields through immutable static StringCells: style, currencyDisplay, currencySign, unitDisplay, notation, compactDisplay, signDisplay, roundingMode, roundingPriority, and trailingZeroDisplay.
  • Keep locale, numberingSystem, currency code, unit identifier, and all other dynamically owned values on their current allocation paths.
  • Make the allowed spelling set explicit at each projection site and fail internal invariant violations rather than silently misprojecting bytes.
  • Preserve ECMA-402 field presence/order, significant-vs-fraction digit rules, grouping booleans/strings, option-observation order, OOM atomicity, and every formatter result.

No-workaround rules

  • Do not cache/reuse observable resolved-options objects, skip unread properties, recognize benchmark source/name, globally intern attacker-controlled strings, or turn dynamic option data into process-global state.
  • Do not touch Promise, microtask, Worker, Thread lifecycle, stacks, queues, or jobs code.

Acceptance

  • Every finite resolved field is the exact unmanaged static cell for its validated spelling; dynamic strings remain managed/owned as before.
  • Exact allocation reductions match the emitted fixed-field count in all four existing rows, with identical execution, Shape requests, and checksums.
  • Exact test262 NumberFormat coverage has zero flips/regressions.
  • Focused normal/TSan, bounded/moving GC, OOM, and full sharded unit gates pass.
  • Order-balanced timing is reported for all four rows; only stable attributable results are claimed.

Security boundary

Options and call frequency are attacker-controlled. Static projection is permitted only after the existing validation and native-state publication boundary; unexpected internal bytes must not fall through to a wrong public value or mutable global cache.

9 days ago

Implemented and pushed to main in b5dd6761 (perf(intl): reuse static NumberFormat options (#604)).

Only constructor-validated finite fields use static cells. Locale, numberingSystem, currency code, unit identifier, and other dynamic values retain managed ownership. Each projection site declares its complete accepted spelling set; an impossible post-validation value panics instead of falling through.

Exact 3,200-operation managed-cell evidence:

  • default: 41,607 -> 22,407 (-19,200; six per operation), GC cell bytes 3,482,816 -> 2,254,016 (-1,228,800)
  • fraction: 44,808 -> 25,608 (-19,200), GC cell bytes 3,892,480 -> 2,663,680 (-1,228,800)
  • locale: 44,809 -> 25,609 (-19,200), GC cell bytes 3,892,544 -> 2,663,744 (-1,228,800)
  • uncommon currency+compact: 80,016 -> 51,216 (-28,800; nine per operation), GC cell bytes 6,145,792 -> 4,302,592 (-1,843,200)

Execution counters, Shape requests, and frozen checksums are identical. In 21 order-balanced pairs, median wall improves by 3.953% (default, 21/21 wins), 3.523% (fraction, 21/21), 3.604% (locale, 21/21), and 12.192% (uncommon, 20/21).

Validation:

  • exact test262 NumberFormat: 249/249 pass before and after, 0 failures/flips
  • exhaustive 38-spelling exact static-cell test plus dynamic-locale ownership control: normal and TSan pass
  • focused bounded/moving/parallel/OOM/BigInt/CLDR/structural group: 15/15 pass
  • full units: 1,720 passed, 1 skipped, 0 failed, 0 leaked

Sign in to comment on this issue.