Runtime internals

The runtime is freestanding C, about 7,700 lines in runtime/, linked into every program. This page describes the data structures and algorithms behind the arena: the pool allocator, the allocation log and its hash table, promotions, the string machinery, the compiler-side analyses that remove scopes, huge pages, and the crash reporter. Numbers refer to the self-hosted compiler precompiling a mid-size source file (49,000 allocations) or its own source (286,000 allocations).

1. The pool allocator

rt_alloc(size) returns a block of at least size bytes from a pool of chunks. A chunk is TOTAL_BLOCKS (4096) blocks of SINGLE_BLOCK_SIZE_HEAP_BSS (512) bytes, i.e. 2 MB, plus a bitmap of one byte per block: 0 free, non-zero occupied. An occupied run is a ramp: 1 on its first block, then 2, 3, ... up to 255, so the first block of the allocation that contains any block is one subtraction away (rt_alloc_base_of, below). The ramp is also the allocation's size: freeing counts it, the header does not store it.

bitmap  [1][2][0][0][1][0][0][0]...   one byte per block
data    [##][##][  ][  ][##][  ][  ][  ]...   512 bytes per block
         ^ header (16 bytes) then the payload; the pointer returned is header + 16
           [p-16] qword  string length (sealed strings only), 0 otherwise
           [p-8]  dword  type id: a class id written by 'new', STR_TYPE_ID for a sealed string, 0 otherwise
           [p-4]  dword  references and state (§3)

Under --noheap the same code path stops at chunk 0 (rt_alloc_raw): running out of blocks is a fatal arena full, never an mmap.

2. The allocation log and its hash table

Every allocation is appended to alloc_log (an array of pointers that starts in the BSS and grows by doubling through mmap when the number of live allocations exceeds --bss-size). Scopes are bookmarks into it (see The arena). Promotions and explicit frees need the index of a pointer in the log, which used to be a linear scan and dominated regex-heavy programs (89% of the time in one profile). Now:

3. Promotion and pinning

4. Strings

5. Scope elision in the compiler

An arena_enter/arena_exit pair per loop iteration costs two calls even when the body allocates nothing. The compiler proves, per loop and per function, whether a scope is needed:

Measured on the self-precompile: arena_enter calls 555,000 → 285,000, instructions 123 M → 110 M. TRYC_DEBUG_SCOPE=name prints the reasoning for a function or loop. The rule that keeps this safe: every fresh str assigned to a local is promoted to the function level, parameters belong to the caller, fields and globals go to the root, so an alias or an interior pointer never needs a promotion of its own; and when one is promoted anyway (a return s + n), the runtime promotes its base.

A related mechanism is the nursery (rt_nursery_alloc): a bump allocator for values the compiler proves never escape the function that creates them, reset in one step at arena_exit, with no log entry. Library functions such as substrget have a _nursery twin that the compiler substitutes when the analysis allows it.

6. Huge pages and page faults

Profiling the self-hosted compiler showed a quarter of its time in the kernel's page-fault path: every freshly mapped 2 MB chunk was touched one 4 KB page at a time (513 traps per chunk). pool_chunk_map now over-maps by 2 MB, trims the ends so the data area is 2 MB-aligned, and calls madvise(MADV_HUGEPAGE): one fault per chunk and one TLB entry (minor faults 15,336 → 5,201, wall 82 → 68 ms). --hugepages extends the same treatment to the three large BSS buffers (hash table, chunk 0, string-builder chunk 0), which brings the faults to 153; it is opt-in because a tiny program then pays for zeroing whole 2 MB pages.

Loading libLLVM.so dynamically cost another fixed 12 ms per process (350,000 relocations, copy-on-write of 8 MB of .data.rel.ro, 590 constructors); --llvm-static links the needed LLVM components statically instead (41 → 31 ms per compile, faults 3,700 → 1,300).

7. The crash reporter

_start installs a SIGSEGV handler on an alternate stack (init_protect_segv). arena_enter receives the address of the function being entered (loops pass 0) and pushes it on bt_stack_addr (BT_STACK_DEPTH = 500; deeper frames are simply not recorded); arena_exit pops it after freeing, so a fault during a free still shows the function that was leaving. The report prints the signal, the faulting address and whether the page was unmapped or protected, the kind of access, the function history from innermost to outermost, then RIP, the instruction bytes and every register. Addresses are hexadecimal; the check_arena_syms.py helper shipped with the runtime annotates them with function names using nm:

./program 2>&1 | python3 check_arena_syms.py ./program

An out-of-bounds array index deliberately triggers the same path after printing Error! array index out of bounds, so it gets the full report instead of a bare message.

8. Numbers to keep in mind

Workload (self-hosted compiler)TimeMemory
precompile of a mid-size file (--dbi)~23 ms49,000 allocations, ~30,000 live at exit
full compile of the same file (in-process LLVM)~30 ms51 MB RSS
precompile of its own source~78 ms286,000 allocations, 119 MB RSS
hello world0.2 ms (1.4 ms with --hugepages)chunk 0 only, no mmap