ADR 0150: Actor Registration Off the Start Path

Status

Implemented (2026-11-16) — Epic BT-4943. The registrations were removed from the start path by BT-4951, and BT-4953 deleted the registry modules and amended the earlier ADRs; condition (b), the owner's re-measure (BT-4952), is met: the owner measured rows 16a/16c/16d at +S 18:18 with tracing off (2.8x, 6.0x and 3.2x faster; one scan over 1,000,082 idle processes took 1.2 to 1.4 s) and said "go" before the registries were deleted, so option C is no longer the fallback. Accepted 2026-11-16. Decision: option D, no table, with the two owner conditions below and, at acceptance, option C (the minimal table design) as the stated fallback. Proposed 2026-11-15; revised the same day after owner review, which sent the first draft back. Issue BT-4854; the rest of the start path is BT-4932.

Revision note. The first draft decided "insert, then monitor" with a table, a bounded pending buffer, a drain pass, a backstop reaper and a scan rebuild, and dismissed a no-table design in six lines. This revision puts four options side by side, corrects the facts the first draft got wrong, and recommended the no-table design (D), with the minimal table design (C) as the fallback if one of the acceptance conditions below fails; the owner accepted D on 2026-11-16. The WIP prototype of the first draft is on bt-4854-registry-impl; it is evidence, not a decision.

Amends: ADR 0005, 0009, 0069, 0079, 0093, 0123, 0126, 0129 and ADR-5 of docs/beamtalk-ddd-model.md (see Consequences for earlier ADRs). Those documents were edited by the cleanup PR (BT-4953).

Context

Problem

