Skip to content

ADR 0069: A split word used only as a dict key is looked up by its span

Status: Final Type: Feature Created: 2026-11-05 Amends: 0043 (the walk over s.split() made a Str per word) Related-ADRs: 0067 (the measurement that asks for it), 0030 (the table the lookup probes), 0033 (what a read of an unbound name is), 0007 (where a raise goes)

Context

for w in s.split(): walks the words of s without the list (0043), but it still makes each word, a fresh Str per iteration. In the word count, the idiom of benchmarks/lowlevel/wordfreq.py, that string is used once, as the key of a lookup that almost always finds it already there:

for w in text.split():
    counts[w] = counts.get(w, 0) + 1

The C version looks the word up where it lies in the text and copies it only for a new key. A hand edit of postpile's generated C doing the same took wordfreq from 2.36× of C to about 1.35× (notes/measurements.md, 2026-11-05). The allocation is the whole remaining distance.

Python binds w on every iteration. If the word is not made, w holds the previous value (or nothing) while the body runs. Any code that can read w would see that stale value: the body, the code after the loop, a handler that catches an exception raised in the body, a function called from the body that reads w as a global.

Decision

  1. Where. A for loop over the no-argument split() of a str (ADR 0043's walk) whose body uses the loop variable w only as a key of a local dict[str, V] d that the body does not rebind, in these positions: d[w], d[w] = e as the only target of an assignment, d[w] op= e, d.get(w, e) with two arguments, w in d and w not in d. w is not bound anywhere in the body, and the loop:

    • is in a function: at the script's top level, w is a global that called functions can read;
    • is not in a try body, handler or finally of the function, nor in a with (a file's with is a try/finally), so an exception raised in the body leaves the function, and w with it;
    • is not in a generator or an inlined body.

    Any other loop over s.split() walks the words as ADR 0043 says.

  2. The lookup. Each such key position finds the entry by the word's span of the string, __pp_dict_find_span(d, s, start, end): the same hash over the same bytes, and a key equal to the span's bytes. Two positions make the word at that point. A d[w] = e that inserts takes the fresh Str as the store's key; a d[w] that raises KeyError takes the word's repr as the message, as before.

  3. The binding. w is bound to the word when control leaves the loop: when the words run out (before the else clause), and at a break. A loop that runs no iteration leaves w as it was. Since the body reads w only as a key and an exception from the body leaves the function, no code reads w while it is stale: this is the binding CPython makes, observed wherever anything can observe it.

  4. A backstop. While the body is lowered, w names a value no instruction defines. A read of w by any lowering other than the key positions above uses that value. The IR verifier rejects the module for it (IR05), so the stale slot is never read.

Consequences

The word count allocates once per distinct word, plus once at the loop's end; before, it allocated once per word. wordfreq, measured interleaved (notes/measurements.md, 2026-11-05): c 2.44× → 1.48× of C, llvm 2.73× → 1.72×, qbe 2.76× → 1.73×. No program of ADR 0067's corpus is now more than 2× off C on the c backend. Of the corpora, only wordfreq has a loop over split().

The positions are syntactic, so k = w; counts[k] += 1 makes the words as before, and so does any use of w other than as a key (print(w), len(w), w.lower()). A word count that lower-cases the words is the next case, and needs a lower-cased span, which this does not provide.

The KeyError of d[w] inside the body can be caught by a try inside the body; the handler reads w only as a key, under the same rule, so it is lowered by the same span.

Rejected: binding w lazily wherever it is read (a flag per iteration, tested at each read). It would cover any body, but the frontend reads a local's slot from many places that do not go through one name lookup; a missed one would read the stale word without a diagnostic. The syntactic positions cover the idiom with a check that fails at compile time.