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: https://github.com/sillsdev/machine/blob/2eba2f502338705a4d529d7034cecb6303db7f35/docs/optimizations/feature-struct-visited-state-reuse.md
Ledger: https://github.com/sillsdev/machine/blob/2eba2f502338705a4d529d7034cecb6303db7f35/docs/hermitcrab-optimization-ledger.md; disposition: https://github.com/sillsdev/machine/blob/2eba2f502338705a4d529d7034cecb6303db7f35/docs/hermitcrab-perf-2026-09-disposition.md
From #490 (closed).

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

    • Status
      🆕 New

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions