Problem
Every sweep invokes optional traceEphemeron and afterWeak passes by scanning the all-cells list, even when an embedder has no weak references, ephemeron entries, or finalization records. A post-zig-js #105 shared object-churn sample attributes 676 samples to the ephemeron pass around sweepPhaseLocked line 1383 and 327 samples to afterWeak around line 1405. That workload creates no weak semantic state.
Direction
Add an optional conservative binding predicate such as hasWeakWork(ctx). When present and false, sweep may skip the ephemeron fixed-point scan, weak-slot clearing, and afterWeak scan. Bindings without the predicate retain current behavior. The predicate must never return false after any weak state can be observed by the collector; a monotonic false-to-true embedder flag is a valid implementation.
Correctness
- False must mean there are no weak slots, ephemeron edges, or afterWeak cleanup records to process.
- Once true, an embedder may conservatively remain true for the heap lifetime.
- Full, minor, incremental, concurrent, and parallel collection semantics must remain unchanged when weak work exists.
- Tests cover false scan elision, true weak processing, and bindings without the hook.
- README documents the optional contract.
Validation
Use the zig-js consumer to verify WeakMap, WeakSet, WeakRef, and FinalizationRegistry behavior, then run focused shared object-churn A/B before retaining the optimization.