ReviewOS

zig-utils/zig-gc

Perf: let embedders allocate same-size cell slabs in one batch

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

Problem

Heap.createBatch amortizes zig-gc metadata publication, but it still invokes the backing allocator once per cell. Embedders with size-class locks therefore pay and contend on the same lock for every slab in the batch.

A post-zig-js #104 8-lane shared object-churn sample attributes 10,150 samples to GcCellBacking.allocFn and 6,955 to gc.allocObjectBatch; the remaining generic Heap.create fallback is only 1,055 samples.

Direction

Add an optional binding hook that fills a same-size raw cell-slab prefix in one operation. Heap.createBatch should use the hook when present, then retain ownership of header initialization, all-list/index publication, nursery counters, and born-grey concurrent-mark handling. Bindings without the hook remain unchanged.

Correctness requirements

  • A zero or short prefix must preserve current allocation-recovery and OOM ordering.
  • Returned slabs remain private until zig-gc publishes the complete prefix under its metadata lock.
  • Concurrent marking must use one marking-state snapshot for the publication batch.
  • Tests cover full batches, short prefixes, first-allocation recovery, and existing parallel publication invariants.
  • README documents the optional hook and exact contract.

Validation

Run zig-gc unit tests once after the implementation is retained, then validate the zig-js consumer under focused regular and TSan tests plus exact shared object-churn A/B.

9 days ago

Implemented and pushed in d35570e.

  • Added the optional Binding.allocateCellBatch contract.
  • Heap.createBatch now accepts a private raw-slab prefix, then keeps header initialization, nursery accounting, born-grey handling, and all-list/index publication inside zig-gc.
  • Zero prefixes retain the normal allocation/recovery path; short non-zero prefixes publish immediately.
  • Added full-prefix/fallback/short-prefix coverage and documented the contract in README.md.
  • Validation: zig build test passed.

Keeping this open until the dependency CI run completes and the zig-js consumer lands.

9 days ago

CI passed on d35570e and the zig-js consumer landed in 0679295 with focused regular/TSan coverage plus the full 764-test engine suite. The reusable contract is complete.

Sign in to comment on this issue.