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:
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¶
-
Where. A
forloop over the no-argumentsplit()of astr(ADR 0043's walk) whose body uses the loop variablewonly as a key of a localdict[str, V]dthat the body does not rebind, in these positions:d[w],d[w] = eas the only target of an assignment,d[w] op= e,d.get(w, e)with two arguments,w in dandw not in d.wis not bound anywhere in the body, and the loop:- is in a function: at the script's top level,
wis a global that called functions can read; - is not in a
trybody, handler orfinallyof the function, nor in awith(a file'swithis atry/finally), so an exception raised in the body leaves the function, andwwith it; - is not in a generator or an inlined body.
Any other loop over
s.split()walks the words as ADR 0043 says. - is in a function: at the script's top level,
-
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. Ad[w] = ethat inserts takes the fresh Str as the store's key; ad[w]that raisesKeyErrortakes the word's repr as the message, as before. -
The binding.
wis bound to the word when control leaves the loop: when the words run out (before theelseclause), and at abreak. A loop that runs no iteration leaveswas it was. Since the body readswonly as a key and an exception from the body leaves the function, no code readswwhile it is stale: this is the binding CPython makes, observed wherever anything can observe it. -
A backstop. While the body is lowered,
wnames a value no instruction defines. A read ofwby 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.