Skip to content

HermitCrab feature structures: reuse traversal state in ValueEquals and Freeze (pooled per-thread sets, no isolated measurement) #589

Open

Description

@johnml1135

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).

Record: see the archived write-up below
Ledger: #490 (comment); disposition: #490 (comment)
From #490 (closed).

Activity

  1. johnml1135 commented on Nov 7, 2026

    @johnml1135
    CollaboratorAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions