ADR 0142: The Class Process Only Owns Class State — Stateless Class Methods Run in the Caller

Status

Implemented (2026-10-30); accepted 2026-10-29. Epic BT-4297.

Amended by ADR 0153 (2026-11-25), the rule that decides where a class method runs. A class has no state, so "the class or any ancestor declares class variables" is no longer a condition. A class method runs in the caller unless its selector is process-bound. performLocally:withArguments: is removed; perform:withArguments: has its behaviour.

Superseded in part by ADR 0152 (2026-11-24), the BT-4307 resolution. The resolution that an ancestor's class-side extension counts as that ancestor's method, with the owner memo declining when one answers first, no longer applies: class-side extensions are removed, so the owner memo has no extension verdict. A runtime class-method fun installed through the class still counts as the class's method.

Implemented note (2026-10-30). What shipped differs from the text below in these ways:

  • Rule 2 is chain-wide (deviation from §1). A class keeps its class process when it or any class in its superclass chain declares class variables, not only when the resolved method's defining class does. A subclass that declares none of its own holds its ancestors' class variables in its own process, and a method it defines itself reaches them through a class-side self send, which would raise class_state_unreachable in the caller. The runtime rule is beamtalk_class_metadata:chain_has_class_variables/1, the compile-time rule ClassHierarchy::runs_class_method_in_caller (Rust), and the derived direct_call_ok/2 flag folds the class-variable facts over the whole chain (BT-4300, BT-4301).
  • The guarded static call also checks erlang:function_exported/3, so a method removed by removeSelector: or a reload takes the class_send fallback instead of raising undef, and gets the class pid by erlang:whereis/1 of the compile-time registered name. The direct call stays in tail position. Class-side self foo and explicit own-class sends go through class_self_direct_ok/4, sealed or not; no unguarded class-side direct call remains. The class_var_abi is 3 (the class-method calling convention changed); modules compiled for 2 are refused at load.
  • Flag erasure is as amended in §2 below: a class's own derived inputs are raised before its change is visible, its descendants' flags are erased just after.
  • Benchmarks (benchmarks/vs-elixir/results/bench-bt4300.md, bench-bt4301.md). Rows 3b and 3c went from about 200 reductions through the class process to about 24 with the in-caller dynamic path (BT-4300), then to 7.03 with the guarded static call (BT-4301), meeting the ≤ 10 gate. Row 3a misses its ≤ 3 target: it went from 2 to 7.03, the cost of the guard, the function_exported/3 check and the real class reference that the unguarded nil call skipped. The miss was accepted for merge; BT-4316 brought it back to the target with one persistent_term read per static call target (§2, amended).
  • performLocally:withArguments: now resolves through the class-method chain from the receiving class (an inherited selector is found, a subclass override wins), but applies compiled methods only: a selector whose nearest definition is an ADR 0084 runtime fun raises does_not_understand.
  • class-process-ffi now also fires in classes that declare classState: (they used to be exempt), with a hint to move the call into an instance method or an Actor.
  • Class-literal signal sends (BT-4270). BT-4115's unguarded caller-side raise for Error signal: m was the last unguarded class-side verdict. It now sits behind a static call key of its own, {caller_safe_raise, Primitive, DefiningModule, Fun, ReceiverTag}, installed by static_call_target/4 under the same conditions as a direct-call key (extended to an inherited owner without a runtime fun) plus the defining module's metadata mapping the selector to that primitive, and erased by the same writers. Row 8b stays at about 89 reductions (86 before), where the plain dynamic path cost 155 (results/bench-bt4270.md).
  • Constant class methods (BT-4363, BT-4504). A class-literal send of a class-side method whose whole body is one scalar literal (Errors protocolError with class protocolError => 1; since BT-4504 also a float, string, symbol, character, nil, true or false, carried as the term it compiles to and certified with =:=) answers the literal in place behind a static call key of its own, {constant_value, Value, Module, Fun, ReceiverTag}. static_call_target/4 installs it under the same conditions as a direct-call key, plus the class's published metadata row mapping the selector to that value (constant_class_methods, generated from the method source and published on registration and reload, not read from the module, whose old code is still current during a reload's -on_load); the same writers erase it, as does a change to that row, so an extension, a runtime fun, removeSelector: or a reload that changes the body is honoured. Sealing plays no part. The send costs 1 reduction instead of 3 for the guarded direct call.
  • Resolved (BT-4307): an ancestor's class-side extension counts as that ancestor's method on every class-side path; the owner memo declines when one answers first, so the class process runs it.
  • Explicit own-class sends (BT-4317, amended 2026-11-04): see the amendment at the end of §2.

Context

Problem

Every class has a gen_server, the class process. Today a class method runs inside that process unless three conditions all hold:

These are ADR 0129 Phase 0b's rules.

Everything else, open classes and non-class sealed methods alike, is a gen_server:call. The benchmarks/vs-elixir suite measures it:

RowSendBeamtalkElixir
3aclass sealed method of a sealed class2 red / 0 w, 0.9 ns2 red, 0.8 ns
3bclass method, sealed class, method not class sealed145 red / 444 w, 2.3 µs2.9 ns
3cclass method on an open class146 red / 480 w, 2.6 µs2.8 ns

The cost is not only speed. The class process is a node-wide serialisation point: every caller of every class method of that class queues on one process. It also makes class methods behave unlike the functions they look like:

What the class process actually protects is class variables. ADR 0130 made the class process their home for the duration of an invocation. A class with no class variables has nothing for the process to own.

For Catalina, class-side sends are about 1% of per-message CPU (the echo_c10_64B profile recorded in epic BT-4291), so this ADR is mostly about correctness and contention, not throughput.

Current state

Two mechanisms put class methods in the caller today:

  1. ADR 0129 Phase 0b direct calls, plus BT-4099 constructors.
    • Rust computes eligibility (ClassInfo::is_direct_call_eligible: sealed + no class variables + class sealed + not a spawn constructor) and bakes it into __beamtalk_meta/0 as direct_class_methods.
    • Static call sites call Mod:class_sel(nil, …) unguarded. Dynamic class sends consult the baked table (lookup_direct_call/3).
    • Trusting a compile-time verdict without a guard is the source of four known caveats:
      • BT-4126: stale descendants after a hot patch;
      • BT-4133: class-side extensions are bypassed;
      • BT-4134: a reparent leaves a stale verdict;
      • BT-4037: callers pass nil as ClassSelf.
  2. Open class-side self-sends (BT-3700).
    • class_self_direct_ok/4 reads one persistent_term flag per send, maintained by beamtalk_class_shadow_flags.
    • The flag is erased under a per-tag lock before an extension or ADR 0084 runtime fun becomes visible.
    • This is a guarded direct call (ADR 0025 amendment, 2026-10-29), but it is used only for self-sends inside an open class.

Constraints

Decision

1. A class method runs in the caller unless the class needs its process

A class method runs in the caller when all of these hold:

  1. The receiving class has no class variables, and is not shadowed. This is checked at run time by the per-class flag direct_call_ok(C). It is the existing beamtalk_class_shadow_flags derived flag, which gains one input. It is true exactly when:

    • C has no class variables (classState:), which is the new input;
    • no ADR 0084 runtime class-method fun is installed on C;
    • no class-side extension shadows a compiled method of C;
    • the class has finished bootstrap (mark_ready).
  2. The class defining the resolved method also has no class variables. (As implemented, this is taken over the receiving class's whole superclass chain; see the Implemented note under Status.)

    • A class method touches only its own class's variables, through the receiver's class-process home (ADR 0130).
    • Take a method defined on a stateful Base and sent to a stateless Sub. Today it runs in Sub's process and raises undeclared_class_variable; run in the caller, it would raise class_state_unreachable instead. So it stays in the class process, and its error does not change.
    • Methods that run in the caller install no class-variable home, so ADR 0130 §1's no-nesting rule is untouched.
  3. The selector is not process-bound. These keep their current routing:

    • new, new:, startLink and startLink: (ADR 0129). Value constructors keep a direct path (BT-4099), behind the same guard (amended, BT-4409: see §2).
    • spawn and spawnWith:, which have dedicated clauses in beamtalk_class_dispatch.
    • Supervisor supervise/spawn*, which are routed before class_send_plain.
    • TestCase run selectors, which answer test_spawn and need the gen_server's noreply.

    Rust owns this list, in class_info.rs next to is_direct_call_eligible. It reaches the runtime as generated metadata. The class-state-abroad lint and the migration lint (§4) read the same list.

sealed and class sealed play no part. A class that needs a serialising home declares class state (classState:) or delegates to an Actor, the idiomatic BEAM owner. No new keyword is added.

2. How sends reach it

Dynamic class sends. These are sends through a variable, perform:, classNamed:, a class passed as an argument, or an Erlang caller of class_send. They all go through beamtalk_class_dispatch:class_send_plain/3.

Static class sends with a literal receiver (Point origin, or class-side self foo), where the selector is one that C itself defines, compile to:

case call 'beamtalk_class_shadow_flags':'direct_call_ok'('Point class', 'Point') of
  <'true'> -> call 'bt@app@point':'class_origin'(PointRef)
  <_>      -> <today's class_send>
end

Amended (BT-4317, 2026-11-04): a class literal always denotes that class. Inside a class method of Base, an explicit own-class send Base foo binds self to Base, as the same send does anywhere else and as Smalltalk does; only self foo late-binds to the receiver. It used to run with the receiver as self when the method was inherited by a subclass C (self name answered #C, class variables were C's and Base new built a C), a leftover of the own-class deadlock workaround. The send now cases on the running receiver's tag: when it is Base, the own-class direct path above runs it in the current process (for a stateful chain that is Base's own class process, which a class_send would deadlock on); otherwise it is the ordinary literal class send to Base (the guarded static call or class_send), which runs in the caller for a stateless chain and in Base's class process for a stateful one, never in the process already running. Rejected: keeping "receiver as self" (it contradicts what a class literal evaluates to), and passing Base's reference to a direct call in the current process (for a stateful chain Base's class variables live in Base's process, not C's). Consequence: if a Base class method running in Base's process sends to C, and C's inherited method then sends Base bar, that is a cross-process cycle between two stateful classes, like any other.

3. What moves into the caller

4. Migration lint

The class-process-ffi lint is inverted into class-method-process-resource. It warns on FFI in a class method of a stateless class that does either of these:

The message names the fix: an Actor, or classState:. The lint reuses the existing validator and its curated table.

Example

Object subclass: Paths
  class join: a :: String with: b :: String -> String => a ++ "/" ++ b
  class each: items :: List do: aBlock -> Nil =>
    items do: [:x | aBlock value: x]
    nil
Paths join: "a" with: "b"                       // => "a/b", computed in the caller: no class-process hop
Paths each: #(1, 2) do: [:x | Paths join: "x" with: x printString]
                                                // => nil; the block runs in the caller, so re-entering Paths is fine
                                                // (today: dispatch_error)

A class with class state keeps its process:

Object subclass: Counter
  classState: count :: Integer = 0
  class bump -> Integer => self.count := self.count + 1
Counter bump   // => 1, in Counter's class process, as today

Error behaviour

There are no new errors. Every error a class method raises today is raised with the same value. The one difference is where: an exit inside a stateless class method now exits the caller, as an Erlang function call would, instead of being relayed back from the class process.

Prior Art

User Impact

Steelman Analysis

A. Only sealed, stateless classes run class methods in the caller

Why not:

B. Status quo (ADR 0129's rule)

Why not:

C. Unguarded trust in compile-time verdicts, extended to every stateless class

Why not: it is the source of the four staleness caveats. A persistent_term read costs about one reduction.

Tension points

Alternatives Considered

Only sealed, stateless classes run in the caller

Considered with the project owner and rejected: process placement should follow class state, not subclassing (steelman A).

Status quo

Rejected (steelman B).

Unguarded compile-time verdicts

Rejected (steelman C).

Consequences

Positive

Negative

Neutral

Implementation

All phases sit under epic BT-4297, after BT-4292, the differential harness of epic BT-4291.

Phase 0 — differential rows (S). Add these class-side rows to BT-4292's differential fixture:

Results and error shapes must be identical. Add vs-elixir rows for an open-class class method.

Phase 1 — runtime (M).

Phase 2 — codegen and lint (M).

Phase 3 — docs (S).

Implementation Tracking

Epic: BT-4297

Issues:

Status: Done (2026-10-30). Follow-ups: BT-4316 (row 3a), BT-4317 (done, amendment in §2), BT-4307.

Migration Path

This ADR changes behaviour only for class methods of classes without class variables.

References