Skip to content

HermitCrab feature structures: allocation-free ValueEquals for frozen flat trees (no isolated measurement) #588

Open

Description

@johnml1135

Freeze marks a FeatureStruct as a flat tree when every reachable value is a SimpleFeatureValue, no value instance repeats and no nested FeatureStruct exists. FeatureStruct.ValueEquals then compares two such frozen structures by feature count, key and value, with no visited-pair set. This is equivalent to the general walk, because a flat tree cannot trigger its reentrancy or cycle guard. Shared leaves, nested structures and cycles use the general path. A test-only entry point lets randomized tests compare the two paths. The change is on the archived branch perf/hc-optimization-archive.

Measured. Nothing in isolation. The same profile that motivated traversal-state reuse put feature-structure equality and freezing at 13.7% of wall time on the heaviest Sena word. The record claims no standalone ratio.

What a revival must do first. Time it separately from the other feature-structure changes, in unification or visited-set operations avoided. Keep the randomized cross-check against the slow path (FeatureStructTreeFastPathTests, FeatureStructValueEqualsFreezeTests).

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-frozen-tree-equality.md), which is being deleted:

    Allocation-free equality for frozen flat feature structures

    Status: retained on perf/hc-optimization-archive, not merged (FeatureStruct.ValueEquals).
    Portable: yes; the proof depends on immutability and tree shape, not the CLR.

    Invariant

    During Freeze, a feature structure is marked as a flat tree only when every reachable value is a
    SimpleFeatureValue, no value instance repeats, and no nested FeatureStruct is present. Once frozen, that
    fact cannot change. Comparing two such structures can never trigger the general algorithm's reentrancy or
    cycle guard, so a feature-count/key/value comparison is equivalent to the general visited-pair walk.

    Implementation

    ValueEquals uses the direct leaf comparison only when both operands are frozen and carry the tree proof.
    Every other shape—including shared leaves, nested structures, and cycles—uses the general algorithm. A
    test-only slow-path entry point lets randomized tests compare both implementations on the same inputs.

    Evidence

    The optimization removes all visited-set work from the dominant frozen, flat case. It was observed in the
    same profile that motivated traversal-state reuse, but was not isolated from the other feature-structure
    changes; no standalone wall-time ratio is claimed.

    Correctness coverage and limits

    FeatureStructTreeFastPathTests cross-checks randomized flat structures against the slow path and exercises
    false comparisons and structures that must not qualify. FeatureStructValueEqualsFreezeTests covers shared
    and cyclic graphs. The full conformance-inclusive run passed 603 tests with one expected host 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