Skip to content

O07: two arrays and a hash - #137

Merged
tamnd merged 1 commit into
mainfrom
o07-the-dict
Aug 31, 2026
Merged

O07: two arrays and a hash#137
tamnd merged 1 commit into
mainfrom
o07-the-dict

Conversation

@tamnd

@tamnd tamnd commented Aug 31, 2026

Copy link
Copy Markdown
Owner

O07, the seventh lesson of the object model part. Six lessons have said "look in the instance dict" or "walk the class dicts" and moved straight on. This one opens the dict.

The frame is that a dict is two arrays, not one. A small array of slot numbers with all the holes in it, and a plain append only array of entries with none. Insertion order, the cost of deleting, the resize points and the size difference between a string keyed dict and an int keyed one all fall out of that split.

The centrepiece is a compact dict written out in Python, about sixty lines, including the real probe recurrence and the real resize rule. It grows at 6, 11, 22, 43, 86 and 171 keys, which is exactly where the real one grows, and it iterates identically over two hundred keys and after deletes.

What it covers:

  • dk_indices and dk_entries, the four slot states, and the one byte per slot a small dict spends
  • the -1 that ends a failed lookup after a single read
  • i = mask & (i * 5 + perturb + 1), why it is not linear probing, and the 0, 1, 6, 7, 4, 5, 2, 3 order it produces for a size 8 table
  • USABLE_FRACTION and GROWTH_RATE, and why the growth factor is three rather than two
  • the empty dict sharing one immortal keys object, which is the single row where the model and the real dict disagree
  • DKIX_DUMMY, why a deleted slot cannot go back to -1, and a churn loop that makes a five key dict grow twice
  • popitem as the one delete that gives the space back
  • the two entry layouts and the word per row a string keyed dict saves, plus the conversion when one non string key arrives
  • estimate_log2_keysize, the reverse of USABLE_FRACTION, and what dict.fromkeys does with it

Sixteen citations, all resolving against the pin. Six diagrams, all under 940 px. Two new glossary terms, compact dict and probe sequence, bringing GLOSSARY.md to 120.

Three cells carry a varies= note, because they print byte counts and a browser build has 4 byte pointers. Everything else is identical on 3.14, on 3.15.0rc1 and on Pyodide 3.14.2.

Checked with the full just check and with just versions.

@tamnd tamnd added kind/lesson A chapter: prose, notebook, experiments, boss fight area/objects The object model, types, slots and the builtin types labels Aug 31, 2026
@tamnd tamnd mentioned this pull request Aug 31, 2026
6 tasks
@tamnd
tamnd merged commit 4651b1c into main Aug 31, 2026
16 checks passed
@tamnd
tamnd deleted the o07-the-dict branch August 31, 2026 17:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/objects The object model, types, slots and the builtin types kind/lesson A chapter: prose, notebook, experiments, boss fight

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant