From 16e50264ca29831b23e87592570ec66a82d99d6d Mon Sep 17 00:00:00 2001 From: aiongg Date: Thu, 19 Nov 2026 02:27:44 +0000 Subject: [PATCH] ui-macros: CommandSet::defs builds each command in its own function The derive pushed every variant's definition inline, so one frame held a CommandDef temporary per variant (175 KB for the largest set in PhotoCraft at opt-level 0). Each variant is now a non-capturing closure in a table of function pointers, and defs loops over the table. --- crates/ui-macros/src/command_set.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/crates/ui-macros/src/command_set.rs b/crates/ui-macros/src/command_set.rs index 50553c1ed..9beb0a132 100644 --- a/crates/ui-macros/src/command_set.rs +++ b/crates/ui-macros/src/command_set.rs @@ -280,12 +280,15 @@ pub(crate) fn derive(input: TokenStream) -> syn::Result { holds.push(quote!((#id, #k::runtime::commands::CommandScope::new(#held)))); } arms.push(quote!(#id => ::core::option::Option::Some(Self::#name))); + // One non-capturing closure per variant, collected in a table of function pointers: `defs` + // loops over the table, so its frame does not grow with the number of variants (a closure's + // frame is released when it returns) and the push code exists once. defs.push(quote! { - { + || { let scope = Self::SCOPE.scope().ok_or_else(|| #k::runtime::commands::CommandDefError::scope(#id))?; let mut def = #k::runtime::commands::CommandDef::new(#k::runtime::commands::CommandRef::new(#id), #label, scope) #chain; #(#defaults)* - out.push(def); + ::core::result::Result::Ok(def) } }); ids.push(id); @@ -310,9 +313,9 @@ pub(crate) fn derive(input: TokenStream) -> syn::Result { } fn defs() -> ::core::result::Result<::std::vec::Vec<#k::runtime::commands::CommandDef>, #k::runtime::commands::CommandDefError> { - let mut out = ::std::vec::Vec::new(); - #(#defs)* - ::core::result::Result::Ok(out) + type Build = fn() -> ::core::result::Result<#k::runtime::commands::CommandDef, #k::runtime::commands::CommandDefError>; + let table: &[Build] = &[#(#defs),*]; + table.iter().map(|build| build()).collect() } fn no_default_on() -> ::std::vec::Vec<(&'static str, #k::runtime::commands::Platform)> {