Skip to content

perf(rune): type-erase Runtime, Measurement, and Cx into non-generic RuntimeCore - #1192

Merged
Hona merged 1 commit into
devfrom
type-erase-runtime-core
Nov 18, 2026
Merged

Hona merged 1 commit into
devfrom
type-erase-runtime-core

Conversation

@Hona

@Hona Hona commented Nov 18, 2026

Copy link
Copy Markdown
Member

Type-erases Runtime<S>, Measurement<'_, S>, and Cx<S> into non-generic RuntimeCore, Measurement<'a>, and CxCore, removing ~22.5k lines of production code from per-state-type Rust monomorphization with zero public API breakage.

Why

Previously, almost all of crates/rune's runtime, commit/measurement, paint, selection, input, a11y, clipboard, and window sync logic lived inside impl<S> Runtime<S>, impl<S> Measurement<'_, S>, and impl<S> Cx<S>. Every distinct app state type S — in downstream apps, examples, and across every test state struct — re-monomorphized and LLVM-codegen'd ~22.5k lines of runtime MIR, dominating compile times, peak RAM, and .rlib/binary sizes.

Architecture

  • Shared-prefix #[repr(C)] layout (crates/rune/src/runtime.rs, crates/rune/src/runtime/init.rs):
    • runtime_core_fields! generates identical #[repr(C)] prefixes for RuntimeCore (244 non-S fields + 3 hoisted metadata fields: app_ime_comp, app_root, has_playground) and Runtime<S>, terminated by a #[repr(align(16))] pub(crate) struct CoreEnd zero-tail-padding sentinel.
    • All 28 pub fields stay directly accessible on Runtime<S>, preserving disjoint field borrows (&mut rt.text.atlas, &rt.instances) across internal call sites and external Compositor<S> apps.
    • Compile-time layout assertions (assert_prefix_layout::<S>()) verify identical field alignment/offsets, covered by runtime::prefix_layout_tests::shared_prefix_layout_holds_across_state_alignments across (), u8, u128, and #[repr(align(64))].
  • Non-generic CxCore & Measurement<'a> (crates/rune/src/runtime/cx.rs, crates/rune/src/runtime/driver.rs):
    • CxCore owns all 18 context fields and methods; Cx<S> is a zero-overhead #[inline(always)] wrapper (preserving #[track_caller]).
    • ViewDriver, AppDriver, and PlaygroundDriver provide narrow trait-object boundaries so all 4 Measurement<'a> impl blocks (~1.2k lines) and 119 RuntimeCore impl blocks (~20.7k lines) compile once inside rune.
    • Playground DCE is preserved by storing coercion fn pointers on playground::Live<S> initialized only in Live::new.
  • Thin forwarders (crates/rune/src/runtime/api.rs, crates/rune/src/runtime/input/api.rs):
    • Public and crate-internal Runtime<S> methods delegate to RuntimeCore via #[inline(always)] one-liners, and non-S internal free functions across a11y, clipboard, file_dialog, popup_menu, motion_gate, tooling, and window now take &[mut] RuntimeCore.

Metrics

  • ~22,470 prod lines moved out of generic MIR (impl RuntimeCore: 20,657 lines across 119 blocks; impl Measurement<'a>: 1,201 lines across 4 blocks; impl CxCore: 131 lines; non-generic free functions: ~480 lines).
  • 0 generic Measurement blocks remaining.
  • 0 public API changes; all new modules are under 560 non-test lines.

@Hona
Hona merged commit 42dad31 into dev Nov 18, 2026
1 check passed
@Hona
Hona deleted the type-erase-runtime-core branch November 18, 2026 23:52
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