Chapter 2 of 1125 min read
Objects in memory
Every value is a pointer to a small block with a reference count and a type tag. The layout, the ownership rules, and the allocator that hands the blocks out.
An aython program never handles a bare number. Every value, even 7 or True, is a pointer to a block of memory that starts with a reference count and a type tag, exactly the shape CPython gives its objects. This chapter lays those blocks out byte by byte, shows where they come from and how they are recycled, and states the one ownership rule that every runtime helper and every line of generated code obeys. The rest of the report assumes all of it.
The model is a decision recorded in the review table of docs/core-design.md: “Boxed PyObject + refcount (not fixnums), to learn the real Python model”. The rejected alternative, common in fast language implementations, stores small integers directly inside the pointer word and tells them apart by a spare bit. It is faster and it hides the very thing this project exists to show: what an object is in memory, and who is responsible for freeing it.
The header #
Every object starts with the same two 8-byte words:
| Offset | Field | Meaning |
|---|---|---|
| +0 | refcnt |
How many references to this object exist: slots in frames, elements of containers, temporaries. At zero the object is freed. A huge sentinel value marks an object that is never counted |
| +8 | type |
The address of the object’s type tag in .data; it is what every type test compares |
| +16 onwards | the payload | The value itself, or a length and a pointer to a separate buffer |
Because the header is uniform, the helpers that only need the header work on any object: aython_incref and aython_decref touch +0, and every type test loads +8. The payload layouts are the ones the comment at the top of runtime.s draws (“draw this on paper”), and sets, which that comment leaves out, are drawn in docs/core-design.md. This table is read from those two comments at build time:
| Type | Bytes | +16 | +24 | +32 | +40 | +48 |
|---|---|---|---|---|---|---|
int |
24 | value |
– | – | – | – |
list |
40 | len |
cap |
items |
– | – |
tuple |
40 | len |
cap |
items |
– | – |
str |
32 | len |
data |
– | – | – |
dict |
48 | len |
cap |
indices |
entries |
– |
range |
48 | len |
start |
step |
stop |
– |
float |
24 | payload |
– | – | – | – |
set |
56 | used |
fill |
mask |
table |
finger |
Three families are visible in it:
- The value is in the header. An
intholds a signed 64-bit value at+16; afloatholds the 64 bits of an IEEE-754 double there.None,TrueandFalseuse the int shape too, with payload 0, 1 and 0, which is why arithmetic accepts a bool wherever it accepts an int: the arithmetic helpers let both tags through (check_intlike_rbx) and read+16either way. - The header points to a buffer. A
strhas its length at+16and a pointer to its bytes at+24. Alistortuplehas length, capacity and a pointer to an array of object pointers. Adicthas two buffers, an index table and a dense array of entries. Asethas one table of hash-and-key pairs. The buffer lives elsewhere in the arena, so a container’s header never moves when it grows; only its buffer pointers change (+32for a list,+32and+40for a dict,+40for a set). - The payload is plain numbers. A
rangestores its length, start, step and stop as raw 64-bit integers, not as pointers to int objects, so a range costs one 48-byte header however long it is; only the values a loop actually takes out of it are boxed.
Every sized type keeps its length at +16, so len() is one load whatever the container is, once the type test has passed:
runtime.s · obj_lenlines 3606–3649 lea rcx, [rip + type_range] # rcx holds address of range type marker
cmp rax, rcx # compare object type against range
jne abort_type # ints/bools/None have no length, like CPython
obj_len_have: # rbx points at a sized object
mov rdi, [rbx + 16] # rdi holds len field at offset 16
call int_new # box len into a new owned int in rax
pop r15 # restore r15
Drawing it #
The figure compiles v = <value> with the real frontend and code generator, replays the emitted code against the runtime’s allocation rules, and draws every block v leads to. Arena addresses are the ones a fresh arena gives, because nothing else has been allocated yet.
.data. Try dict to see the index buckets and the dense entry array, and str to see a 32-byte header that starts at an address that is not a multiple of 8.Type tags #
The type field is not a number aython picks per type. It is the address of a label in .data, one per type, each holding an 8-byte constant:
runtime.s · type_intlines 62–64.globl type_int # make type_int visible to linker
type_int: # label for int type tag
.quad 0x00696e74 # type tag bytes spelling int
Only the address matters. Every type test in the runtime and in generated code loads the tag’s address with lea and compares pointers, and nothing ever reads the eight bytes at the address. Here is the start of obj_len’s dispatch:
runtime.s · obj_lenlines 3606–3649 mov rax, [rbx + 8] # rax holds type pointer at offset 8
lea rcx, [rip + type_list] # rcx holds address of list type marker
cmp rax, rcx # compare object type against list
je obj_len_have # lists have their length at offset 16
lea rcx, [rip + type_str] # rcx holds address of str type marker
cmp rax, rcx # compare object type against str
The constants are there for a person reading a memory dump: each spells its type’s name. They also show little-endian byte order at work. The assembler stores the least significant byte first, so a literal that reads int in hexadecimal appears backwards in memory, and two tags were written the other way round, so that memory spells the name:
| Tag | .quad in runtime.s |
The literal, read as text | The 8 bytes in memory, lowest address first |
|---|---|---|---|
type_none |
0x6e6f6e65 |
none |
enon···· (65 6e 6f 6e 00 00 00 00) |
type_bool |
0x626f6f6c |
bool |
loob···· (6c 6f 6f 62 00 00 00 00) |
type_int |
0x00696e74 |
int |
tni····· (74 6e 69 00 00 00 00 00) |
type_list |
0x6c697374 |
list |
tsil···· (74 73 69 6c 00 00 00 00) |
type_tuple |
0x7475706c65 |
tuple |
elput··· (65 6c 70 75 74 00 00 00) |
type_str |
0x73747200 |
str· |
·rts···· (00 72 74 73 00 00 00 00) |
type_dict |
0x64696374 |
dict |
tcid···· (74 63 69 64 00 00 00 00) |
type_range |
0x72616e6765 |
range |
egnar··· (65 67 6e 61 72 00 00 00) |
type_float |
0x74616f6c66 |
taolf |
float··· (66 6c 6f 61 74 00 00 00) |
type_set |
0x00746573 |
tes |
set····· (73 65 74 00 00 00 00 00) |
(A middle dot is a zero byte.) Since the content is never read, the inconsistency is harmless, and the tags could hold anything; what they must not do is share an address. Even set_dummy, the tombstone a set leaves in a deleted slot, borrows type_none as its tag only because something must be there: sets recognise it by its own address.
Immortal objects #
Some objects are referenced from everywhere and must never be freed: None, True, False, the empty tuple, and the small integers. They are written into .data by the assembler, fully formed, with a reference count no real count can reach:
runtime.s · aython_nonelines 91–96.p2align 3 # align next object to 8 bytes
.globl aython_none # make aython_none visible to linker
aython_none: # address of the None singleton object
.quad SENTINEL # refcnt sentinel so it is never freed
.quad type_none # type pointer for None object
.quad 0 # payload slot unused for None
The sentinel is 0x4000000000000000, 262. aython_incref and aython_decref begin by comparing the count with it, unsigned, and return at once if it is at least that large, so an immortal object’s count never changes and its header is never written:
runtime.s · aython_increflines 592–606# ---- aython_incref ----------------------------------------------------
# Input: rdi = object. Output: rax = same object, owned once more.
# No-op on singletons (refcnt sentinel). Never fails.
.p2align 4 # align function to 16 bytes
.globl aython_incref # make aython_incref visible to linker
.type aython_incref, @function # declare aython_incref as function
aython_incref: # entry, rdi is borrowed object
mov rax, [rdi] # rax gets refcnt at offset 0
movabs rcx, SENTINEL # rcx gets sentinel 2 to 62
cmp rax, rcx # compare refcnt with sentinel
jae incref_single # skip inc if immortal singleton
inc qword ptr [rdi] # add one shared owner to refcnt
incref_single: # shared path done
mov rax, rdi # rax returns same object pointer
ret # return to caller
The same test opens aython_decref, followed by the ordinary path: subtract one, and stop unless the count reached zero.
runtime.s · aython_decreflines 608–751 movabs rcx, SENTINEL # rcx gets sentinel 2 to 62
...
jae decref_ret # return at once if immortal
dec qword ptr [rdi] # drop one owner from refcnt
jnz decref_ret # return if owners still remain
The small-int cache applies the same trick to integers. small_ints is a table of 262 ready-made int objects for the values -5 to 256, 6,288 bytes in all, generated by a .rept loop in the assembler. int_new checks the range first and returns the cached object’s address without touching the allocator:
runtime.s · int_newlines 753–789 cmp rdi, -5 # below the cached range?
...
cmp rdi, 256 # above the cached range?
...
lea rax, [rdi + 5] # rax = cache index 0..261 for value -5..256
imul rax, rax, 24 # rax = byte offset into the table (24 bytes each)
lea rcx, [rip + small_ints] # rcx = cache base (rip+index is illegal)
add rax, rcx # rax = cached immortal int, no alloc and no free ever
The range is CPython’s own small-int cache. It is the reason loop counters, list lengths and most arithmetic results in ordinary programs never allocate, and it keeps identity in agreement with CPython where the corpus tests it: print(1 is 1) prints True on both sides, because both hand out the same cached object. (The comment above small_ints says identity is unobservable because the subset has no is. That comment is out of date: is is in the subset, and the cache is what makes it agree with CPython here.)
The ownership rule #
Reference counting only works if everyone agrees on who increments and who decrements. runtime.s states the rule in its header:
Ownership rule the generated code relies on: every helper BORROWS its inputs (does not consume them) and returns a NEW owned reference (or nothing for void helpers). The caller shows every transfer with an explicit incref/decref. Helpers that store into a list incref what they keep; helpers that overwrite or release call decref.
In practice: a helper never takes a reference away from its caller, and anything it returns is the caller’s to release. The generated code then makes every remaining transfer visible as an explicit call. That is principle 4 of the design, “No magic”: “every transfer of ownership shows up explicitly as an increment or decrement of a reference count in the generated code. If you can’t point at it, it shouldn’t exist.”
| Situation | What the generated code does | Where to see it |
|---|---|---|
| A helper returns a value | The code owns it, count 1 for a new object | call int_new # rax = new owned int box (refcnt 1) |
| A variable’s value is used | aython_incref first: the use gets its own reference, the slot keeps its own |
b = a below |
| A name is assigned | Park the new value in a temporary slot, aython_decref the old one, then store |
Every assignment |
| A temporary has been used | aython_decref it as soon as the helper that borrowed it returns |
Operands of int_add |
| A container keeps a value | The helper increfs what it stores; the code then releases its own temporary | list_append, obj_setitem, set_add |
| A user function is called | Arguments’ references travel with the call; the callee adopts them without counting | The machine chapter |
| A function returns a local | The slot’s reference is moved, not copied; the slot is set to None |
return n in fib |
| A function ends | The epilogue decrefs every slot | # epilogue: release every local |
Here is b = a from tests/mvp/pass_alias.py, where a already holds a list:
tests/mvp/pass_alias.py · emitted · main_mainmov rdi, [rbp-56] # release our reference to the element
call aython_decref # (the list keeps its own incref from the append)
mov rax, [rbp-48] # rax = the finished owned list
mov [rbp-40], rax # park the new owned value where calls cannot clobber it
mov rdi, [rbp-8] # rdi = old a (borrowed for the call)
call aython_decref # release old a; may free it right here
mov rax, [rbp-40] # rax = new value again (survived the call in memory)
mov [rbp-8], rax # a now owns the new value
# line 3: b = <expr> (release old, compute new, store)
mov rax, [rbp-8] # rax = a's object (still owned by the slot)
mov rdi, rax # arg = same pointer (helper borrows it)
call aython_incref # take our own shared ownership; rax = same pointer
mov [rbp-40], rax # park the new owned value where calls cannot clobber it
mov rdi, [rbp-16] # rdi = old b (borrowed for the call)
call aython_decref # release old b; may free it right here
mov rax, [rbp-40] # rax = new value again (survived the call in memory)
mov [rbp-16], rax # b now owns the new value
The second half of that listing is the assignment pattern the whole code generator uses. The new value is computed first, because the expression may read the old one (x = x + 1). It is parked in memory, because the call aython_decref that follows may change rax. Only then is the old value released, which may free it on the spot, and the slot takes the new pointer.
flowchart TB A["slot a<br/>owns 1 reference"] -->|"mov rax, [rbp-8]"| R[rax: same pointer] R -->|"call aython_incref<br/>count + 1"| T["temporary [rbp-40]<br/>owns 1 reference"] OLD["old value of b"] -->|"call aython_decref<br/>count − 1, maybe freed"| GONE[released] T -->|"mov [rbp-16], rax"| B["slot b<br/>owns 1 reference"] class A,B accent
The figure below replays two corpus programs’ generated main_main the same way and shows every object’s count after each helper call and each store into a variable’s slot. The replay also produces the program’s output, and the build checks it against what CPython prints for the program.
pass_alias.py the list reaches 3 while it is being printed (the slots of a and b, plus the print's own temporary reference) and is freed only in the epilogue, while every int stays immortal. Switch to pass_leakflat.py to watch big ints being freed onto the int free list and the next allocation reuse the header.The rule costs calls. Every use of a variable is an aython_incref and an aython_decref, even when the object is immortal and both return at once. The code generator removes a few of them where it can prove them unnecessary, and says so in its comments: a comparison returns an immortal bool, so its result is never released (# test value is immortal (comparison or not): no decref call), and return <local> moves the reference instead of incrementing and then decrementing the same pointer.
The allocator #
All objects live in one region of memory, the arena, requested once at start-up. arena_init asks libc’s mmap for 64 MiB of private, anonymous, readable and writable memory, and keeps three pointers to it in .bss: its start, its end, and the bump pointer, the first byte not yet handed out:
runtime.s · arena_initlines 13777–13805# share arena_init, which stashes the args and mmaps the 64 MiB arena.
...
push rbp # save the frame base (also aligns for mmap below)
...
xor edi, edi # mmap hint address 0 lets libc choose
mov rsi, 67108864 # ask for 64 MiB arena
...
mov r8, -1 # no file descriptor for anonymous mmap
...
call mmap # reserve the arena via libc; -1 (MAP_FAILED) on error
...
je abort_memory # abort if mmap failed (noreturn: realigns itself)
mov [rip + arena_base], rax # remember arena start
mov [rip + arena_ptr], rax # bump pointer starts empty at base
add rax, 67108864 # compute arena end as base plus size
mov [rip + arena_end], rax # remember arena limit
Handing out memory is then a handful of instructions and no bookkeeping: move the bump pointer forward by the size asked for, and abort with MemoryError if that passes the end or wraps around:
runtime.s · alloc_rawlines 529–540# ---- internal: alloc_raw --------------------------------------------
# Input: rdi = byte size (multiple of 8). Output: rax = fresh address.
# Bump-allocates inside the arena. Aborts MemoryError when exhausted.
alloc_raw: # entry for raw bump allocator, rdi size
mov rax, [rip + arena_ptr] # rax gets current arena cursor
mov rcx, rax # rcx copies cursor for math
add rcx, rdi # rcx advances by requested size
jc abort_memory # abort MemoryError if size wrapped
cmp rcx, [rip + arena_end] # compare new cursor with arena end
ja abort_memory # abort MemoryError if past the end
mov [rip + arena_ptr], rcx # save new cursor back to arena
ret # return fresh address in rax
Nothing is ever given back to the bump pointer. Instead, when a count reaches zero, aython_decref releases what the object holds (a list’s elements, a dict’s keys and values, a set’s active keys) and pushes the header onto a free list for its size. The list is threaded through the freed headers themselves: the old head goes into the freed header’s +16 word, and the header becomes the new head. Here is the int case:
runtime.s · aython_decreflines 608–751 lea rcx, [rip + type_range] # rcx gets address of range tag
cmp rax, rcx # compare object type with range
je decref_free_range # go free range if it matches
jmp decref_unwind # unknown type, just unwind
decref_free_int: # int free path starts here
mov rax, [rip + free_int_head] # rax gets int free list head
mov [rbx + 16], rax # store old head in value slot
mov [rip + free_int_head], rbx # push freed block onto int list
jmp decref_unwind # done, go restore registers
and the other end, where int_new pops a recycled header before falling back to the bump pointer:
runtime.s · int_newlines 753–789 mov rax, [rip + free_int_head] # rax gets int free list head
test rax, rax # check if a recycled header is waiting
jz intnew_bump # bump-allocate when the free list is empty
...
mov [rip + free_int_head], rcx # pop the head off the free list
...
call alloc_raw # get fresh bytes from arena
There is one free list per header size and kind, each a single 8-byte head in .bss:
| Free list | Header size | Holds |
|---|---|---|
free_int_head |
24 bytes | int objects |
free_list_head |
40 bytes | list/tuple headers (same shape, shared pool) |
free_str_hdr |
32 bytes | str headers |
free_dict_hdr |
48 bytes | dict headers |
free_range_hdr |
48 bytes | range headers |
free_set_hdr |
56 bytes | set headers (own pool: refcnt/type/used/fill/mask/table/finger) |
free_float_head |
24 bytes | float objects (same size as ints, separate pool) |
Lists and tuples share a pool because they have the same 40-byte shape. Ints and floats are the same size but keep separate pools, and so do dicts and ranges, so a recycled header always comes back to the same kind of object.
Only headers are recycled. Item buffers, string bytes, dict index and entry buffers, and set tables are bump-allocated at their exact size and never reused: when a list grows past its capacity, list_append copies the pointers into a buffer twice the size and the old one is abandoned, and when a list is freed its buffer stays where it was. The comment on aython_decref gives the reason: “freeing them would need size classes we do not have yet”. The consequences are bounded and tested. A program that keeps building and dropping containers eventually exhausts the arena and aborts with MemoryError, which tests/mvp/abort_oom.py pins with a 100-million-character string. And a program that only allocates headers must reuse them all: the judge’s leak probe runs main_main twice in one process and requires that the second run moves the bump pointer by zero bytes, which catches any missing aython_decref in the generated code (Proving it right describes it).
CPython’s allocator is more careful: it groups small blocks into size classes, frees buffers, and returns memory to the system. aython’s is the smallest one that keeps memory use flat for the programs the judge runs, and every part of it fits on one screen.
What reference counting cannot free #
A reference count says how many references exist, not whether any of them can still be reached from the program. A container that holds itself keeps its own count above zero forever:
tests/mvp/abort_recursion_print.pylines 1–4# mvp: abort:RecursionError
a = [1]
a.append(a)
print(a)
After a.append(a) the list’s count is 2, one for the slot of a and one for its own element. Releasing the slot of a, which the epilogue would do if the program got that far, leaves the count at 1, and there it stays: the list is never freed. CPython has the same problem in its reference counting and solves it with a separate cycle collector, the gc module. aython has none: the decision table in docs/core-design.md says “Cycles: Leak by design, tested to not crash”. The memory is reclaimed when the process exits.
The program above is in the corpus for a different reason. CPython prints [1, [...]]; aython’s printer recurses into the list and aborts with RecursionError when the nesting depth reaches 100, rather than overflowing the native stack. That is a pinned divergence; repr() detects cycles and prints CPython’s markers instead. Where it differs from CPython lists both.
One more path leaks on purpose. When an exception unwinds to an except, the code between the raise and the handler is abandoned mid-way, frames included, and any object it still held, local or temporary, leaks: it is neither released nor double-freed.
Instances are dicts #
User-defined classes add no new kind of object. C(...) allocates a fresh dict, the instance’s namespace, and __init__ stores into it; an attribute read or write is a dict lookup or store with the attribute’s name as a str key:
tests/mvp/pass_class_basic.py · emitted · fn_Counter__read.Lbody_fn_Counter__read: # both modes join here with params owned
# body of Counter__read (every return jumps forward to .Lret_fn_Counter__read):
# line 10: return <expr> (park value, release locals, leave)
mov rax, [rbp-8] # rax = self's object (still owned by the slot)
mov rdi, rax # arg = same pointer (helper borrows it)
call aython_incref # take our own shared ownership; rax = same pointer
mov [rbp-24], rax # park the owned receiver
# attr key 'n': copy pooled bytes into a fresh box
lea rdi, .Lstr_4[rip] # key bytes (helper borrows them)
mov rsi, 1 # key length
call str_new # rax = new owned str key (refcnt 1)
mov [rbp-32], rax # park the owned key
mov rdi, [rbp-24] # arg1 = receiver (helper borrows it)
mov rsi, [rbp-32] # arg2 = key (helper borrows it)
call obj_getitem # rax = owned value (missing aborts KeyError)
Methods are resolved by the compiler, not looked up at run time, so an instance needs no pointer to its class and the dict header is enough. The cost is that an instance behaves like the dict it is wherever CPython would consult the class:
| Program | CPython | aython |
|---|---|---|
print(c) |
<__main__.Counter object at 0x...> |
{'n': 0}, the namespace |
| Reading a missing attribute | AttributeError |
KeyError |
isinstance(c, dict) |
False |
Refused at compile time: instances share the dict tag, so no run-time test could tell them apart |
docs/core-design.md (“Classes”) lists the full set, and the frontend chapter shows how classes are flattened before code generation.
Where aython differs from CPython #
- Ints are one signed 64-bit word; overflow aborts with
OverflowErrorinstead of growing (the machine chapter). - Reference cycles are never freed, and printing one aborts with
RecursionError. - Buffers are never reused, so a long-running program that churns containers can run out of its 64 MiB arena.
- Instances are dicts, with the visible differences in the table above.
- Strings are ASCII bytes with no encoding; a string’s length is its byte count.
Where this lives #
| Concept | Where |
|---|---|
| Every layout, and the ownership rule in the runtime’s words | runtime.s, header comment “Object model” and “Ownership rule”; sets in docs/core-design.md, “Object layout” |
| Type tags, singletons, the small-int cache, the sentinel | runtime.s .data: type_*, aython_none, aython_true, aython_false, empty_tuple, set_dummy, small_ints, SENTINEL |
| Counting | runtime.s aython_incref, aython_decref |
| The arena and the bump pointer | runtime.s arena_init, alloc_raw, arena_used; arena_base, arena_ptr, arena_end in .bss |
| Free lists | runtime.s free_* heads in .bss; the decref_free_* paths; the pop at the top of int_new, float_new, str_from_buf, list_new, tuple_new, dict_new, range_new, set_new, and of the few other helpers that build a header directly |
| The assignment pattern and the incref on every use | src/aython/codegen.py, the comments it writes on every line; read them in the assembly reader |
| Instances | src/aython/codegen.py the C(...) construction path (dict_new as the instance namespace) and attribute access through obj_getitem / obj_setitem |
| Cycles and arena exhaustion, pinned | tests/mvp/abort_recursion_print.py, tests/mvp/abort_oom.py, tests/mvp/pass_leakflat.py |
| The figures | docsite/figures/objects.py, docsite/static/widgets/layout.js, docsite/static/widgets/refcount.js |
Check yourself #
Check yourself
In
pass_alias.py,a = [1, 2]thenb = a. What is the list’s reference count afterb = afinishes?- 1: assignment only copies a pointer
- 2: one for the slot of
a, one for the slot ofb - 3: the two slots plus the list’s own header
Answer
2: one for the slot ofa, one for the slot ofb. The code increfs the value it reads out ofa’s slot, parks it, releasesb’s old value (None, a no-op) and stores the pointer inb’s slot. Each slot owns one reference. It reaches 3 briefly when the program passes the list toprint, which takes a reference for the call.x = 10and theny = x. What does theaython_increfemitted fory = xdo to the int 10?- Raises its count from 1 to 2
- Nothing: 10 is in the small-int cache, its count is the sentinel, and
aython_increfreturns at once - Copies it, so
xandypoint to different objects
Answer
Nothing: 10 is in the small-int cache, its count is the sentinel, andaython_increfreturns at once.int_new(10)returns the cached object fromsmall_ints. Its count is 2^62, at or above the sentinel, so the unsignedjaeinaython_increfskips the increment. Both names point to the same immortal object.aython_decrefdrops a 24-byte int to zero. Where does its memory go?- Back to the bump pointer
- Onto
free_int_head: the old head is written into the freed header’s+16word and the header becomes the new head, to be popped by the nextint_newof a value outside the cache - To the kernel, with
munmap
Answer
Ontofree_int_head: the old head is written into the freed header’s+16word and the header becomes the new head, to be popped by the nextint_newof a value outside the cache. Headers are recycled through per-size free lists threaded through the freed headers themselves. The bump pointer only ever moves forward, and the arena is never returned to the kernel before exit.A list’s item buffer is full and
list_appendgrows it. What happens to the old buffer?- It is freed onto a buffer free list
- It is abandoned: the pointers are copied into a buffer twice the size and the old one stays in the arena, never reused
- It is kept and linked to the new one
Answer
It is abandoned: the pointers are copied into a buffer twice the size and the old one stays in the arena, never reused. Only headers are recycled. Buffers leak by design, because reusing them would need size classes the allocator does not have; the arena’s size bounds the cost, andabort_oom.pypins what happens when it runs out.How does the runtime decide that an object is a list?
- It reads the 8 bytes at the type tag and compares them with the text
list - It compares the pointer at
+8with the address oftype_list, loaded withlea rcx, [rip + type_list] - It checks the object’s size
Answer
It compares the pointer at+8with the address oftype_list, loaded withlea rcx, [rip + type_list]. Only addresses are compared. The bytes stored attype_listspell its name for human readers, and nothing reads them, which is why the inconsistent byte order oftype_floatandtype_setis harmless.- It reads the 8 bytes at the type tag and compares them with the text