From 69a5d42158e16e5462183e7b0717241724b029a1 Mon Sep 17 00:00:00 2001 From: Tony Date: Mon, 18 May 2026 19:08:35 +0800 Subject: [PATCH 1/4] Removed a few clones --- crates/tauri-runtime-wry/src/lib.rs | 123 ++++++++++++++-------------- 1 file changed, 60 insertions(+), 63 deletions(-) diff --git a/crates/tauri-runtime-wry/src/lib.rs b/crates/tauri-runtime-wry/src/lib.rs index 7cfbe1855033..f9d861a93869 100644 --- a/crates/tauri-runtime-wry/src/lib.rs +++ b/crates/tauri-runtime-wry/src/lib.rs @@ -2950,6 +2950,57 @@ impl Wry { event_loop, }) } + + fn into_event_loop_and_handler) + 'static>( + self, + mut callback: F, + ) -> ( + EventLoop>, + impl FnMut(Event<'_, Message>, &EventLoopWindowTarget>, &mut ControlFlow), + ) { + let windows = self.context.main_thread.windows; + let window_id_map = self.context.window_id_map; + let web_context = self.context.main_thread.web_context; + let plugins = self.context.plugins; + + #[cfg(feature = "tracing")] + let active_tracing_spans = self.context.main_thread.active_tracing_spans; + let proxy = self.event_loop.create_proxy(); + + (self.event_loop, move |event, event_loop, control_flow| { + for p in plugins.lock().unwrap().iter_mut() { + let prevent_default = p.on_event( + &event, + event_loop, + &proxy, + control_flow, + EventLoopIterationContext { + callback: &mut callback, + window_id_map: window_id_map.clone(), + windows: windows.clone(), + #[cfg(feature = "tracing")] + active_tracing_spans: active_tracing_spans.clone(), + }, + &web_context, + ); + if prevent_default { + return; + } + } + handle_event_loop( + event, + event_loop, + control_flow, + EventLoopIterationContext { + callback: &mut callback, + window_id_map: window_id_map.clone(), + windows: windows.clone(), + #[cfg(feature = "tracing")] + active_tracing_spans: active_tracing_spans.clone(), + }, + ); + }) + } } impl Runtime for Wry { @@ -3187,13 +3238,13 @@ impl Runtime for Wry { #[cfg(desktop)] fn run_iteration) + 'static>(&mut self, mut callback: F) { use tao::platform::run_return::EventLoopExtRunReturn; - let windows = self.context.main_thread.windows.clone(); - let window_id_map = self.context.window_id_map.clone(); + let windows = &self.context.main_thread.windows; + let window_id_map = &self.context.window_id_map; let web_context = &self.context.main_thread.web_context; - let plugins = self.context.plugins.clone(); + let plugins = &self.context.plugins; #[cfg(feature = "tracing")] - let active_tracing_spans = self.context.main_thread.active_tracing_spans.clone(); + let active_tracing_spans = &self.context.main_thread.active_tracing_spans; let proxy = self.event_loop.create_proxy(); @@ -3241,18 +3292,16 @@ impl Runtime for Wry { } fn run) + 'static>(self, callback: F) { - let event_handler = make_event_handler(&self, callback); - - self.event_loop.run(event_handler) + let (event_loop, event_handler) = self.into_event_loop_and_handler(callback); + event_loop.run(event_handler) } #[cfg(not(target_os = "ios"))] - fn run_return) + 'static>(mut self, callback: F) -> i32 { + fn run_return) + 'static>(self, callback: F) -> i32 { use tao::platform::run_return::EventLoopExtRunReturn; - let event_handler = make_event_handler(&self, callback); - - self.event_loop.run_return(event_handler) + let (mut event_loop, event_handler) = self.into_event_loop_and_handler(callback); + event_loop.run_return(event_handler) } #[cfg(target_os = "ios")] @@ -3262,58 +3311,6 @@ impl Runtime for Wry { } } -fn make_event_handler( - runtime: &Wry, - mut callback: F, -) -> impl FnMut(Event<'_, Message>, &EventLoopWindowTarget>, &mut ControlFlow) -where - T: UserEvent, - F: FnMut(RunEvent) + 'static, -{ - let windows = runtime.context.main_thread.windows.clone(); - let window_id_map = runtime.context.window_id_map.clone(); - let web_context = runtime.context.main_thread.web_context.clone(); - let plugins = runtime.context.plugins.clone(); - - #[cfg(feature = "tracing")] - let active_tracing_spans = runtime.context.main_thread.active_tracing_spans.clone(); - let proxy = runtime.event_loop.create_proxy(); - - move |event, event_loop, control_flow| { - for p in plugins.lock().unwrap().iter_mut() { - let prevent_default = p.on_event( - &event, - event_loop, - &proxy, - control_flow, - EventLoopIterationContext { - callback: &mut callback, - window_id_map: window_id_map.clone(), - windows: windows.clone(), - #[cfg(feature = "tracing")] - active_tracing_spans: active_tracing_spans.clone(), - }, - &web_context, - ); - if prevent_default { - return; - } - } - handle_event_loop( - event, - event_loop, - control_flow, - EventLoopIterationContext { - callback: &mut callback, - window_id_map: window_id_map.clone(), - windows: windows.clone(), - #[cfg(feature = "tracing")] - active_tracing_spans: active_tracing_spans.clone(), - }, - ); - } -} - pub struct EventLoopIterationContext<'a, T: UserEvent> { pub callback: &'a mut (dyn FnMut(RunEvent) + 'static), pub window_id_map: WindowIdStore, From 3a46002f9d76383eef28f5ca98f4acf028b45220 Mon Sep 17 00:00:00 2001 From: Tony Date: Mon, 18 May 2026 19:15:32 +0800 Subject: [PATCH 2/4] A few more --- crates/tauri-runtime-wry/src/lib.rs | 44 ++++++++++++++--------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/crates/tauri-runtime-wry/src/lib.rs b/crates/tauri-runtime-wry/src/lib.rs index f9d861a93869..6f0705b4a1b4 100644 --- a/crates/tauri-runtime-wry/src/lib.rs +++ b/crates/tauri-runtime-wry/src/lib.rs @@ -241,8 +241,8 @@ pub(crate) fn send_user_message( &context.main_thread.window_target, message, UserMessageContext { - window_id_map: context.window_id_map.clone(), - windows: context.main_thread.windows.clone(), + window_id_map: &context.window_id_map, + windows: &context.main_thread.windows, }, ); Ok(()) @@ -2976,10 +2976,10 @@ impl Wry { control_flow, EventLoopIterationContext { callback: &mut callback, - window_id_map: window_id_map.clone(), - windows: windows.clone(), + window_id_map: &window_id_map, + windows: &windows, #[cfg(feature = "tracing")] - active_tracing_spans: active_tracing_spans.clone(), + active_tracing_spans: &active_tracing_spans, }, &web_context, ); @@ -2993,10 +2993,10 @@ impl Wry { control_flow, EventLoopIterationContext { callback: &mut callback, - window_id_map: window_id_map.clone(), - windows: windows.clone(), + window_id_map: &window_id_map, + windows: &windows, #[cfg(feature = "tracing")] - active_tracing_spans: active_tracing_spans.clone(), + active_tracing_spans: &active_tracing_spans, }, ); }) @@ -3264,10 +3264,10 @@ impl Runtime for Wry { control_flow, EventLoopIterationContext { callback: &mut callback, - window_id_map: window_id_map.clone(), - windows: windows.clone(), + window_id_map: &window_id_map, + windows: &windows, #[cfg(feature = "tracing")] - active_tracing_spans: active_tracing_spans.clone(), + active_tracing_spans: &active_tracing_spans, }, web_context, ); @@ -3282,10 +3282,10 @@ impl Runtime for Wry { control_flow, EventLoopIterationContext { callback: &mut callback, - windows: windows.clone(), - window_id_map: window_id_map.clone(), + windows: &windows, + window_id_map: &window_id_map, #[cfg(feature = "tracing")] - active_tracing_spans: active_tracing_spans.clone(), + active_tracing_spans: &active_tracing_spans, }, ); }); @@ -3313,15 +3313,15 @@ impl Runtime for Wry { pub struct EventLoopIterationContext<'a, T: UserEvent> { pub callback: &'a mut (dyn FnMut(RunEvent) + 'static), - pub window_id_map: WindowIdStore, - pub windows: Arc, + pub window_id_map: &'a WindowIdStore, + pub windows: &'a WindowsStore, #[cfg(feature = "tracing")] - pub active_tracing_spans: ActiveTraceSpanStore, + pub active_tracing_spans: &'a ActiveTraceSpanStore, } -struct UserMessageContext { - windows: Arc, - window_id_map: WindowIdStore, +struct UserMessageContext<'a> { + windows: &'a WindowsStore, + window_id_map: &'a WindowIdStore, } fn handle_user_message( @@ -4434,7 +4434,7 @@ fn handle_event_loop( fn on_close_requested<'a, T: UserEvent>( callback: &'a mut (dyn FnMut(RunEvent) + 'static), window_id: WindowId, - windows: Arc, + windows: &WindowsStore, ) { let (tx, rx) = channel(); let windows_ref = windows.0.borrow(); @@ -4462,7 +4462,7 @@ fn on_close_requested<'a, T: UserEvent>( } } -fn on_window_close(window_id: WindowId, windows: Arc) { +fn on_window_close(window_id: WindowId, windows: &WindowsStore) { if let Some(window_wrapper) = windows.0.borrow_mut().get_mut(&window_id) { window_wrapper.inner = None; #[cfg(windows)] From bd14b1c2c350faa2eb1000b6f36641ec7bcf1cda Mon Sep 17 00:00:00 2001 From: Tony Date: Mon, 18 May 2026 19:36:21 +0800 Subject: [PATCH 3/4] Move back make_event_handler and take context --- crates/tauri-runtime-wry/src/lib.rs | 121 ++++++++++++++-------------- 1 file changed, 59 insertions(+), 62 deletions(-) diff --git a/crates/tauri-runtime-wry/src/lib.rs b/crates/tauri-runtime-wry/src/lib.rs index 6f0705b4a1b4..4cbef1e871db 100644 --- a/crates/tauri-runtime-wry/src/lib.rs +++ b/crates/tauri-runtime-wry/src/lib.rs @@ -2950,57 +2950,6 @@ impl Wry { event_loop, }) } - - fn into_event_loop_and_handler) + 'static>( - self, - mut callback: F, - ) -> ( - EventLoop>, - impl FnMut(Event<'_, Message>, &EventLoopWindowTarget>, &mut ControlFlow), - ) { - let windows = self.context.main_thread.windows; - let window_id_map = self.context.window_id_map; - let web_context = self.context.main_thread.web_context; - let plugins = self.context.plugins; - - #[cfg(feature = "tracing")] - let active_tracing_spans = self.context.main_thread.active_tracing_spans; - let proxy = self.event_loop.create_proxy(); - - (self.event_loop, move |event, event_loop, control_flow| { - for p in plugins.lock().unwrap().iter_mut() { - let prevent_default = p.on_event( - &event, - event_loop, - &proxy, - control_flow, - EventLoopIterationContext { - callback: &mut callback, - window_id_map: &window_id_map, - windows: &windows, - #[cfg(feature = "tracing")] - active_tracing_spans: &active_tracing_spans, - }, - &web_context, - ); - if prevent_default { - return; - } - } - handle_event_loop( - event, - event_loop, - control_flow, - EventLoopIterationContext { - callback: &mut callback, - window_id_map: &window_id_map, - windows: &windows, - #[cfg(feature = "tracing")] - active_tracing_spans: &active_tracing_spans, - }, - ); - }) - } } impl Runtime for Wry { @@ -3264,10 +3213,10 @@ impl Runtime for Wry { control_flow, EventLoopIterationContext { callback: &mut callback, - window_id_map: &window_id_map, - windows: &windows, + window_id_map, + windows, #[cfg(feature = "tracing")] - active_tracing_spans: &active_tracing_spans, + active_tracing_spans, }, web_context, ); @@ -3282,26 +3231,26 @@ impl Runtime for Wry { control_flow, EventLoopIterationContext { callback: &mut callback, - windows: &windows, - window_id_map: &window_id_map, + windows, + window_id_map, #[cfg(feature = "tracing")] - active_tracing_spans: &active_tracing_spans, + active_tracing_spans, }, ); }); } fn run) + 'static>(self, callback: F) { - let (event_loop, event_handler) = self.into_event_loop_and_handler(callback); - event_loop.run(event_handler) + let event_handler = make_event_handler(self.context, callback); + self.event_loop.run(event_handler) } #[cfg(not(target_os = "ios"))] - fn run_return) + 'static>(self, callback: F) -> i32 { + fn run_return) + 'static>(mut self, callback: F) -> i32 { use tao::platform::run_return::EventLoopExtRunReturn; - let (mut event_loop, event_handler) = self.into_event_loop_and_handler(callback); - event_loop.run_return(event_handler) + let event_handler = make_event_handler(self.context, callback); + self.event_loop.run_return(event_handler) } #[cfg(target_os = "ios")] @@ -3311,6 +3260,54 @@ impl Runtime for Wry { } } +fn make_event_handler) + 'static>( + context: Context, + mut callback: F, +) -> impl FnMut(Event<'_, Message>, &EventLoopWindowTarget>, &mut ControlFlow) { + let windows = context.main_thread.windows; + let window_id_map = context.window_id_map; + let web_context = context.main_thread.web_context; + let plugins = context.plugins; + + #[cfg(feature = "tracing")] + let active_tracing_spans = context.main_thread.active_tracing_spans; + let proxy = context.proxy; + + move |event, event_loop, control_flow| { + for p in plugins.lock().unwrap().iter_mut() { + let prevent_default = p.on_event( + &event, + event_loop, + &proxy, + control_flow, + EventLoopIterationContext { + callback: &mut callback, + window_id_map: &window_id_map, + windows: &windows, + #[cfg(feature = "tracing")] + active_tracing_spans: &active_tracing_spans, + }, + &web_context, + ); + if prevent_default { + return; + } + } + handle_event_loop( + event, + event_loop, + control_flow, + EventLoopIterationContext { + callback: &mut callback, + window_id_map: &window_id_map, + windows: &windows, + #[cfg(feature = "tracing")] + active_tracing_spans: &active_tracing_spans, + }, + ); + } +} + pub struct EventLoopIterationContext<'a, T: UserEvent> { pub callback: &'a mut (dyn FnMut(RunEvent) + 'static), pub window_id_map: &'a WindowIdStore, From b2927293cc3e047d42e20f83c2f6090dc05e1ab4 Mon Sep 17 00:00:00 2001 From: Tony Date: Tue, 19 May 2026 09:58:21 +0800 Subject: [PATCH 4/4] Add change file --- .changes/runtime-wry-avoid-clones.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changes/runtime-wry-avoid-clones.md diff --git a/.changes/runtime-wry-avoid-clones.md b/.changes/runtime-wry-avoid-clones.md new file mode 100644 index 000000000000..afa03acab1f4 --- /dev/null +++ b/.changes/runtime-wry-avoid-clones.md @@ -0,0 +1,5 @@ +--- +"tauri-runtime-wry": "minor:changes" +--- + +`EventLoopIterationContext` and `UserMessageContext` now takes references to avoid clones