Skip to content

refactor: Arc<FunctionDef> for closure capture (eliminate quadratic cloning) #2

Description

@dean0x

Problem

Every @define deep-clones the full scope chain when capturing variables into closures. This creates quadratic behavior when nesting definitions, as each nested definition clones an increasingly large scope.

Scope

This is a refactoring of the ownership model across multiple modules:

  • src/scope.rs — Wrap FunctionDef in Arc for shared ownership
  • src/resolver.rs — Update closure capture patterns
  • src/evaluator.rs — Adjust function invocation logic

Related Tech Debt

  • ARCH-4: CapturedScope struct extraction (subsumed by this)
  • PERF-4: Arc for cache hits (related ownership model change)

Benefits

  • Eliminates quadratic closure capture cloning
  • Enables shared ownership of resolver cache via Arc
  • Reduces memory pressure in deeply nested @define chains

Acceptance Criteria

  • FunctionDef wrapped in Arc across scope.rs, resolver.rs, evaluator.rs
  • CapturedScope struct extracted from scope management
  • Cache hits in resolver use Arc
  • All existing tests pass
  • Benchmark shows reduced allocation in nested @define scenarios

Co-Authored-By: Claude noreply@anthropic.com

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions