Commit b0d4fe90

b0d4fe90f9e9Merged

Sam Ruby <[email protected]> · 6 months ago · 14 files · +10 added · ~4 modified

Description

feat(analyze,codegen,runtime): Fiber[:k] / Fiber[:k]= per-fiber storage

Minimum-viable per-fiber context dictionary, matching MRI's Fiber[] / Fiber[]= surface. Resolves the gap discussed in #577.

API (matz's design preferences from the issue):

  • Fiber[:k] reads from the currently-running fiber's storage Hash; unset keys (and fibers whose storage hasn't been allocated yet) return nil.
  • Fiber[:k] = v writes to the current fiber's storage.
  • Fiber.current[:k] and Fiber.current[:k] = v are aliases.
  • A new fiber's storage is lazily NULL until its first []= write (matches MRI's lazy-init).
  • At Fiber.new time, the parent's storage (if any) is shallow-copied into the child. Subsequent writes on either side are independent.
  • Storage is narrowed to symbol keys + poly values (sp_SymPolyHash), rather than generic Hash[Symbol, Object]. Covers the request-context-propagation use cases; string-keyed storage can be a follow-up.
  • Fiber.current.storage getter/setter intentionally not exposed — keeping the storage slot internal preserves the GC-scan story.

Implementation:

  • lib/sp_runtime.h (+12 LOC):

    • sp_SymPolyHash *storage field on both Windows and Unix sp_Fiber structs (NULL when unallocated).
    • sp_Fiber_scan walks the storage Hash when non-NULL.
    • sp_Fiber_new lazily inherits from sp_fiber_current->storage via sp_SymPolyHash_dup (shallow snapshot).
    • sp_Fiber_storage_get returns nil on NULL storage; otherwise sp_SymPolyHash_get.
    • sp_Fiber_storage_set lazily allocates the Hash on first write.
  • spinel_analyze.rb (+17 LOC): infer_call_type arm for [] on Fiber-typed receivers returns poly. Placed right after the lambda [] arm so it pre-empts later constant-recv / name-based dispatches that would otherwise default to int.

  • spinel_codegen.rb (+42 LOC):

    • compile_call_method: emit sp_Fiber_storage_get(...) for the [] read shape (recv = Fiber constant or :fiber-typed expr, one symbol arg).
    • compile_mutating_call_stmt: emit sp_Fiber_storage_set(...) for the []= write shape before falling through to compile_bracket_assign.
  • README.md: one-sentence add to the Fiber paragraph documenting the new storage surface.

Tests (test/fiber_storage_*.rb, 5 files, ~50 LOC total):

  • basic: get/set round-trip; unset keys read as nil.
  • inherits: parent-snapshot copy at Fiber.new time.
  • siblings: independent storage between sibling fibers; neither leaks back to parent.
  • persists: storage survives Fiber.yield / Fiber.resume cycles.
  • current_alias: Fiber.current[:k] and Fiber[:k] read/write the same slot.

Full suite: 545 / 545 pass (540 baseline + 5 new). All existing fiber and FFI tests unaffected.

Closes #577.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
b0d4fe90f9e9
Parent
f0063046556d

README.mdmodifiedView file

@@ -228,7 +228,10 @@
228228
229229 **Fiber**: Cooperative concurrency via `ucontext_t`. `Fiber.new`,
230230 `Fiber#resume`, `Fiber.yield` with value passing. Captures free
231-variables via heap-promoted cells.
231+variables via heap-promoted cells. Per-fiber storage via `Fiber[:k]`
232+/ `Fiber[:k] = v` (and the `Fiber.current[:k]` aliases) — symbol-keyed
233+poly-valued, lazily allocated, shallow-snapshot inherited from the
234+parent at `Fiber.new` time.
232235
233236 **Memory**: Mark-and-sweep GC with size-segregated free lists, non-recursive
234237 marking, and sticky mark bits. Small classes (≤8 scalar fields, no