refactor(runtime): consolidate spawn, streams, strings, and mutex patterns#279
Merged
refactor(runtime): consolidate spawn, streams, strings, and mutex patterns#279
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Runtime Consolidation
Reduces duplicated and complex tiered logic across the Hew runtime.
Changes
Phase 1 — MutexExt / RwLockExt / CondvarExt traits (
util.rs)lock_or_recover(),read_or_recover(),write_or_recover(),wait_or_recover(),wait_timeout_or_recover()methodsPhase 2 — Actor spawn consolidation (
actor.rs)ActorSpawnConfigstruct +spawn_actor_internal()helperPhase 3 — Stream transform deduplication (
stream.rs)stream_transform_entry()helper for shared C ABI entry logicnext()methods remain explicit (marshalling genuinely differs)Phase 4 — is_static_string unification (
string.rs)is_static_string()entry point calling platform-specificstatic_string_bounds()OnceLockOnceLockcaching (previously recomputed each call)Testing
-D warnings)#[no_mangle]exports