| @@ -94,3 +94,84 @@ In user space, the time is spread over the scheduler loop, fiber switches and fr |
| 94 | 94 | - **BEAM's OTP 25 buffers, added under contention.** They make a locked queue scale, at the price of 64 buffers per contended actor and a protocol to install and remove them. A lock-free inbox scales without them, and they could still be added later if one inbox line becomes the bottleneck. |
| 95 | 95 | - **Vyukov's MPSC queue (Pony's).** One swap per push, cheaper than a CAS loop under contention, but a push cannot fail, so an exited actor cannot refuse messages. They would need another way to reach their sender for freeing. |
| 96 | 96 | - **Links and monitors as signals in the inbox (OTP 21).** It would remove the last lock, but exits, links and monitors would have to be argued again in terms of signal order. They are rare enough that their lock is not where the time goes. |
| 97 | + |
| 98 | +## Revision 1 — 2026-10-12: implemented (issues #136, #137) |
| 99 | +**Status:** Proposed; needs engineering review (#44). It also changes the runtime ABI (version 3, below). |
| 100 | + |
| 101 | +### The inbox (issue #136) |
| 102 | +Implemented as decided in point 2 (`runtime/louise_rt/src/inbox.rs`, `otp.rs`), with these differences: |
| 103 | +- **Finding the receiver:** a per-thread cache of cells (`sched::SendCache`, 64 slots), not a reference carried by subjects, which would have changed what a subject is. A cached cell may outlive its actor, but its inbox is closed then and a send to it is refused, as one to an unknown pid is; pids are never reused. A pid is cached on its second miss in a row in its slot. Otherwise a thread sending to many actors in turn (the 1,000-actor ring) evicts an entry on every send, which made `messages` 4% slower than having no cache at all. Without the cache, `fanin` on 22 threads took 303 ms (2.6 times one thread), against 149 ms with it. |
| 104 | +- **Timers and kills wake a receive under the actor's lock**, not through signal nodes: they take that lock anyway. `WAITING` may then outlive the wait it was set for. A push that clears it tries a wake-up, and `Proc::woken_by_message` refuses it unless the actor waits in a receive, so it is harmless (a message never wakes a sleep). |
| 105 | +- **Handing a message over:** a sender that finds the receiver waiting, with an empty inbox and its lock free (`try_lock`, so it never waits), hands the message over if the receiver waits for its tag. That costs no node and nothing for the receiver to take: a ring, or a call's reply. Without it, `messages` was 8.5% slower on one thread. Under fan-in the lock is often taken, and senders push. |
| 106 | +- A message the receive waits for skips the tag map (`otp::take_wanted`). Nodes come from mimalloc. A resumed receive takes one lock, not two. |
| 107 | +- **Spurious wake-ups:** a message on a tag the receive does not wait for wakes it, at most once per wait. The new `tests/bench/selective.lse` makes 50,000 calls, each waiting on its own reply subject while 4 actors flood its other subject. It is faster than before at every thread count (below). |
| 108 | + |
| 109 | +### Memory per idle actor (issue #137) |
| 110 | +None of these changes how deep a compiled actor can recurse, so point 3's condition (an ADR of their own for changes to recursion depth) does not apply. |
| 111 | +- **Hibernation.** An actor parked in a receive or a sleep for 400 to 600 ms of wall time has its stack saved and given back, and written back in place before it next runs, whatever wakes it. An idle actor's stack holds 688 bytes, against the 4 KiB page and the 4 KiB page table it kept resident. |
| 112 | + - Each scheduler thread lists the actors that park on it, and looks at them again two epochs (200 ms each) later. |
| 113 | + - Blocking calls are excluded, since they run on the actor's values. So are socket waits, which are meant to be short. |
| 114 | + - It uses wall time, not the scheduler's clock, so it changes no behaviour, only memory. |
| 115 | + - `LOUISE_PARKED_EPOCH_MS=0` hibernates every parked actor at once. CI runs every native test program that way, and they pass under ASan and TSan too. |
| 116 | + - An epoch of 50 ms made `timers` 10% slower on one thread: its 2,001 servers are idle longer than that between calls. |
| 117 | +- **Stacks:** |
| 118 | + - Stack tops are aligned on 2 MiB, so a hibernated stack's page tables are freed as well: Linux's `CONFIG_PT_RECLAIM` frees one only when the whole range it maps is released. |
| 119 | + - Stacks start a varying distance below that top, in steps of a page and a cache line. Aligned alike, every stack's top frames fell in the same cache sets, which cost the ring 5%. |
| 120 | + - The pool keeps up to 16,384 finished stacks, given back beyond the first 256. |
| 121 | + - At exit, stacks are no longer unmapped one by one; unwinding and `defer`s still run. |
| 122 | +- **Fewer and smaller allocations:** |
| 123 | + - A fiber's channel and result share one allocation. |
| 124 | + - A receive keeps one tag inline. |
| 125 | + - Links, monitors and exit-signal settings are allocated on first use. |
| 126 | +- **Runtime ABI 3: `ctx->xargs` points at the running thread's slots.** The 26 slots for call arguments beyond the sixth were part of every actor's context: 208 of an `Actor`'s 336 bytes. |
| 127 | + - `run_slice` points `ctx->xargs` at the resuming thread's slots. |
| 128 | + - It is safe because nothing suspends between a caller's stores and the callee's copy (`docs/runtime-abi.md`). |
| 129 | + - Generated C is unchanged. |
| 130 | + - The new `tests/run/many_arguments.lse` covers it. With a deliberately stale pointer, it fails in 10 runs of 10. |
| 131 | +- **The budget's mapping item was wrong.** `MADV_GUARD_INSTALL` guards would make a stack one mapping instead of two. But their markers live in page tables: 4.26 KiB per stack, against about 230 bytes of kernel memory per mapping. Two mappings per actor stay. |
| 132 | + |
| 133 | +### Measurements |
| 134 | +Memory: `idle.lse` with 100,000 against 1,000 actors, sampled 3.5 s after spawning, so after hibernation. |
| 135 | + |
| 136 | +| Per idle actor | Before | After | |
| 137 | +|---|---|---| |
| 138 | +| Resident memory | 5.2 KiB | 1.59 KiB | |
| 139 | +| Page tables | 4.3 KiB | 0.26 KiB | |
| 140 | +| Total | 9.5 KiB | **1.85 KiB** (budget: 2 KiB) | |
| 141 | +| Mappings | 2 | 2 (and about 0.45 KiB of kernel memory for them) | |
| 142 | + |
| 143 | +- **Where the memory goes:** |
| 144 | + - 688 bytes of saved stack; |
| 145 | + - the `Actor` (136 bytes) and its `Cell`; |
| 146 | + - the program's own values; |
| 147 | + - one page of upper-level page tables per GiB of stack address space, shared by about 15 stacks. |
| 148 | +- **Possible next steps:** |
| 149 | + - Smaller stack reservations would share those upper-level pages, but they change recursion depth (ADR 0004). |
| 150 | + - Until actors hibernate, spawning 100k actors on one thread peaks at about 530 MB. |
| 151 | + |
| 152 | +Time: release builds, the runtime before this revision (#138) against after, interleaved, best of 9 to 15, pinned to the performance cores. On this hybrid CPU, the same build's best time varies by 3–5% from one batch of runs to the next. |
| 153 | + |
| 154 | +| Benchmark | 1 thread | 11 threads | 22 threads | |
| 155 | +|---|---|---|---| |
| 156 | +| `fanin` | 107 → 89 ms (−17%) | 508 → 90 ms (−82%) | 546 → 144 ms (−74%) | |
| 157 | +| `selective` | 41 → 39 ms (−6%) | 101 → 44 ms (−56%) | 102 → 48 ms (−53%) | |
| 158 | +| `messages` | +3 to +8% (55 → 58 ms) | +3% | −2% | |
| 159 | +| `parallel` | +3% | 0% | not measured | |
| 160 | +| `idle` (spawning) | 1211 → 732 ms (−40%) | 1169 → 1023 ms (−12%) | not measured | |
| 161 | +| `timers` | +0 to +10% | −11% | not measured | |
| 162 | + |
| 163 | +- **The targets:** |
| 164 | + - `fanin` on 22 threads takes 1.6 times its one-thread time, within the 2 times aimed for (4.8 times before). |
| 165 | + - `messages` is at the 5% limit on one thread, and within it on more. |
| 166 | + - Memory per idle actor fell rather than grew. |
| 167 | +- **Instructions, one thread, exact:** |
| 168 | + - `messages` runs 13% more user-space instructions, `timers` 11%: the inbox's atomic operations and the hibernation bookkeeping. |
| 169 | + - Kernel instructions fall by 18–47% in `messages`, `timers` and `idle`, mostly because stacks are no longer unmapped one by one. `fanin`'s grow by 56%; that was not investigated (it runs 17–82% faster). |
| 170 | + |
| 171 | +### Verification |
| 172 | +- **loom:** |
| 173 | + - The `otp` models run the inbox and `Proc` together on a model scheduler. They pass bounded to 3 preemptions (in CI) and unbounded (9 minutes). |
| 174 | + - Bounded, they still find each of these, introduced on purpose: a sender that does not wake a waiting receiver, whether it pushed or handed over; `stop` parking without re-checking the inbox; an exit that does not interrupt a receive; a hand-over that does not wake. |
| 175 | +- **Tests and CI:** |
| 176 | + - The full `scripts/ci.sh` passes, with `LOUISE_CI_TSAN=1`. |
| 177 | + - That includes ASan and UBSan, the native programs on several threads, the native programs with immediate hibernation, the stress tests, the demo, and the demo under load with the leak oracle. |