ReviewOS

zig-utils/zig-js

JSON.parse: batch sustained private container allocation

#1009
Closed glennmichael123 opened this 9 days ago ·
9 days ago

Parent: #473

Measured problem

After #1008, the frozen representative_json invocation allocates 1,001,001 GC cells. Its unpublished JSON.parse containers still take one allocator-slab lock and one publication lock per object or array. Exact current-main attribution recorded 4,954,046 cell-slab lock acquisitions, including 1,001,001 single-allocation and 1,001,001 publication acquisitions.

The existing Interpreter.gc_object_reserve is traced, relocated, and already fed through gc.allocObjectBatch for sustained VM allocation. JSON.parse can use the same rooted reserve because each container remains private until the parse succeeds.

Executed ReleaseFast/real-GC adaptive prototype against pushed #1008:

  • total invocation cells remain exactly 1,001,001;
  • base slab-lock acquisitions fall 4,954,046 -> 3,114,143 (-37.1%);
  • variant slab-lock acquisitions fall 4,548,530 -> 3,110,056 (-31.6%);
  • single-allocation acquisitions fall 1,001,001 -> 407,017;
  • publication acquisitions fall 1,001,001 -> 444,141;
  • 37,124 bounded 16-cell batch acquisitions replace the removed per-cell traffic.

The policy is adaptive: the first 16 private containers in each interpreter invocation retain exact one-cell allocation, and only sustained demand fills a 16-cell rooted reserve. A real-GC unit proves both the cold and sustained thresholds.

Three alternating exact-parent pairs, exact checksums and thermal state 0 throughout:

  • representative_json: 0.993529x retired instructions;
  • representative_json_variant: 0.989040x;
  • representative_json_reviver_source: 0.985153x;
  • escaped-string control: 0.997527x;
  • depth-4096 stringify control: 0.994145x;
  • shallow-4096 stringify control: 0.999669x.

Scope

  • Route only JSON.parse's unpublished object/array construction through a sustained private allocation helper.
  • Preserve arena/no-GC mode as one exact allocation per container.
  • Reuse the existing traced and relocated interpreter object reserve; do not add a second root mechanism.
  • Preserve parse atomicity, prototypes, array flags, OOM behavior, moving-GC safety, no-GIL isolation, and all observable allocation semantics.

Acceptance

  • Seven alternating exact-parent pairs retain the parse gains with exact checksums and neutral controls.
  • Cold first-16 and sustained 17th-container behavior is pinned under real GC.
  • JSON test262 is 165/165 and the same files agree in required-VM mode.
  • Real-GC JSON/OOM and 50,000-level deep-input probes pass.
  • Moving-GC/root relocation coverage and no-GIL JSON.parse TSan witnesses pass.
  • Commit lands directly on main with evidence linked here and in #473.
9 days ago

Landed directly on main as c8d9027.

Final evidence

Seven alternating exact-parent ReleaseFast/real-GC pairs against pushed #1008, with exact checksums and thermal state 0 -> 0 in all 84 invocations:

rowcandidate / parent instructionsCPUwallinstruction RSD parent/candidate
representative_json0.990045x0.975397x0.977660x0.547% / 0.489%
representative_json_variant0.994758x1.018799x1.021360x0.559% / 0.550%
representative_json_reviver_source1.002510x1.021635x1.021540x0.507% / 0.613%
escaped-string control1.000521x1.003846x1.005136x0.032% / 0.089%
depth-4096 stringify control0.999807x1.004515x1.005418x0.131% / 0.570%
shallow-4096 stringify control0.999923x0.948094x0.931740x0.174% / 0.262%

The primary deterministic metric retains the intended gain on both frozen parse rows. Reviver and controls remain within measured instruction variance; no checksum or thermal drift occurred.

Attribution on the exact landed source:

  • total invocation cells: 1,001,001 -> 1,001,001;
  • base slab-lock acquisitions: 4,954,046 -> 3,114,143;
  • variant slab-lock acquisitions: 4,548,530 -> 3,110,056;
  • single-allocation acquisitions: 1,001,001 -> 407,017;
  • publication acquisitions: 1,001,001 -> 444,141;
  • bounded batch acquisitions: 0 -> 37,124.

Gates

  • Cold first-16 and sustained 17th-container behavior pinned under real GC and TSan.
  • 34/34 focused JSON/OOM/GC tests.
  • JSON test262 165/165.
  • Required-VM agreement 165/165.
  • No-GIL JSON.parse TSan witness 2/2.
  • Interpreter reserve tracing/relocation test 2/2.
  • 50,000-level array/object probe in tree and VM modes.
  • Guarded build/test peaks remained 1.97-2.13 GB, below the 3 GB cap.
  • Commit author and committer are glennmichael123 <gtorregosa@gmail.com> with no trailers.

Sign in to comment on this issue.