ReviewOS

zig-utils/zig-gc

Perf: validate trusted typed edges without live-list fallback

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

Downstream: zig-utils/zig-js#62 Related: #4

Problem

Visitor.isManaged must safely accept wild legacy/embedder pointers, so a miss in the exact ownership/index paths falls back to a full live-cell-list scan. Precise runtime edges often hold a valid typed allocation that may be GC-managed or arena/embedded. Treating those as wild pointers makes an embedded root Environment scan the entire heap during every young collection.

A post-optimization zig-js object_churn profile attributes about 22% of samples to headerForPayload, primarily Environment parent/root edges that miss the managed heap.

Design

  • add a trusted typed Visitor predicate for valid typed allocation pointers;
  • derive the exact allocation size from the pointee type;
  • when the binding guarantees that managed cells of that size use owned storage, validate only the matching owned size class and header magic;
  • return false directly for a valid arena/embedded allocation instead of falling back to the live-cell list;
  • retain isManaged unchanged for arbitrary, stale, interior, and wild pointers;
  • preserve generic fallback behavior for bindings without owned typed storage.

Acceptance criteria

  • Typed managed cells validate and can be marked.
  • Valid typed stack/arena/embedded objects reject without the live-list fallback.
  • Broad isManaged retains safe wild/interior/stale rejection.
  • Bindings without owned storage retain existing behavior.
  • zig-gc tests pass.
  • Downstream profile removes the repeated headerForPayload live-list cost with identical checksums.
9 days ago

Profiling follow-up showed the affected zig-js edges already carry, or can carry, exact immutable ownership metadata (Environment.gc_managed and Promise.gc_owned). Using those flags avoids membership validation entirely and is safer and faster than adding a trusted-pointer API to generic zig-gc. The dependency prototype was fully reverted; no zig-gc code landed for this issue. The downstream implementation is zig-utils/zig-js@8e861ec and zig-utils/zig-js#64.

Sign in to comment on this issue.