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

@@ -1,8 +1,9 @@
11 # RFC: A Minimal Ractor for Spinel
22
3-Status: **experimental / Milestone 3** (working; ported to the C compiler in
4-`src/`; message codec spans immediates/Symbol/String/Array/Hash; Ractor.new
5-spawn arguments). This document is both the design
3+Status: **experimental / Milestone 6** (working; ported to the C compiler in
4+`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 design
67 rationale and the record of what currently ships. It proposes whether to
78 graduate to a full implementation (see *Path forward*).
89
@@ -166,11 +167,21 @@
166167
167168 ## Path forward (if this graduates)
168169
169-Done since the original RFC: the codec now spans strings → symbols → arrays →
170-hashes (M2/M3b), the mailbox is an unbounded FIFO, and `Ractor.new(args){|p|}`
171-spawn arguments are deep-copied in (M3a). Remaining, in rough dependency order:
172-extend the codec to **user objects / structs** (the last big container gap);
173-replay class-body `@@cvar`/`$gvar` initializers in a child (they are `__thread`,
174-hence empty in a child today); shareable-by-value frozen captures; and a
175-`Ractor::Port`-style multi-consumer surface. None change the core partition
176-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 mailbox
172+is an unbounded FIFO; `Ractor.new(args){|p|}` spawn arguments are deep-copied in
173+(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 consumers
176+on different Ractors rendezvous through it (M6). Remaining: shareable-by-value
177+frozen captures, object-graph cycles in the codec (acyclic assumed today), and
178+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*** is
183+ parsed as a reflective send (`x.send("m")` → `x.m`, a whole-program parser
184+ behaviour). Send via `x.send(var)` or an Integer literal; `x.send("lit")` is
185+ reflective.
186+- **`port << v`** can be mis-inferred as an Array push (the `<<` push-promotion
187+ heuristic types the receiver as an array). Use `port.send(v)` for ports.