You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[complexity, flattening, checker] O(n^2) — Checking a call copies its growing argument list each time #1956
type Unit is Data:
U{}
def discard(x1: Unit, x2: Unit) -> Unit:
U{}
def example() -> Unit:
discard(U{}, U{})
type Unit is Data:
U{}
def discard(x1: Unit, x2: Unit, x3: Unit) -> Unit:
U{}
def example() -> Unit:
discard(U{}, U{}, U{})
discard ignores its arguments and returns U{}. Let n be the number of arguments
passed to discard: 2 and 3 above. Checking example copies 0 + 1 + ... + (n-1) = n(n-1)/2 existing argument-list entries:
1 and 3, respectively. Checking discard is separate.
Cause. Bend represents a call as nested applications. On each step towards the
called function, term_infer builds [tm.x, ...sp], where sp holds pending
arguments. Adding one argument copies every argument already collected.
This quadratic list work may not be necessary. A linked list could add each
argument with one node pointing to the existing list. Building it and converting
it to an array once would take O(n) work.
The same copying occurs when body_flatten loads ordinary parameters with repeated vars.slice(1) calls: n parameters copy n(n-1)/2 entries to emit n lambdas.
Keeping the original array and advancing an index could make this list work O(n).
Constructor matching also copies growing field arrays for the recursion check and
the branch's type. With n fields, each loop copies n(n-1)/2 entries. Each generated
function could keep a linked prefix and build the ordered array once all fields
are present, giving O(n) list work including conversion.
To reproduce on main 7a7e6505a, save either program as example.bend;
run bun bend2/main.ts example.bend --check-only.
Adding one pending argument need not copy the arguments already collected, regardless of what the called function does. The saving therefore does not depend on discard ignoring its arguments.
Constructor-field functions can be opened more than once. Their previously collected fields must remain unchanged: one mutable array shared between openings would violate that. A linked prefix could preserve those earlier values and produce an ordered array when complete. Building that final array must be included in the O(n) list work.
Activity
pjcavalcanti commentedon Nov 2, 2026
Addendum — 2 November 2026
Adding one pending argument need not copy the arguments already collected, regardless of what the called function does. The saving therefore does not depend on
discardignoring its arguments.Constructor-field functions can be opened more than once. Their previously collected fields must remain unchanged: one mutable array shared between openings would violate that. A linked prefix could preserve those earlier values and produce an ordered array when complete. Building that final array must be included in the O(n) list work.
fix(check): keep only the constructor-field lists
fix(check): a constructor's fields grow as a list, without copying