Skip to content

[MED] Graph#walk uses to_a on Sets to build stack (avoidable allocs) #110

@duncanita

Description

@duncanita

Severity: MED — small allocations in reachable? (which is on the add_edge hot path).

Where:

  • lib/dag/graph.rb:614-628

What:
stack = fetch_set(adj, start).to_a allocates an Array; stack.concat(fetch_set(adj, current).to_a) allocates another per loop iteration.

walk is the body of reachable? / ancestors / descendants. reachable? is the cycle-detection walk on the add_edge path.

Suggested fix:
Iterate the Set and push: fetch_set(adj, start).each { |x| stack << x } and the same in the loop. Combined with the Loader fix (use insert_edge), shaves real loader latency.


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