ReviewOS

zig-utils/zig-js

Intl.NumberFormat: emit format strings without materializing parts

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

Parent: #475 Depends on: #563

Problem

After #563 removes repeated resolved-option decoding, the frozen 360,000-call representative_intl invocation still records approximately 1,080,042 backing allocations and 293,771,203 cumulative backing bytes. Intl.NumberFormat.prototype.format calls the structural nfBuildParts path, which allocates a parts list and integer grouping fragments, then joins those parts into a second output buffer even though only formatToParts/range APIs need structural part boundaries.

Scope

  • Add exact-checksum controls that compare plain format, formatToParts, and range consumers across ungrouped/grouped/Indic, fraction/sign, currency/unit, compact/scientific, non-Latin numbering, NaN/Infinity, and BigInt inputs.
  • Refactor the formatter around one ordered emission algorithm with text and structural sinks; format must emit into one ownership-correct result buffer without first materializing parts.
  • Keep formatToParts, formatRange, and formatRangeToParts exact, including part types, shared affixes/suffixes, and source annotations.
  • Bound temporary numeric/grouping storage and transfer the final string without a precise-GC arena-to-managed copy where ownership permits.

No-workaround rules

  • No duplicate formatting algorithm, locale/workload recognition, skipped grouping/numbering translation, fixed maximum output truncation, borrowed stack bytes, or special case limited to the representative row.
  • Preserve all coercion order, BigInt exactness, rounding, signs/accounting, locale symbols, and OOM behavior.

Acceptance

  • Focused rows freeze exact string/part/range checksums before implementation.
  • Attribution shows the retained allocation and cumulative-byte deltas for the frozen row plus focused controls.
  • Seven-pair exact-parent evidence improves plain format without regressing structural consumers outside noise.
  • Exact-parent Intl402 and BigInt locale compatibility remain regression-free.
  • OOM, bounded heap, moving GC, no-GIL, TSan, and full units pass with zero leaks.
9 days ago

Benchmark boundary landed in 2749108a. The four 250-job controls freeze plain text, structural parts, range text, and range parts across eight formatter configurations plus negative zero, NaN, Infinity, fractional values, large integers, and BigInt. Parent medians are 53,296,209 / 135,265,041 / 86,013,958 / 379,120,208 ns; checksums are 23,988,000 / 35,256,000 / 11,638,523 / 160,111,647.

9 days ago

Implemented and pushed to main in 1b24c77f, following the frozen controls from 2749108a.

The formatter now uses one ordered emission algorithm with direct text and structural sinks. Plain format no longer materializes a parts array, integer grouping uses slices rather than copied fragments, localized digits are emitted by the selected sink, and range APIs reuse their already-built structural output instead of formatting both endpoints twice.

Seven-pair exact-parent medians (ns, parent -> candidate; all checksums exact):

  • representative Intl: 521,045,458 -> 515,189,625 (-1.12%)
  • representative Intl variant: 515,591,250 -> 512,913,291 (-0.52%)
  • text: 52,878,083 -> 51,658,541 (-2.31%)
  • parts: 132,489,042 -> 133,323,417 (+0.63%, noise-level)
  • range: 85,231,959 -> 74,212,709 (-12.93%)
  • range parts: 369,544,625 -> 358,144,291 (-3.08%)

Scored backing-byte deltas (parent -> candidate): representative Intl 294,556,993 -> 151,032,957 (-48.73%); text 19,057,407 -> 8,808,959 (-53.78%); parts 31,004,571 -> 31,004,775 (flat); range 32,264,705 -> 24,498,781 (-24.07%); range parts 74,958,714 -> 67,315,296 (-10.20%). Allocation-request deltas are unchanged or one lower. Current backing-byte deltas also fall 74.52% for representative Intl, 81.22% for text, 30.34% for range, and 32.05% for range parts; parts remains flat.

Validation:

  • Intl402: 3,341/3,341 pass (exact parent 3,341/3,341; 0 test262 flips)
  • focused NumberFormat: 7/7 pass
  • focused TSan: 7/7 pass
  • OOM, bounded precise heap, moving nursery, and four-lane no-GIL publication coverage pass
  • full sharded units: 1,646 pass, 1 skip, 0 fail, 0 leak

All acceptance criteria are satisfied.

Sign in to comment on this issue.