Skip to content

perf: O(N²) → O(N) upvalue collection in closure handler#154

Merged
davydog187 merged 1 commit intomainfrom
perf/on-upvalue-collection
Feb 27, 2026
Merged

perf: O(N²) → O(N) upvalue collection in closure handler#154
davydog187 merged 1 commit intomainfrom
perf/on-upvalue-collection

Conversation

@davydog187
Copy link
Contributor

Summary

Replace list-append accumulation (cells ++ [ref]) with list-prepend ([ref | cells]) followed by a single Enum.reverse/1 in the :closure instruction handler.

Each ++ was O(N) in the current accumulated list length, making the full upvalue collection O(N²) for N upvalue descriptors. Prepend is O(1), so the collection is now O(N).

In practice closures rarely have more than a handful of upvalues, but this removes a pathological cost for any closure that captures many variables and is created repeatedly (e.g. inside a tight loop).

Test plan

  • All 1273 existing tests pass with 0 failures

🤖 Generated with Claude Code

Replace list-append accumulation (`cells ++ [ref]`) with
list-prepend (`[ref | cells]`) followed by a single `Enum.reverse/1`
at the end of the reduce.

Each `++` was O(N) in the current list length, making the full
collection O(N²) for N upvalue descriptors. Prepend is O(1), so
the full collection is now O(N).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@davydog187 davydog187 merged commit 76656be into main Feb 27, 2026
2 checks passed
@davydog187 davydog187 deleted the perf/on-upvalue-collection branch February 27, 2026 01:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant