| @@ -0,0 +1,33 @@ |
| 1 | +# ADR 0073: `std/table`, tables any actor reads without a message |
| 2 | + |
| 3 | +**Status:** Proposed (2026-10-30, issue #407); needs engineering review (#44). It adds shared mutable state to a language whose state otherwise lives in actors, and a second way values become shared (ADR 0002). |
| 4 | + |
| 5 | +## Context |
| 6 | + |
| 7 | +toki2 keeps one notebook actor per owner. Every read asks that actor: a page costs several message hops, and at an equal 2,500 req/s it measured p95 3.9 ms / p99 18.6 ms against 2.1 / 3.5 ms for a design that read one shared value. Erlang answers this read path with ETS (`read_concurrency`) and `persistent_term`: any process reads the latest value under a key with no message, while one process keeps it current. |
| 8 | + |
| 9 | +Louise had no global or shared mutable storage: no registry, no table. It had the pieces: promotion of a value graph to atomic counts (`send.rs`, ADR 0002's "promoted to shared"), type-erased generic externs, and an interpreter that runs every actor on one thread. |
| 10 | + |
| 11 | +## Decision |
| 12 | + |
| 13 | +```louise |
| 14 | +pub opaque type Table<v> |
| 15 | +pub fn new :: -> Table<v> / <proc> |
| 16 | +pub fn get :: table: Table<v> key: String -> Option<v> / <proc> |
| 17 | +pub fn put :: table: Table<v> key: String value: v -> () / <proc> where Send<v> |
| 18 | +pub fn remove :: table: Table<v> key: String -> () / <proc> |
| 19 | +``` |
| 20 | + |
| 21 | +- **Values are shared, never copied.** `put` makes the value shared with `send::share`, a new entry point that promotes the whole graph even when the root's count is 1 (`transfer` would move a unique value, leaving a non-atomic count other threads would race on). `get` retains the stored value; every reader holds the same blocks. A value that cannot be shared panics in the writer, as a send does; `where Send<v>` makes that a compile error in typed code. |
| 22 | +- **A reader can't race a reclaim.** The map is 64 shards, each a read-write lock over the entries. `get` retains the value before it releases the read lock; `put` and `remove` release what they replace only after releasing the write lock. A reader therefore always holds its own count before a writer can drop the map's. |
| 23 | +- **Tables are immortal.** A table is an id (an `Int`), never freed, so no finalizer has to release Louise values (resource finalizers can't). Entries are released on replace and remove; what a program leaves in a table at exit is still alive then. |
| 24 | +- **Effects:** all four operations are `<proc>`, and none is `pure()` in the extern table, so fusion never moves or drops a read. |
| 25 | +- **`put` consumes its arguments** (`owned`, in the extern table and the declaration). |
| 26 | +- **Interpreter:** a map in the interpreter's state; one thread runs every actor, so values are stored as they are; `put` refuses unsendable values like `send`. |
| 27 | + |
| 28 | +## Consequences |
| 29 | + |
| 30 | +- A read is a shard lookup and an atomic increment, from any actor, in parallel with other reads. |
| 31 | +- Writers to one shard serialize; the 64 shards keep unrelated keys apart. |
| 32 | +- Shared mutable state is visible to every actor holding the table. Programs keep one writer per key (an owning actor), as with `persistent_term`; the table does not order concurrent writers. |
| 33 | +- Tests: the runtime's scenarios check exact counts (no live block left) for put, get, replace and remove, and `share`'s promotion and refusal; `tests/run/table.lse` runs on both backends (replace and remove while a reader keeps a value, two tables apart, 32 actors reading while one writes 2,000 times); `tests/ui/table_send.lse` checks the compile-time refusal; the program is clean under AddressSanitizer and UBSan on 4 threads. |