Skip to content

refactor(runtime): consolidate spawn, streams, strings, and mutex patterns#279

Merged
slepp merged 5 commits intomainfrom
refactor/runtime-consolidation
Mar 21, 2026
Merged

refactor(runtime): consolidate spawn, streams, strings, and mutex patterns#279
slepp merged 5 commits intomainfrom
refactor/runtime-consolidation

Conversation

@slepp
Copy link
Copy Markdown
Contributor

@slepp slepp commented Mar 21, 2026

Runtime Consolidation

Reduces duplicated and complex tiered logic across the Hew runtime.

Changes

Phase 1 — MutexExt / RwLockExt / CondvarExt traits (util.rs)

  • New lock_or_recover(), read_or_recover(), write_or_recover(), wait_or_recover(), wait_timeout_or_recover() methods
  • Replaced 124 instances of the 3-line match/Ok/Err poison-recovery pattern across 18 files
  • Net: −302 lines

Phase 2 — Actor spawn consolidation (actor.rs)

  • ActorSpawnConfig struct + spawn_actor_internal() helper
  • All 6 public spawn functions (3 native + 3 WASM) now build a config and delegate
  • Eliminated triplicated ~25-field struct initialisation
  • Net: −53 lines

Phase 3 — Stream transform deduplication (stream.rs)

  • stream_transform_entry() helper for shared C ABI entry logic
  • next() methods remain explicit (marshalling genuinely differs)

Phase 4 — is_static_string unification (string.rs)

  • Single is_static_string() entry point calling platform-specific static_string_bounds()
  • macOS: replaced hand-rolled AtomicUsize cache with OnceLock
  • Windows: added OnceLock caching (previously recomputed each call)
  • Net: −9 lines

Testing

  • All 527 E2E tests pass
  • All Rust unit tests pass
  • Clippy clean (-D warnings)
  • No function signature changes to #[no_mangle] exports

slepp added 5 commits March 20, 2026 23:43
Add MutexExt, RwLockExt, and CondvarExt traits in a new util module
that consolidate the pervasive match-lock-recover-from-poison pattern
into single method calls. The runtime deliberately recovers from
poisoned locks rather than panicking, because a panicked thread should
not cascade-crash independent actors.

Replaces 124 instances of the 3-line match/Ok/Err boilerplate across
18 files with one-line calls to lock_or_recover(), read_or_recover(),
write_or_recover(), wait_or_recover(), and wait_timeout_or_recover().
Extract ActorSpawnConfig struct and spawn_actor_internal() that handles
the ~25-field HewActor construction, ID generation, tracking, profiler
registration, and tracing. All six public spawn functions (3 native +
3 WASM) now build a config and delegate, eliminating triplicated struct
initialisation.

The internal helper has platform-specific variants: native uses
next_actor_id() and arena allocation; WASM uses plain serial IDs and
null arenas. Public function signatures are unchanged.
Extract define_map_stream! and define_filter_stream! macros that
generate the identical struct definitions, Send impls, and Drop impls
shared by the four stream transform adapters (map/filter × string/bytes).

Add stream_transform_entry() helper that consolidates the common
null-guard → consume-upstream → build-backing → wrap pattern used by
all four C ABI entry points. Each entry function is now a thin wrapper
that transmutes the callback and constructs its specific backing type.

The StreamBacking::next() implementations remain explicit since their
marshalling logic genuinely differs between variants.
Restructure the four platform-specific is_static_string() implementations
into a single entry point that delegates to static_string_bounds() helpers.
The bounds check (addr >= start && addr < end) is now shared across all
platforms.

Platform-specific changes:
- ELF: unchanged logic, now in static_string_bounds()
- macOS Mach-O: replace hand-rolled AtomicUsize cache with OnceLock
- Windows PE: add OnceLock caching (previously recomputed each call)
- WASM: returns (0, __heap_base) for the common bounds check
Use PoisonError::into_inner method references instead of closures in
util.rs. Add expect(needless_pass_by_value) for ActorSpawnConfig.
Add backtick escapes in stream macro doc comments.
@slepp slepp merged commit 93de2bc into main Mar 21, 2026
12 checks passed
@slepp slepp deleted the refactor/runtime-consolidation branch March 21, 2026 17:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant