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) returnnil.Fiber[:k] = vwrites to the current fiber's storage.Fiber.current[:k]andFiber.current[:k] = vare aliases.- A new fiber's storage is lazily NULL until its first
[]=write (matches MRI's lazy-init). - At
Fiber.newtime, 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 genericHash[Symbol, Object]. Covers the request-context-propagation use cases; string-keyed storage can be a follow-up. Fiber.current.storagegetter/setter intentionally not exposed — keeping the storage slot internal preserves the GC-scan story.
Implementation:
-
lib/sp_runtime.h(+12 LOC):sp_SymPolyHash *storagefield on both Windows and Unixsp_Fiberstructs (NULL when unallocated).sp_Fiber_scanwalks the storage Hash when non-NULL.sp_Fiber_newlazily inherits fromsp_fiber_current->storageviasp_SymPolyHash_dup(shallow snapshot).sp_Fiber_storage_getreturns nil on NULL storage; otherwisesp_SymPolyHash_get.sp_Fiber_storage_setlazily allocates the Hash on first write.
-
spinel_analyze.rb(+17 LOC):infer_call_typearm for[]on Fiber-typed receivers returnspoly. Placed right after the lambda[]arm so it pre-empts later constant-recv / name-based dispatches that would otherwise default toint. -
spinel_codegen.rb(+42 LOC):compile_call_method: emitsp_Fiber_storage_get(...)for the[]read shape (recv =Fiberconstant or:fiber-typed expr, one symbol arg).compile_mutating_call_stmt: emitsp_Fiber_storage_set(...)for the[]=write shape before falling through tocompile_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]andFiber[: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
**Fiber**: Cooperative concurrency via `ucontext_t`. `Fiber.new`,`Fiber#resume`, `Fiber.yield` with value passing. Captures freevariables via heap-promoted cells.variables via heap-promoted cells. Per-fiber storage via `Fiber[:k]`/ `Fiber[:k] = v` (and the `Fiber.current[:k]` aliases) — symbol-keyedpoly-valued, lazily allocated, shallow-snapshot inherited from theparent at `Fiber.new` time.**Memory**: Mark-and-sweep GC with size-segregated free lists, non-recursivemarking, and sticky mark bits. Small classes (≤8 scalar fields, nolib/sp_runtime.hmodifiedView file
Loading diff…
spinel_analyze.rbmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…
test/fiber_storage_basic.rbaddedView file
Loading diff…
test/fiber_storage_basic.rb.expectedaddedView file
Loading diff…
test/fiber_storage_current_alias.rbaddedView file
Loading diff…
test/fiber_storage_current_alias.rb.expectedaddedView file
Loading diff…
test/fiber_storage_inherits.rbaddedView file
Loading diff…
test/fiber_storage_inherits.rb.expectedaddedView file
Loading diff…
test/fiber_storage_persists.rbaddedView file
Loading diff…
test/fiber_storage_persists.rb.expectedaddedView file
Loading diff…
test/fiber_storage_siblings.rbaddedView file
Loading diff…
test/fiber_storage_siblings.rb.expectedaddedView file
Loading diff…