Skip to content

list-quadratic-concat (OPT101)

Family: Complexity Rewrites (OPT) · Exact · Enabled by default · Severity warning (slower code) · Safe fix (--fix)

Unconditionally profitable list quadratic concatenation (x = x + [e] -> x.append(e)).

What it does

Checks loops that grow a list by building a new one every iteration, x = x + [e], and rewrites it to x.append(e) (x.extend([...]) for several elements).

What a report states

A report states that x = x + [e], in a loop, builds a new list from the whole accumulated list each time it runs, where appending would change it in place.

Its premises:

  • Builtin type by construction: the value is a builtin list by its construction (a display, a comprehension, list(...)). Established: the Soundness lane takes types from constructions only, never from annotations.
  • Loop-carried: the result is the value the next iteration starts from. Established by the IR.
  • In a loop: the statement is in the body of a loop. Established by the IR.
  • Rewrite proven safe: the help says [safe fix: ...], [proven-safe suggestion: ...], or [proven safe; ...]. A report whose help names a blocking obligation instead ([not auto-fixable: ...]) lacks this premise and is heuristic whatever the rule's certainty; the proven reports are the rule's exact tier.

Not part of the claim: how many times the statement runs (one under a branch inside the loop counts), and how large the loop or the list is. The message claims no size and no cost estimate. The rewrite never makes the code slower, so a loop the code fixes at a few items is reported too.

Why is this bad?

x + [e] copies the whole list, so the loop is quadratic. append is amortized constant time and is never slower, even for one iteration.

Example

def squares(n: int) -> list[int]:
    out = []
    for i in range(n):
        out = out + [i * i]
    return out

Use instead:

def squares(n: int) -> list[int]:
    out = []
    for i in range(n):
        out.append(i * i)
    return out

Fix safety

The fix is applied by heki check --fix only when heki proves it safe:

  • the list is an exact builtin list created in the function, not shared (not a parameter, not stored in another object, not captured by a closure, not passed to unknown code);
  • no other reference to the old list is live after the statement: no alias, no for loop iterating it, no iter(x) or view;
  • evaluating the right-hand side does not rebind or mutate the list;
  • no exception handler can observe an older binding of another name aliasing it.

Otherwise the diagnostic explains which proof obligation blocks the fix, and nothing is rewritten. A disproved candidate is not reported at all: for v in x: x = x + [v] terminates, but x.append(v) would loop forever, so heki says nothing there and never touches it. Type annotations are never trusted.

The fix rewrites a plain x = x + [...] statement. For other shapes of the same update (a parenthesized x = (x + [i]) or x = (x) + [i], an annotated x: list = x + [i]), the diagnostic says that the rewrite is proven safe and leaves it to be made by hand.