Skip to content

HermitCrab engine: share frozen syntactic feature structures on internal Word clones (about 15% of a heavy Sena profile, never isolated) #595

Open

Description

@johnml1135

Internal clones of a frozen Word deep-copy its SyntacticFeatureStruct, then freeze and hash it again when building analysis keys. The change adds Word.CloneForEngine, which shares the frozen structure until a rule must mutate it. The three in-place analysis mutation sites call EnsureOwnSyntacticFeatureStruct first. A per-Morpher setting (HC_SHARE_SYNTACTIC_FS=false opts out) controls sharing. Public Word.Clone() stays a deep, mutable clone. This is sharing, not a cache. CloneForEngine and EnsureOwnSyntacticFeatureStruct are on the archived branch only.

Measured. The heaviest Sena profile attributed roughly 15% of CPU to freezing and hashing a newly cloned syntactic feature structure while building analysis keys. The five-language measurement is branch-wide, so no standalone speedup is assigned. The wall-clock figure is a profile share and is not attributed.

What a revival must do first. Time it with the toggle alone, in clones and freezes avoided. Do not use the old morph-count signature for parity. Sharing is unavailable for mutable source words.

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/syntactic-feature-structure-sharing.md), which is being deleted:

    Copy-on-write syntactic feature structures in engine clones

    Status: retained on perf/hc-optimization-archive, not merged (Word.CloneForEngine,
    EnsureOwnSyntacticFeatureStruct). Portable: partly; immutable sharing is general, while the ownership
    plumbing is specific to this engine.

    Invariant

    HermitCrab freezes a Word before it becomes an input to the analysis or synthesis graph. Internal clones may
    therefore share that frozen word's syntactic feature structure until a rule needs to mutate it. The public
    Word.Clone() contract remains a deep, mutable clone and never exposes copy-on-write sharing to callers.

    Implementation

    Internal engine sites use CloneForEngine(). A per-Morpher setting is copied onto new words and controls
    whether those clones share a frozen syntactic feature structure. The three in-place analysis mutation sites
    call EnsureOwnSyntacticFeatureStruct() before modification; assignments of a fresh structure need no guard.
    The opt-out is internal and captured per Morpher, preventing one parser or test from changing another
    existing parser's behavior.

    Evidence

    The heaviest Sena profile attributed roughly 15% of CPU to freezing and hashing a newly cloned syntactic
    feature structure while constructing analysis keys. Sharing removes that repeated deep clone/freeze on the
    engine path. The final five-language measurement is branch-wide rather than an isolated toggle result, so no
    standalone speedup is assigned here.

    Correctness coverage and limits

    WordSyntacticFsSharingTests covers copy-on-write, nested/shared structures, public-clone mutability, and
    isolation between morphers. The broader HermitCrab suite exercises integration behavior and passed both with
    sharing enabled and with HC_SHARE_SYNTACTIC_FS=false; the full conformance-inclusive run passed 603 tests
    with one expected host skip. Sharing is intentionally unavailable for mutable source words.

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