Skip to content

[MED] Graph#== and #hash materialize full edge Set even for frozen graphs #109

@duncanita

Description

@duncanita

Severity: MED — O(E) per equality / hash on Graph.

Where:

  • lib/dag/graph.rb:425-433 (== and hash both call edges)
  • lib/dag/graph.rb:239-242 (edges returns @cached_edges for frozen but still returns the full Set)

What:
== does @nodes == other.nodes && edges == other.edges. For frozen graphs edges returns the cached Set, but ==/hash still walks/materializes Edge objects. For unfrozen graphs compute_edges allocates a fresh Set of fresh Edges per call.

Definition's Data.define derived ==/hash recurses into Graph and Registry — anywhere a Definition is keyed in a Hash or compared, this fires.

Suggested fix:
Short-circuit == with node_count != other.node_count || edge_count != other.edge_count then compare @adjacency Sets directly (Set values are frozen on freeze). hash can be [@nodes, @adjacency, @edge_metadata].hash for frozen graphs.


Filed from a multi-agent review (DRY / efficiency / bug). Behavior change is out of scope; suggested fixes are refactors only.

Metadata

Metadata

Assignees

No one assigned

    Labels

    performancePerformance / efficiency issue (no behavior change)

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions