ADR-threads-v1: There Is One Build; How Many Threads Run the User’s Code Is user-pool, and Each Value’s Representation Comes from the Crossing Analysis

Status: Accepted Date: December 1, 2026 Answers: whether the build switch user-parallelism stays, and what decides a value’s representation without it (#3027) Supersedes / Related: ADR-037 D2, D3 and D5 (the switch, and where it lives): superseded; D4 and D6 lose the switch; ADR-312 D10 (at no every count is plain): superseded by D3 and D6 here; ADR-037 D7 and D8 (a count is plain only where the analysis proves nothing crosses; no way to ask for the cheaper count): unchanged, now the only rule; ADR-281 D1-D4 (a lock is Local or Crossing per value, and both report re-entry alike): unchanged; ADR-tether-v1 D28 (a keep’s count is decided by the same walk): unchanged; ADR-055 D4 (the switch decides how many threads the executor has): now user-pool decides it; ADR-119 D1 (a bare-metal target pins the switch to no): now it pins user-pool to one; ADR-363 (the toolchain’s package lowered at yes): moot; ADR-372 D7 (at no a parallel fold is its sequential fold): now at one worker; ADR-async-v7 D20 (where tasks run in parallel): now at more than one worker; ADR-002 D4 and Part III 13.2’s rule that std’s Nikaia half lowers the same at every build option: reworded by D5

1. Question

Should the build switch user-parallelism (no: nothing the user wrote runs concurrently; yes: it may) stay, or should there be one build whose values each get their representation from the crossing analysis, with the number of threads chosen at run time?

The switch makes two programs of one source: a second dimension of the build cache, a question every user of a library has to answer, and a program built at no that cannot use a second core without a rebuild. What it buys at no is cheaper code: every count plain, every lock a borrow flag, no pool and one deterministic order. The analysis that gives a value that cheaper code at yes wherever nothing crosses already exists (ADR-037 D7, ADR-281), so the question is whether the switch still earns its cost, and it had to be decided before Pool start for tasks in a parallel build (#3011) gave the switch a further meaning at no.

2. How others do it

Language / tool Its answer What it costs there Source
Rust Rc or Arc, RefCell or Mutex, chosen by hand per type the programmer decides, and a library picks for every caller the standard library documentation
Swift every count atomic an atomic per retain and release, in every program from memory, not checked; biased counting was studied for it (Choi et al., PACT 2018)
CPython without the GIL biased counting: non-atomic for the owning thread, atomic otherwise a check on every count operation PEP 703
Lean 4 an object is marked shared when it reaches another thread; until then its count is non-atomic a check on every count operation from memory, not checked
Go, OCaml 5 a garbage collector the question does not arise —

What does not transfer: the collectors, and Rust’s choice by hand, which ADR-037 D8 refuses. Lean’s is the closest: it makes the same split per object, but at run time with a check on every operation. Here the split is made per allocation class at compile time, so the plain case carries no check.

3. Options, weighed

The source a developer writes is the same under every option; what differs is what decides the representation and how many threads run.

let hits = Shared[Locked[i32]](0)
task::scope(|s| {
    for _ in 0..4 { s.spawn(|| hits.update(|n| n + 1)) }
})

Option 0 — Keep the switch

[build] user-parallelism stays a project key. no stays as cheap as it is; the two programs per source, the cache dimension and the rebuild stay too.

Option A — The switch for the root project only

Libraries are always built open and the root project decides. This is close to today already, since libraries are source built at the root’s setting (ADR-286 D15); the switch moves and is still there.

Option B — One build with cheaper primitives everywhere

Biased counts and biased locks: a non-atomic path for the owning thread, an atomic path otherwise. No analysis is needed, but every operation pays a check: counted with Callgrind, +8 instructions per clone-and-drop pair over Rc and +6 per borrow over RefCell (#3027, Measured).

Option C — One build, representation per value, plus a second whole-program instantiation for one thread

As D below, and the compiler also emits the whole program instantiated with nothing crossing; the runtime runs that copy when it finds user-pool = 1. At one thread the code is exactly today’s no, but every program carries up to twice the code of the functions that touch a crossing class, whether or not it is ever run at one thread.

Option D — One build, representation per value

The switch goes. Each allocation class gets the representation the crossing analysis proves for it: plain count and borrow-flag lock where nothing crosses, atomic count and crossing lock where something may. user-pool sets the number of threads at run time. What remains at one thread (an atomic on a value the program text lets cross, which at one thread cannot) is left to a later compile-time option.

Weighed

  D1 the special case pays, not the common one D2 the compiler works, not the developer D3 no cost for the common case D4 consistent with itself If it is wrong
0 every project answers a question only some need the developer chooses the build none at no; at yes as D two programs per source nothing to undo
A as 0, for root projects as 0 as 0 the switch moves, the split stays nothing to undo
B every operation pays for the crossing case the compiler does nothing +8 / +6 instructions per operation, everywhere one representation the check stays in every binary
C every binary pays the size of the one-thread copy the compiler does it none at run time; up to 2× code one build a second instantiation to take out of the emitter
D only values that may cross pay the compiler does it the plain code wherever nothing crosses one build, one rule per value a value wrongly proven not to cross is a race; the floor and the seeds (ADR-037 D7) already guard it

4. Decision

Option D (owner, 2026-12-01). 0 and A keep a switch whose work the analysis now does per value; B makes the common case pay on every operation; C makes every binary pay for the one-thread case, which is the special case (D1). The cost D accepts, the atomics left on values the text lets cross when only one thread runs, is the special case’s, and a later option takes it away (§6).

D1 — There is one build. The switch user-parallelism is removed.

The user’s code may always run on more than one thread; the checks have never read the switch (ADR-037 D4 and D6, ADR-005 §1 Group B), so no program’s verdict changes. [build] user-parallelism in nikaia.toml, --user-parallelism and NIKAIA_USER_PARALLELISM are gone, and so is the dimension of the build cache’s key. A manifest that still names the key is refused with a message that names user-pool.

D2 — How many threads run the user’s code is user-pool, at run time, one included.

user-pool in nikaia-runtime.toml (Part III 13.3b) is the number of workers, with 0 the threads the process may run on, as today. The pool starts at the first spawn that needs it, so a program that never spawns starts no worker. task::cores() is the pool’s size.

D3 — A value’s representation comes from the crossing analysis, and from nothing else.

A count is plain where the analysis proves that nothing crosses a thread with its value, and atomic everywhere else (ADR-037 D7, with D8’s refusal of a way to ask for the cheaper one); a lock is Local or Crossing by the same proof (ADR-281); a keep’s count likewise (ADR-tether-v1 D28). These rules held at yes and now hold for every program. ADR-312 D10, which made every count plain at no, has nothing left to apply to.

D4 — A function that receives both kinds is instantiated per kind, as a generic is.

Where a function is reached by a value that may cross and by one that may not, it gets one instance per representation, chosen at each call by what the argument’s class was proven to be, rather than both classes being merged to the atomic floor. A container or a dyn that holds values of both kinds is one class, and one element that may cross makes it a crossing class. A public signature stays at the floor (ADR-037 D8’s last row).

D5 — std is instantiated like the program.

std’s types and functions that hold a count or a lock take the representation as a type parameter, and rustc instantiates them in the program’s crate, so the compiled std in the cache stays one per machine. The rule that std’s Nikaia half lowers the same at every build option holds; what it now permits is a Shared or Locked in that half, whose representation is the parameter the analysis picks per value at the program’s use. The toolchain’s own package is no longer lowered at a setting of its own.

D6 — At one worker, a run keeps today’s no order.

At user-pool = 1 the executor is the program’s own thread: no worker thread starts, the tasks of a group interleave on it at their suspension points in the order no gave them (ADR-055 D4’s first row), and a parallel fold is its sequential fold (ADR-372 D7). A run at one worker is as deterministic as a run at no was. Only at more than one worker does a task run on the pool (ADR-async-v7 D20).

D7 — A target without threads runs one worker.

On a target without threads (a bare-metal target, ADR-119 D1; wasm32-unknown, ADR-037 D1) user-pool is one and cannot be raised; it was the switch’s no there before.

D8 — The program is not instantiated a second time for one thread.

No copy of the program with nothing crossing is emitted for the runtime to pick at start (Option C). A binary does not grow for the one-thread case.

5. Consequences

The work is carried by #3027 and the issues it lists.

6. What this does not decide

This site is open source. Improve this page.