You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
HermitCrab feature structures: reuse traversal state in ValueEquals and Freeze (pooled per-thread sets, no isolated measurement) #589
FeatureStruct.ValueEquals and Freeze allocate visited sets and dictionaries on every call. The change allocates them only once a recursive value is reached. Top-level calls borrow pre-sized collections from a [ThreadStatic] pool and clear them in finally. An InUse guard makes reentrant calls use fresh collections. This is a pool of scratch collections, not a cache of results, and it holds only a few collections per thread. The change is on the archived branch perf/hc-optimization-archive.
Measured. The heaviest Sena profile put feature-structure equality and freezing at 13.7% of wall time, with HashSet resizing a major allocation source. After the change, ValueEqualsImpl and those resizes left the top of the profile. It was not timed in isolation, so no speedup is claimed.
What a revival must do first. Measure it alone, in allocations avoided. Check that the pool size stays bounded per thread. Keep FeatureStructPooledVisitedSetsTests (reuse, clearing after success and failure, reentrancy).
Archived from docs/hc-optimization-ledger @ 2eba2f5 (docs/optimizations/feature-struct-visited-state-reuse.md), which is being deleted:
Reuse feature-structure traversal state
Status: retained on perf/hc-optimization-archive, not merged (FeatureStruct.ValueEquals and Freeze). Portable: the allocation-avoidance principle is portable; the [ThreadStatic] pool is
.NET-specific.
Invariant
Equality and freezing need visited sets only after a recursive value is reached. A top-level
operation owns its traversal state until it returns. A nested top-level call cannot borrow that same state,
so it falls back to fresh collections.
Implementation
The recursive APIs accept nullable state and allocate it only when traversal requires it. Ordinary top-level
calls borrow pre-sized sets and dictionaries from a per-thread pool, clear them in finally, and release the
pool. The pool has an InUse guard so reentrant calls retain the old independent-state semantics.
Evidence
The heaviest Sena profile put feature-structure equality/freezing at 13.7% of wall time, with HashSet
resizing a major allocation source. After this change, ValueEqualsImpl and those resizes disappeared from
the top of the profile. This mechanism was not timed in isolation, so no standalone speedup is claimed.
Correctness coverage and limits
FeatureStructPooledVisitedSetsTests covers reuse, clearing after success and failure, and reentrancy. Cyclic
and shared graphs continue down the general path. The complete conformance-inclusive HermitCrab run at the
final code tip passed 603 tests with one expected host-privilege skip.
Activity
johnml1135 commentedon Nov 7, 2026
Archived from
docs/hc-optimization-ledger@ 2eba2f5 (docs/optimizations/feature-struct-visited-state-reuse.md), which is being deleted:Reuse feature-structure traversal state
Status: retained on
perf/hc-optimization-archive, not merged (FeatureStruct.ValueEqualsandFreeze). Portable: the allocation-avoidance principle is portable; the[ThreadStatic]pool is.NET-specific.
Invariant
Equality and freezing need visited sets only after a recursive value is reached. A top-level
operation owns its traversal state until it returns. A nested top-level call cannot borrow that same state,
so it falls back to fresh collections.
Implementation
The recursive APIs accept nullable state and allocate it only when traversal requires it. Ordinary top-level
calls borrow pre-sized sets and dictionaries from a per-thread pool, clear them in
finally, and release thepool. The pool has an
InUseguard so reentrant calls retain the old independent-state semantics.Evidence
The heaviest Sena profile put feature-structure equality/freezing at 13.7% of wall time, with
HashSetresizing a major allocation source. After this change,
ValueEqualsImpland those resizes disappeared fromthe top of the profile. This mechanism was not timed in isolation, so no standalone speedup is claimed.
Correctness coverage and limits
FeatureStructPooledVisitedSetsTestscovers reuse, clearing after success and failure, and reentrancy. Cyclicand shared graphs continue down the general path. The complete conformance-inclusive HermitCrab run at the
final code tip passed 603 tests with one expected host-privilege skip.