Commit 82becc5d
82becc5d6d21Merged
Ori Pekelman <[email protected]> · 5 months ago · 14 files · +2 added · ~12 modified
Description
Ractor Milestone 6: Ractor::Port (shared channel)
Add Ractor::Port -- a process-shared, mutex+condvar FIFO message channel not bound to any one Ractor, so producers and consumers on different Ractors rendezvous through it. API: Ractor::Port.new / port.send(v) / port.receive / port.close (+ << alias).
Unlike a message value, a Port crosses the boundary BY REFERENCE: the codec serializes the channel pointer (SPR_PORT) rather than deep-copying, and the struct is registry-owned for the process lifetime so the shared pointer never dangles. SP_BUILTIN_RACTOR_PORT is added to sp_mark_rbval's skip list (like FOREIGN_PTR) since a port is not a GC allocation.
Compiler: TY_RACTOR_PORT; the namespaced constant Ractor::Port recognised in the analyzer + codegen; Ractor::Port.new and port send/receive/close dispatch; emit_boxed / emit_boxed_text learn the port boxing. A Ractor::Port passed as a spawn argument keeps its static type (the analyzer propagates TY_RACTOR_PORT from the arg to the block param, since ports cross by reference) so port methods dispatch on the receiver inside the body.
Full suite 932 pass / 0 fail / 0 error; port demo (two-job worker + string round-trip through ports passed as spawn args) is GC-verify-, valgrind memcheck-/helgrind-clean. Adds test/ractor_port.rb.
Two documented sharp edges: port.send("literal") is parsed as a reflective
send (use a variable), and port << v can be mis-inferred as an Array push
(use port.send) -- both noted in docs/RACTOR-RFC.md.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 82becc5d6d21
- Parent
- 02d79dc13e12
docs/RACTOR-RFC.mdmodifiedView file
# RFC: A Minimal Ractor for SpinelStatus: **experimental / Milestone 3** (working; ported to the C compiler in`src/`; message codec spans immediates/Symbol/String/Array/Hash; Ractor.newspawn arguments). This document is both the designStatus: **experimental / Milestone 6** (working; ported to the C compiler in`src/`). Deep-copy message codec spans immediates / Symbol / String / Array /Hash / user-objects; `Ractor.new` spawn arguments; compile-time `@@cvar`/`$gvar`isolation; and a shared `Ractor::Port` channel. This document is both the designrationale and the record of what currently ships. It proposes whether tograduate to a full implementation (see *Path forward*).## Path forward (if this graduates)Done since the original RFC: the codec now spans strings → symbols → arrays →hashes (M2/M3b), the mailbox is an unbounded FIFO, and `Ractor.new(args){|p|}`spawn arguments are deep-copied in (M3a). Remaining, in rough dependency order:extend the codec to **user objects / structs** (the last big container gap);replay class-body `@@cvar`/`$gvar` initializers in a child (they are `__thread`,hence empty in a child today); shareable-by-value frozen captures; and a`Ractor::Port`-style multi-consumer surface. None change the core partitionabove — they extend it.Done since the original RFC: the deep-copy codec now spans strings → symbols →arrays → hashes → **user objects/structs** (M2/M3b/M4, recursively); the mailboxis an unbounded FIFO; `Ractor.new(args){|p|}` spawn arguments are deep-copied in(M3a); `@@cvar`/`$gvar` access inside a Ractor is rejected at compile time (M5);and **`Ractor::Port`** provides a process-shared channel (`new`/`send`/`receive`/`close`), passed across the boundary *by reference* so producers and consumerson different Ractors rendezvous through it (M6). Remaining: shareable-by-valuefrozen captures, object-graph cycles in the codec (acyclic assumed today), andthe Ruby-3.5 Ractor-as-Port API reshaping. None change the core partition.### API sharp edges (operator/method overload collisions)- **`Ractor#send` / `Ractor::Port#send` with a bare String/Symbol *literal*** is parsed as a reflective send (`x.send("m")` → `x.m`, a whole-program parser behaviour). Send via `x.send(var)` or an Integer literal; `x.send("lit")` is reflective.- **`port << v`** can be mis-inferred as an Array push (the `<<` push-promotion 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_util.cmodifiedView file
Loading diff…
src/types.cmodifiedView file
Loading diff…
src/types.hmodifiedView file
Loading diff…
test/ractor_port.rbaddedView file
Loading diff…
test/ractor_port.rb.expectedaddedView file
Loading diff…