Skip to content

immutable-quadratic-concat (OPT103)

Family: Complexity Rewrites (OPT) · Exact · Opt-in · Severity warning (slower code) · No automatic fix

Opt-in: size-dependent immutable quadratic accumulation (bytes/tuple/attr str).

What it does

Checks quadratic accumulation of immutable values in a loop:

  • acc = acc + chunk / acc += chunk where acc is a bytes or tuple;
  • self.text += piece on an attribute that the class's methods in the module (or, for another receiver, the function) only ever assign a string, bytes, or tuple literal of the pieces' kind (self.text = ""): self.values += (i,) extends a list in place.

Local str accumulation is not reported: CPython already appends in place when the string has no other reference. Neither are:

  • an accumulator the loop's own test caps at a small constant (while len(buf) < 10, also as one and operand or with a module constant: at most 4096), whatever the input;
  • obj.attr += x where obj is a different object in every iteration (the loop element, or an object created in the iteration): each attribute gets one piece.

Opt-in and size-dependent: it runs when extend-select names it. It has no automatic rewrite.

What a report states

A report states that concatenating to an immutable value (bytes, a tuple, a str attribute), in a loop, copies all of it each time it runs.

Its premises:

  • Builtin type by construction: the value's exact type is known from its construction. Established.
  • 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 value is. The message claims no size and no cost estimate. A loop or a value that the code fixes at 16 or fewer items is not reported, since the rewrite would not pay off. The exact tier also asks that the data decide how many times the loop runs: the loop steps through a parameter (*args included) or a part of one, the rows of a query, the lines of a file, or the entries of a directory, as EFF103's Origin establishes them, and the code does not fix or cap their number. A proven report over any other loop is heuristic, for example over a loop whose trip count the code fixes at more than 16 or caps with a length test every path to the loop passes, over the program's structure (an attribute of self, a project function's result, range of a configured number, a class's MRO, a module-level list), or in a while loop. Such a report is true, but its loop usually runs a few times, however large the input.

Why is this bad?

Each concatenation copies everything accumulated so far.

Example

def encode(parts: list[bytes]) -> bytes:
    out = b""
    for p in parts:
        out = out + p
    return out

Use instead:

def encode(parts: list[bytes]) -> bytes:
    return b"".join(parts)

Fix safety

No automatic fix (the rewrite needs a buffer before the loop and a join after it). The diagnostic still lists the proof obligations: the accumulator must not be read inside the loop or by an exception handler, and its intermediate values must not escape.