Skip to content

loop-linear-dedup (OPT111)

Family: Complexity Rewrites (OPT) · Exact · Opt-in · Severity warning (slower code) · Proven-safe suggestion (--fix --include-size-dependent)

Opt-in: size-dependent if v not in seen: seen.append(v) -> auxiliary set.

What it does

Checks the de-duplication idiom if v not in seen: seen.append(v) inside a loop and suggests keeping an auxiliary set next to the list.

Opt-in and size-dependent: it runs when extend-select names it or with --include-size-dependent, which also applies its proven rewrites with --fix.

What a report states

A report states that if v not in seen: seen.append(v), in a loop, scans the list each time it runs.

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.
  • 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. A loop or a list 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?

The membership test scans the list, so de-duplicating n items costs n² comparisons.

Example

def unique(values: list[int]) -> list[int]:
    seen = []
    for v in values:
        if v not in seen:
            seen.append(v)
    return seen

Use instead:

def unique(values: list[int]) -> list[int]:
    seen = []
    seen_set = set()
    for v in values:
        if v not in seen_set:
            seen.append(v)
            seen_set.add(v)
    return seen

Fix safety

Proven safe only when the list is an exact builtin list owned by the function, the paired append is its only change inside the loop (a removal would make the set stale), no other live reference could change it, and every element and probe is a hashable builtin scalar.