Commit e25d8e3e
e25d8e3e2050Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 21 files · +8 added · ~13 modified
Description
Merge pull request #1448 from OriPekelman/rfc-minimal-ractor
Ractor Milestones 3–6: spawn args, Hash/object messages, isolation, Ractor::Port
Details
- Commit
- e25d8e3e2050
- Parent
- 6edcb4097963
- Merge parent
- 82becc5d6d21
docs/RACTOR-RFC.mdmodifiedView file
@@ -1,7 +1,9 @@
11
# RFC: A Minimal Ractor for Spinel22
3-
Status: **experimental / Milestone 2** (working; ported to the C compiler in4-
`src/`). This document is both the design3+
Status: **experimental / Milestone 6** (working; ported to the C compiler in4+
`src/`). Deep-copy message codec spans immediates / Symbol / String / Array /5+
Hash / user-objects; `Ractor.new` spawn arguments; compile-time `@@cvar`/`$gvar`6+
isolation; and a shared `Ractor::Port` channel. This document is both the design57
rationale and the record of what currently ships. It proposes whether to68
graduate to a full implementation (see *Path forward*).79
@@ -119,20 +121,23 @@
119121
120122
## Deliberate divergences (where "minimal" buys simplicity)121123
122-
1. **Value codec covers immediates, Symbol, String, and Arrays** (poly + typed123-
int/float/string), deep-copied through a heap-neutral malloc'd blob124-
(`sp_ractor_serialize`/`_deserialize`, installed via a hook because the125-
rebuild allocators are static to the generated TU). Hashes, plain objects,126-
Procs, Fibers, and IO raise `Ractor::Error` at the boundary — extending the127-
codec to those (and to nested objects) is the main follow-up. Symbols travel128-
by name and are re-interned in the receiver's per-Ractor table.124+
1. **Value codec covers immediates, Symbol, String, Arrays, and Hashes** (poly125+
+ typed variants), deep-copied recursively through a heap-neutral malloc'd126+
blob (`sp_ractor_serialize`/`_deserialize`, installed via a hook because the127+
rebuild allocators are static to the generated TU). Plain objects, Procs,128+
Fibers, and IO raise `Ractor::Error` at the boundary — extending the codec to129+
user objects is the main follow-up. Symbols travel by name and are130+
re-interned in the receiver's per-Ractor table; hashes are normalised to a131+
poly hash on rebuild. The same codec seeds `Ractor.new(args) { |params| }`132+
spawn arguments.129133
2. **FIFO mailbox / outgoing queue** (mutex + condvar, grows on demand), so a130134
Ractor may `receive` several messages and a sender need not block per slot.131-
3. **`@@cvars` / `$globals` are `__thread`** → isolated per Ractor, rather than132-
CRuby's "shared, raise on cross-Ractor mutation". Simpler and safe, but a133-
real semantic difference: a class-body cvar initializer has not run in a134-
child Ractor unless re-executed there. Programs that rely on cross-Ractor135-
cvar/gvar sharing are out of scope for this milestone.135+
3. **`@@cvars` / `$globals` accessed inside a Ractor are rejected at compile136+
time** with `Ractor::IsolationError`, mirroring CRuby (which raises on137+
cross-Ractor class-variable / global access). Special, thread-local or138+
read-only globals (`$~`, `$stdout`, regex/status specials — not user-declared)139+
are allowed. The globals themselves remain `__thread` in the runtime; the140+
compile-time check just prevents the silent-empty-value footgun.136141
4. **`Ractor#send` with a bare String/Symbol *literal*** is parsed as a137142
reflective send (`recv.send("m")` → `recv.m`, a whole-program parser138143
behaviour), so send messages via `r << v` or a local (`r.send(v)`); literals@@ -162,9 +167,21 @@
162167
163168
## Path forward (if this graduates)164169
165-
In rough dependency order: extend `sp_deep_copy` to strings → arrays → hashes →166-
objects (symbols travel by name, re-interned on receive); make the mailbox167-
unbounded; thread-local-ize `@@cvars`/`$globals` with a class-body initializer168-
replay on the child; add spawn args and shareable-by-value capture; and a169-
`Ractor::Port`-style multi-consumer surface. None of these change the core170-
partition above — they extend it.170+
Done since the original RFC: the deep-copy codec now spans strings → symbols →171+
arrays → hashes → **user objects/structs** (M2/M3b/M4, recursively); the mailbox172+
is an unbounded FIFO; `Ractor.new(args){|p|}` spawn arguments are deep-copied in173+
(M3a); `@@cvar`/`$gvar` access inside a Ractor is rejected at compile time (M5);174+
and **`Ractor::Port`** provides a process-shared channel (`new`/`send`/`receive`/175+
`close`), passed across the boundary *by reference* so producers and consumers176+
on different Ractors rendezvous through it (M6). Remaining: shareable-by-value177+
frozen captures, object-graph cycles in the codec (acyclic assumed today), and178+
the Ruby-3.5 Ractor-as-Port API reshaping. None change the core partition.179+
180+
### API sharp edges (operator/method overload collisions)181+
182+
- **`Ractor#send` / `Ractor::Port#send` with a bare String/Symbol *literal*** is183+
parsed as a reflective send (`x.send("m")` → `x.m`, a whole-program parser184+
behaviour). Send via `x.send(var)` or an Integer literal; `x.send("lit")` is185+
reflective.186+
- **`port << v`** can be mis-inferred as an Array push (the `<<` push-promotion187+
heuristic types the receiver as an array). Use `port.send(v)` for ports.lib/sp_gc.hmodifiedView file
Loading diff…
lib/sp_ractor.cmodifiedView file
Loading diff…
lib/sp_ractor.hmodifiedView file
Loading diff…
lib/sp_runtime.hmodifiedView file
Loading diff…
src/analyze_infer.cmodifiedView file
Loading diff…
src/analyze_pass.cmodifiedView file
Loading diff…
src/codegen.cmodifiedView file
Loading diff…
src/codegen_call.cmodifiedView file
Loading diff…
src/codegen_internal.hmodifiedView file
Loading diff…
src/codegen_util.cmodifiedView file
Loading diff…
src/types.cmodifiedView file
Loading diff…
src/types.hmodifiedView file
Loading diff…
test/ractor_hash.rbaddedView file
Loading diff…
test/ractor_hash.rb.expectedaddedView file
Loading diff…
test/ractor_object.rbaddedView file
Loading diff…
test/ractor_object.rb.expectedaddedView file
Loading diff…
test/ractor_port.rbaddedView file
Loading diff…
test/ractor_port.rb.expectedaddedView file
Loading diff…
test/ractor_spawn.rbaddedView file
Loading diff…
test/ractor_spawn.rb.expectedaddedView file
Loading diff…