loop-linear-membership (OPT110)¶
Family: Complexity Rewrites (OPT) · Exact · Opt-in · Severity warning (slower code) · Proven-safe suggestion (--fix --include-size-dependent)
Opt-in: size-dependent e in lst inside a loop -> hoist set outside loop.
What it does¶
Checks membership tests against a list or tuple inside a loop, e in lst, where the list does
not change during the loop, and suggests building a set once before the loop.
This is a size-dependent rewrite, and the rule is opt-in: it runs when extend-select names it
or with heki check --include-size-dependent, which also applies its proven rewrites with
--fix. Not reported:
- small containers: at most 16 items that nothing grows, whatever the items are
(
x in ("a", "b"),x in [b"1", b"2"], also through a name bound to one, a comprehension over one, orlist(range(5))); - lists whose elements are dicts, lists, or sets: no
setcan hold them.
What a report states¶
A report states that the membership test e in lst, in a loop, scans the list (or tuple) each
time it runs.
Its premises:
- Builtin type by construction: the value is a builtin
listortupleby 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. Whether a set can replace the list
is not part of the claim either: elements that cannot be hashed, or a list that changes during the
loop, block the rewrite (the help names the obligation); the scan happens either way.
Why is this bad?¶
e in lst scans the list, so the loop costs the product of both sizes. A set lookup is
constant time once the set exists.
Example¶
def allowed(items: list[int]) -> list[int]:
ok = list(range(0, 1000, 3))
return [i for i in items if i in ok]
Use instead:
def allowed(items: list[int]) -> list[int]:
ok = set(range(0, 1000, 3))
return [i for i in items if i in ok]
Fix safety¶
Proven safe only when:
- the container is an exact
list/tuplebound outside the loop and not changed inside it (a list mutated in the loop would make the hoisted set stale); - every element is an
int,str,bytes,bool, orfloat: building the set cannot raise even when the loop runs zero times, and hashing agrees with==; - the probe value is of those types too (an object with a custom
__eq__could match list elements a set lookup never compares with).
A list[int] annotation is never trusted: a list subclass can override __contains__.
Not reported when the container is small whatever the input, or when the loop runs a fixed
small number of times (for i in range(3):): building the set costs about as much as the scans
it saves.