From 0e5c47bc79c88c34043e0eb045334e23e07e30aa Mon Sep 17 00:00:00 2001 From: Berchia <58830244+Berchia@users.noreply.github.com> Date: Sat, 21 Nov 2026 15:47:39 -0800 Subject: [PATCH] Disjoint sets box their state, so a system holding the host is small (#3045) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A `disjoint_set!` held every member's state inline, and `ScriptCallHost`'s is 181 KB. Bevy keeps a system's param state inline from the moment the system is made, so every system that takes the host was 181 KB or more on the stack while a plugin added it. Building the headless client came close to a Linux test thread's 2 MB, and any change near those systems could overflow `the_hosted_vm_keeps_one_state_across_calls`. The set's state is now boxed; nothing outside the macro changes. A test holds the host's state to at most 64 bytes. On Windows, that test's stack need drops from about 1.5 MB to about 400 KB. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- .../benilla-app/src/script_calls/disjoint.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/crates/benilla-app/src/script_calls/disjoint.rs b/crates/benilla-app/src/script_calls/disjoint.rs index e4c3ee6cb..930616051 100644 --- a/crates/benilla-app/src/script_calls/disjoint.rs +++ b/crates/benilla-app/src/script_calls/disjoint.rs @@ -63,16 +63,17 @@ macro_rules! disjoint_set { // holds outside the set ([`crate::script_calls::disjoint::gather`]), and lends one member // at a time, through `&mut self`. unsafe impl bevy::ecs::system::SystemParam for $name<'_, '_> { - type State = $state; + // Boxed, as a system holds its params' state inline from the moment it is added. + type State = Box<$state>; type Item<'w, 's> = $name<'w, 's>; fn init_state(world: &mut bevy::ecs::world::World) -> Self::State { - $state { + Box::new($state { $( $(#[cfg($cfg)])? $field: <$ty as bevy::ecs::system::SystemParam>::init_state(world), )* - } + }) } fn init_access( @@ -142,7 +143,7 @@ macro_rules! disjoint_set { change_tick: bevy::ecs::change_detection::Tick, ) -> Self::Item<'w, 's> { $name { - states: state, + states: &mut **state, meta: meta.clone(), world, change_tick, @@ -387,6 +388,15 @@ mod tests { use super::Pair; + /// A system holds the host's state inline from the moment it is added. + #[test] + fn the_hosts_state_is_a_few_words() { + use crate::script_calls::ScriptCallHost; + use bevy::ecs::system::SystemParam; + let size = size_of::< as SystemParam>::State>(); + assert!(size <= 64, "{size}"); + } + #[derive(Resource, Default)] struct Cell(u32);