Every actor start passes through up to three registrations:

  1. beamtalk_object_instances (a gen_server and two ETS tables: allInstances, hot reload, class_of/1). The generated spawn, spawn_monitored and spawn_monitored_async (and the native facade's spawn) call its register/2, a synchronous gen_server:call, from the spawning process (gen_server/spawn.rs, instance_registration_doc).
  2. The node-wide actor list. Naming matters here, because two different things are called "actor registry":
    • the process registered locally as beamtalk_actor_registry is beamtalk_repl_actors (start_link(registered)): the actor list behind Node current actors, the REPL, IDE and LiveView lists, stop_class_actors and should_purge_module. Each compiled actor's generated init/1 emits [beamtalk, actor, lifecycle, start]; maybe_announce_lifecycle/2 in beamtalk_actor.erl turns it into a track_spawned/2 cast; the server resolves the module with a call to the class process and monitors the pid;
    • the module beamtalk_actor_registry.erl is the ADR 0079 name registry (register_name/2, all_registered/0, actor_started_at/1). It is not part of this change. Below, "the actor list" means the first, and "the name registry" the second.
  3. On REPL-originated spawns only, generated code calls beamtalk_actor:register_spawned/4 (dispatch_codegen/spawn.rs) → on_actor_spawned/4 → register_actor/4, a second synchronous call to the actor list, plus the actor_spawned_hook that feeds beamtalk_workspace_meta. It adds the module name (the lifecycle path resolves it anyway) and the hook.

The two servers are singletons, so every start on the node queues behind them. Phase 1 of BT-4854 measured the cost and where the rest of the cost sits. The numbers are from a cloud VM (Firecracker, 4 cores, OTP 28.5), so they compare only with each other, not with the maintainer's M5 Max numbers (rows 16a–16d in the issue). +S 1 is stable and measures total CPU; +S 4 is noisy (16a/16c varied by up to 40% between identical runs) and is shown as a median.

Per actor, µs16a start→run→stop +S4 / +S116c plain spawn + stop +S4 / +S116d, 100 in flight +S4 / +S1
bare floor1.05 (spawn_monitor + DOWN)3.5 (gen_server start + stop)2.0
baseline (today)31.9–36.3 / 10.5–10.914.3–14.4 / 11.6–11.814.6–15.0 / 10.6–11.0
ets-reaper11.6–11.8 / 8.0–8.310.5–10.7 / 8.16.9–8.5 / 7.9–8.6
lazy-batch11.7 / 8.310.7 / 8.46.9 / 8.1
insert-then-monitor12.7 / 8.911.8 / 9.49.8 / 9.1
label-scan (no table)11.5 / 7.49.7 / 7.74.9 / 7.2
sharded (4 servers)38.7 / 10.336.8 / 11.417.4 / 10.5
partition-link14.0–42.6 / 8.719–23 / 10.58.3 / 8.5

What the profile says:

Current state, corrected

Readers

Phase 1 listed every reader. Frequency matters, and the first draft got it wrong.

ReaderSource todayWhat it asksHow often
class_of/1 via lookup_class/1 in beamtalk_actor.erl (5 direct sites + resolve_class/2)instance tablethe class of a bare pid, for dispatch telemetry and the {Class, Selector} aggregates, the TimeoutProxy → infinity timeout rule in sync_send_remote, and deadlock_detected error contextsee below
Node current actors / actorsOf: / actorAt: (beamtalk_node.erl, erpc targets)actor listevery live actoron demand, local or over erpc; tests pin it (node_test.bt, workspace_actors.btscript)
REPL / CLI / MCP / VS Code actor lists, actor_snapshot_frames (LiveView)actor listevery live actor with class, module, spawn time; the snapshot must leave no gap with the pusheson demand, plus on connect
stop_class_actors (removeFromSystem)actor listlive actors of one classper removeFromSystem
should_purge_module (beamtalk_repl_eval.erl)actor listdoes any tracked actor's recorded module equal this eval module?after every REPL evaluation (cleanup_module in after clauses)
Hot reload (beamtalk_repl_loader_activate.erl)instance table, all_instances/1live pids of one classonce per reloaded class and once per descendant, per reload
trace_store bottlenecks (raw fold of beamtalk_instance_registry), actor_healthinstance tablequeue depth of every live actor / class of one pidon demand
count_actors_for_module (list-classes), workspace_meta countactor listcountson demand

What class_of/1 is used for, site by site (read from the code, not measured):

SiteClass used forHint available?
async_send/4 final clause (beamtalk_actor.erl ~1511)dispatch_span metadata and always-on aggregateno _known variant exists
cast_send/3 → cast_send_pid/4 (~1609)samecast_send_known/4 passes the record's class
sync_send/3 → sync_send_remote/4 (~1829)span, and the TimeoutProxy rule (semantic)sync_send_known/4 passes it
sync_send/4 family (~2027)span onlysync_send_known/5 passes it
raise_self_send_during_away_super/2 (~2072), raise_call_cycle/3 (~2183)context field of a deadlock_detected errorerror path only
kill_and_wait/1 (~2420)metadata of the kill lifecycle eventpid only

The ordinary Beamtalk send path (beamtalk_message_dispatch) already passes the #beamtalk_object{} record's class (BT-4113) and never calls lookup_class. The bare-pid callers are: async_send/4 (futures), the generated native facade (native_facade.rs, which has the record and a static class in hand), beamtalk_reactive_subprocess, sync_send(ActorPid, node, []) inside is_remote, the two error paths and kill. Today a lookup of an unregistered pid (a supervised child) already answers unknown.

Options

S — Status quo

Keep both singletons and the synchronous call. Cost: 16a is 30× the floor at +S 4 in the VM; buccaneer's HTTP/2 streams stay bare processes. Benefit: nothing changes, nothing to test. It does not meet BT-4854.

B — Insert, then monitor, as specified in the first draft

Caller inserts rows (pid-keyed row with a registration token, class-table row), adds the pid to a pending table, bumps an atomics counter; a server drains the pending table in 50 ms batches, monitoring live pids and deleting dead ones; a bounded pending buffer with a synchronous flush/0 at the bound; a backstop timer reaper (max(30 s, 20 × pass time)); a restart pass; a scan rebuild when the heir is lost; the actor list becomes a view; the actor's own init/1 inserts again. Six mechanisms. 16a 12.7 / 8.9 µs.

C — The minimal table design

Reviewer's reduction of B, which this ADR accepts as the form a table design should take:

Four mechanisms: pending + drain, bound, restart pass, scan rebuild.

D — No registry table; the class travels; the rare readers scan

Remove all three registrations from the start path. There is no table, no server, no monitor, no cast.

  1. The class travels. Every lookup_class site either gets the class from the caller or computes it only when needed:

    • sites with a #beamtalk_object{} or a static class already pass it (*_known variants); add async_send_known/… and make the native facade emit the _known call (it has Self and a static class_name); beamtalk_reactive_subprocess passes the class it holds;
    • the error paths and kill_and_wait/1 are rare and may read process_info(Pid, {dictionary, '$beamtalk_actor'}) (≈1.6 µs plus a signal to the target, against ≈0.7 µs for the ETS read);
    • the remaining bare-pid callers use the same read. This is the only cost D adds to a send, and only for sends that arrive with a bare pid. The {Class, Selector} aggregate is always on, so a bare-pid send needs the class whether or not telemetry is attached; the TimeoutProxy rule needs it semantically, but proxies are reached through beamtalk_message_dispatch, which has the record.
  2. Readers become scans or stop enumerating. erlang:processes_iterator/0 plus process_info(Pid, {dictionary, '$beamtalk_actor'}). One enumeration costs about 2.5–4.5 µs per process in the node, whatever the fraction that are actors:

    live processesone enumeration (+S 4)
    1k3–6 ms
    100k0.21–0.30 s
    1M2.6–4.6 s (two concurrent scans slow each other: 4.9–5.7 s each)

    Reader by reader:

    ReaderUnder DCost
    Node current actors, actorsOf:, actorAt:, REPL / CLI / MCP / IDE lists, trace_store bottlenecks, count_actors_for_module, workspace_meta countone scan per call; actorAt: is process_info on the parsed pid plus the marker check, no scan3–6 ms at 1k; 0.2–0.3 s at 100k; seconds at 1M
    allInstances / instanceCountscan, filter by classO(processes in the node), not O(instances of the class)
    stop_class_actorsone scanper removeFromSystem
    should_purge_moduledoes not enumerate actors. The question is "does anything still run this module's code", which code:soft_purge/1 / erlang:check_process_code/2 answer directly, including processes outside the actor list and processes holding funs of the module. The actor list's recorded module is the class module, never the eval module beamtalk_repl_eval_N, so the present check is in practice always true after the read. To be verified when implemented.no registry read, no scan; this also removes the per-evaluation O(actors) call today
    Hot reloadone scan per reload, grouped by class, instead of one ets:select per class and per descendantone scan per save
    class_of/1 readerssee item 1≈0 for the common path
    actor_snapshot_frames and the incremental IDE feedthe consistent-snapshot problem is real: a scan is not one, and the feed must leave no gap with pushes. Not solved by D alone.see below
  3. An index only while someone is watching (optional, not part of D's first step). ActorSpawned / ActorStopped already exist as system announcements (ADR 0093). An IDE or workspace session that wants an incremental feed subscribes, takes one scan as its initial snapshot, and applies the announcements after it (subscribe first, then scan, then drop announcements older than the scan). The index lives in the subscriber, not in the runtime: no runtime state exists without a consumer, which is the property docs/beamtalk-principles.md §4 asks for (the same operations mean the same thing in the REPL, beamtalk run, beamtalk test and releases). It differs from the dismissed "register only when a workspace is attached" in that Node current actors, hot reload and purge work everywhere, by scan; only the incremental feed needs a subscriber.

  4. What it gives and removes. Start path removes: both register calls, the track_spawned cast and its monitor, the class-process call in the cast handler, and the REPL register_spawned → on_actor_spawned → register_actor/4 call (the REPL no longer needs to tell the runtime about an actor; the actor_spawned_hook for beamtalk_workspace_meta moves to the ActorSpawned announcement subscriber). What remains: the '$beamtalk_actor' write the actor already does, one erlang:system_time/1 write for '$beamtalk_actor_started_at' (see below), the lifecycle announce and telemetry (BT-4932). Measured prototype: 16a 7.4 µs at +S 1 against 8.9 for B and 10.9 today, 11.5 vs 12.7 vs 31.9–36.3 at +S 4.

  5. What spawned_at needs. A scan cannot recover '$beamtalk_actor_started_at' unless the process wrote it. D has the generated init/1 write it (one system_time call and one put; the cost is not yet measured and goes into the acceptance measurements). That also fixes the ADR 0126 §4 tie-break for compiled actors and does not depend on BT-4871.

  6. native: backing-module actors. User gen_servers with no marker and no lifecycle event. They are in allInstances today only because the facade's spawn registers them. Under D they would disappear from allInstances and hot reload unless the scan also matches '$initial_call' against the backing-module index beamtalk_actor_registry (the name-registry module) already keeps for Node current actors. D uses that index in the scan; this must be tested, and it is a point where B or C would be safer.

What D costs, plainly:

Who is hurt: a node with ~1M processes that runs hot reload, removeFromSystem or a polled actor list. The project does not currently serve that case through those readers (hot reload is a development operation; production nodes are not hot-reloaded in bulk), and ordinary runtime paths do not touch them. Whether the project needs to is the one owner call D hinges on.

What each option leaves to build and maintain

mechanismsstart path (+S 1, 16a)readersrisk
S0 new10.9 µsunchangedthe problem stays
Bpending buffer, drain, bound, timer reaper, restart pass, scan rebuild (6)8.9 µstable viewshighest: most code and states to test; shared counter and ETS writes on every scheduler
Cpending + drain, bound, restart pass, scan rebuild (4)≈8.9 µstable viewsmedium: shared writes at +S 18 unmeasured
Dscan, plumbing the class through sends, started_at write (3 small)7.4 µsscansreaders O(processes); native actors; snapshot only with a subscriber

Decision

Accepted 2026-11-16: adopt D, no table. It removes the work instead of making it cheaper, it needs the fewest mechanisms, it has no shared write on the start path (so nothing for 18 schedulers to contend on), it needs no recovery story (no owner, no heir, no rebuild), and the two readers that run most often (class_of/1 on the send path, should_purge_module after every evaluation) stop enumerating or stop looking up. Its costs fall on rare, human-driven readers, which is also where the owner's steer (the registries "may not be needed as much now") points.

Owner conditions (2026-11-16).

Acceptance conditions. D is accepted only if each of these holds when measured; if one fails, fall back to C, which was the stated fallback at acceptance (both conditions held, so it was not needed):

  1. Rows 16a/16c/16d on the maintainer's machine at +S 18 with tracing off (condition b), before and after, and the nightly vs-elixir gate does not regress. (The cloud-VM numbers in this ADR are not sufficient to accept either design; contention is the thing they cannot show.)
  2. One enumeration at the process counts of the project's real nodes (the maintainer's, and a buccaneer load test), including with 100k–1M live processes, stays inside a bound the owner accepts for hot reload and the REPL actor list.
  3. The '$beamtalk_actor_started_at' write in generated init/1 costs less than the registry hop it replaces.
  4. Native-actor listing through '$initial_call' works in a test.
  5. The weaker read-your-writes is pinned by tests: a sync-spawned actor is visible to the next allInstances; an async-spawned one may not be, and the test says so; a process dying mid-scan is skipped.
  6. Every reader in the table above has a test (including trace_store bottlenecks and actor_health, which must not return empty silently).

If C is chosen instead, it must be built as specified in Option C, with PendingMax a placeholder (the first draft's 8192 was asserted before any harness result). The open-loop harness (16 spawners, PendingMax 512) showed pending peaking at ≈520, server mailbox 36–58 and dead rows ≤530 over ≈400k registrations per mode; the value for production has to come from the +S 18 run (where does pacing start, how much memory does an async flood hold), and is part of acceptance.

Observable changes

Common to any option that removes the synchronous call:

Membership. Under D, scanning by the marker finds every compiled actor, supervised and named children included, so allInstances and hot reload gain members (today they miss plain supervised and named children). The actor list readers (Node current actors, stop_class_actors, should_purge_module) already have them. Option C with the actor's own init/1 insert has the same effect. If the owner wants membership kept as today, that is a separate decision (see Alternatives, "Keep membership"). The effect on purge is analysed next.

Supervised children and removeFromSystem / purge

This hazard exists on main today, independent of this ADR: stop_class_actors/1 reads the registered actor list, which already holds supervised and named children, and kills each. removeFromSystem kills the actors before it stops the class process and deletes the module.

Reproduced on main (40 runs, each on a fresh node, real REPL path with the workspace running; RfsPermAN is an Actor with supervisionPolicy #permanent, or #transient, and RfsPermSN a Supervisor whose childSpecs name it):

So this is a bug on main: removing a class that a supervisor holds a spec for leaves a zombie child and a time bomb in the supervisor. It is out of scope for this ADR to fix, and the ADR neither worsens nor fixes it: the actor list already holds supervised children, so stop_class_actors and should_purge_module already see them. The only readers that gain members are allInstances and hot reload (see Migration Path). The fix belongs to its own issue; this ADR records the options so that issue is not decided by accident:

  1. Refuse removeFromSystem while any supervisor holds a child spec that names the class (a structured #beamtalk_error{} naming the supervisor). Smallest, safest, and the user decides.
  2. Stop through the supervisor: supervisor:terminate_child/2 then delete_child/2 for each spec naming the class, then delete the module. Removes the spec from a running supervisor, which silently changes its shape.
  3. Kill the supervisor first. Rejected: takes unrelated siblings down.

Recommendation for that issue: (1). Until it is fixed, whichever option this ADR adopts must not make stop_class_actors see fewer supervised children than today (that would trade the zombie for a dead actor with a live module, which is no better).

Prior Art

User Impact

Steelman Analysis

OptionStrongest case
SOperator: nothing moves; the synchronous call is proven flow control.
B/C (table)BEAM veteran: monitor-driven membership, O(class) reads, exact instanceCount, a consistent snapshot, the Elixir Registry shape. Tooling developer: the IDE lists stay O(actors).
D (no table)Language designer: nothing to keep consistent; the table cannot disagree with reality. Smalltalk purist: allInstances is a scan, as in Smalltalk. Operator: no singleton, no recovery path, the lowest start cost.
Partition-linkBEAM veteran: the known Elixir design.

Tension: tooling wants an index, operators want no shared state on the start path. D resolves it by putting the index in the subscriber; C keeps it in the runtime.

Alternatives Considered

Periodic reaper as the primary cleanup (research/bt-4854-ets-reaper)

Caller-side inserts, no monitor per actor, a reaper walks the pid index. 16a 11.8 / 8.3 µs. Rejected as the primary mechanism: a pass walks every live row, 346 ms per pass at 1M rows (26.8 ms at 100k), so a fixed 250 ms period costs more than a core at about 1M actors and the period has to grow with the table, pushing the stale window to seconds; dead rows peak at kills × period (63k with exit(kill)); a periodic sweep of live state is not the OTP convention, where the runtime reports death.

Partition processes with links (research/bt-4854-partition-link)

Rejected: a partition crash kills its actors (143 of 400 measured); the link is visible in process_info(links) and, for a trapping actor, in onLinkedExit:reason:; 16a at +S 4 was unstable (14.0–42.6 µs); the actor must link from its own init/1, leaving a window that needs a sweep anyway. No BIF sets a monitor on behalf of another process, so a monitor variant with no start-path message does not exist.

Sharded registries (research/bt-4854-sharded)

No gain (16a 38.7 µs at +S 4): the synchronous round trip stays on the path.

Lazy batch (research/bt-4854-lazy-batch)

Pending buffer drained on a timer and before enumeration. Same start cost; readers need a flush; the buffer reached 680k rows (168 MB) under an async flood. C is this design with the rows inserted at once and the bound added.

Keep membership as it is today

The decision of this ADR bundles four separable things: registration off the start path; merging the two registries; widening allInstances and hot reload to supervised and named actors; moving rare readers to scans. All the measured gain comes from the first. One could keep membership as it is today (for example a via column on a table, set by the generated spawn, which allInstances and hot reload filter on) and take the widening as its own decision once the supervision interplay above is settled. It is rejected here only because it keeps the table and the two-source problem it exists to remove; if the owner wants the widening separated, that is C with a via column, and the hazard analysis above decides the second step.

Register only when a workspace is attached

Not prototyped. Breaks Node current actors and hot reload under plain beamtalk run and beamtalk test, and adds a mode whose meaning differs by boot context (docs/beamtalk-principles.md §4). D's optional subscriber-held index is not this: no runtime state depends on it.

Consequences

Positive

Negative

Neutral

Migration Path

Observable changes and what to do about them:

Implementation Tracking

Epic: BT-4943. Status: Planned.

OrderIssueWhat
1BT-4944scan reader module (additive)
1BT-4945should_purge_module via the code server (independent)
2BT-4947runtime readers on the scan: allInstances, hot reload, stop_class_actors, Node actors, trace_store
2BT-4949the class travels with sends (touches beamtalk_actor.erl; after BT-4871)
2BT-4950workspace actor lists on the scan and the ActorSpawned feed
3BT-4951remove the registrations from the start path (touches beamtalk_actor.erl; after BT-4937 and BT-4871; not merged until BT-4952 reports)
4BT-4952owner re-measures rows 16a/16c/16d at +S 18, tracing off (condition b)
5BT-4953delete the dead modules, amend the earlier ADRs
6BT-4954follow-up in beamtalk-buccaneer (filed, not started)

Related bugs found on the way: BT-4936 (removeFromSystem leaves a zombie child), BT-4937 (compiled actors never record a start time). BT-4932 (the rest of the start path) comes after the epic. The branch bt-4854-registry-impl holds a WIP of the dropped insert-then-monitor design and is not to be reused.

Implementation

After acceptance, one PR for the chosen option, plus a small PR per behaviour fix found on the way. For D:

  1. Plumb the class: async_send_known, native-facade _known call, beamtalk_reactive_subprocess; the remaining bare-pid sites read the marker.
  2. Generated init/1 writes '$beamtalk_actor_started_at'; actor_started_at/1 is unchanged.
  3. Scan reader in one runtime module (beamtalk_actor_scan or similar) used by allInstances, hot reload (one scan per reload, grouped), stop_class_actors, the actor lists, trace_store bottlenecks, actor_health; '$initial_call' match for native actors.
  4. should_purge_module answers with code:soft_purge/1 / erlang:check_process_code/2.
  5. Remove register from generated spawn* and the native facade, track_spawned, register_spawned, beamtalk_object_instances, beamtalk_repl_actors' state; the actor_spawned_hook for beamtalk_workspace_meta moves to an announcement subscriber. Update docs/development/surface-parity.md for the lists.
  6. Tests: a reader test per row of the reader table; a scan-cost guard; native actors in allInstances; a CI test that a spawn flood leaves no registry state.
  7. Measurements as the acceptance conditions; list in the PR what to re-measure on the maintainer's machine at +S 18.

Follow-up issue (filed, not done here): beamtalk-buccaneer makes HTTP/2 streams actors again, with 133.6k req/s as the figure to beat.

Consequences for earlier ADRs

ADRAmendment made by the implementation PR
0005"allInstances tracks actors via ETS … process monitors" becomes a scan of the process marker (D), or batched monitors (C); supervised and named actors are included.
0009beamtalk_object_instances / beamtalk_repl_actors leave the supervision tree (D) or become a view (C).
0069The class lookup description (a bag, ets:match by pid) is already stale; replace with: the class travels with the reference, a bare pid reads the marker.
0079The '$beamtalk_actor' marker was introduced for identification and all_registered/0 filtering, not enumeration; it now also backs the scan. '$beamtalk_actor_started_at' is written by generated init/1.
0093ActorSpawned / ActorStopped unchanged; they become the only incremental feed, and the source of actor_spawned_hook.
0123Hot-reload migration enumerates by one scan and includes supervised actors; a failed migration leaves a supervised actor suspended and a supervisor shutdown waits out its timeout.
0126local_actors/0, local_actor_at/1 become scans; the unknown class note for a missing row goes away; actor_started_at/1 returns values for compiled actors.
0129Node current actors "backed by beamtalk_actor_registry" (the process beamtalk_repl_actors) becomes a scan.
0010, 0087Supervision tree / start order lists beamtalk_object_instances; amendment note only.
docs/beamtalk-ddd-model.md ADR-5"ETS table with process monitors" becomes "process marker, enumerated on demand" (D).

References