From e9f768d8b5cf5e1e32d9f87fc2dc906074f4e201 Mon Sep 17 00:00:00 2001 From: Mr0Bread <55754540+Mr0Bread@users.noreply.github.com> Date: Mon, 23 Nov 2026 14:01:45 +0200 Subject: [PATCH] perf(events): serialise an event once for all windows (#5445) With auxiliary windows open (comparison, diff, etc. that share the project's documents), every backend event was serialised once per window, because `emit_to` serialises its payload on each call. For a large diagnostics event that is 2.3 MB (10k items) or 11.6 MB (50k items) of JSON built again for each window, on the thread that emits the event. `TauriSink` now serialises the event once and sends the same JSON string to each window with `emit_str_to`. The payload is the same `serde_json::to_string(&event)` that `emit_to` produced, so the frontend (`getCurrentWebviewWindow().listen` in `src/platform/ipc.ts`) receives exactly what it did before. Which windows get which events is unchanged. Co-authored-by: Mr0Bread <55754540+Mr0Bread@users.noreply.github.com> --- src-tauri/src/events.rs | 80 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 73 insertions(+), 7 deletions(-) diff --git a/src-tauri/src/events.rs b/src-tauri/src/events.rs index 9b854171f..c66793446 100644 --- a/src-tauri/src/events.rs +++ b/src-tauri/src/events.rs @@ -155,6 +155,23 @@ fn flash_taskbar_unless_focused(app: &AppHandle) { }); } +/// Sends `event` to `peers` (failures ignored: a window may be closing) and then to `owner`, +/// whose result is returned. The event is serialised once; `send` gets each window's copy of the +/// JSON, which Tauri's `emit_to` would otherwise re-serialise per window (large for +/// diagnostics of a big file). +fn fan_out( + event: &T, + peers: &[String], + owner: &str, + mut send: impl FnMut(&str, String) -> tauri::Result<()>, +) -> tauri::Result<()> { + let json = serde_json::to_string(event)?; + for peer in peers { + let _ = send(peer, json.clone()); + } + send(owner, json) +} + impl EventSink for TauriSink { fn emit(&self, event: IdeEvent) { if matches!( @@ -180,13 +197,12 @@ impl EventSink for TauriSink { } let result = match &self.window { Some(label) => { - for target in crate::windows::document_windows(&self.app, label) - .into_iter() - .filter(|_| !matches!(&event, IdeEvent::McpRequest { .. } | IdeEvent::McpRequestClosed { .. })) - { - let _ = self.app.emit_to(target.as_str(), IDE_EVENT, &event); - } - self.app.emit_to(label.as_str(), IDE_EVENT, &event) + let peers = if matches!(&event, IdeEvent::McpRequest { .. } | IdeEvent::McpRequestClosed { .. }) { + Vec::new() + } else { + crate::windows::document_windows(&self.app, label) + }; + fan_out(&event, &peers, label, |window, json| self.app.emit_str_to(window, IDE_EVENT, json)) } None => self.app.emit(IDE_EVENT, &event), }; @@ -240,6 +256,56 @@ mod tests { } } + /// Counts how often it is serialised. + struct CountingEvent<'a> { + event: &'a IdeEvent, + serialised: AtomicUsize, + } + + impl serde::Serialize for CountingEvent<'_> { + fn serialize(&self, serializer: S) -> Result { + self.serialised.fetch_add(1, Ordering::Relaxed); + self.event.serialize(serializer) + } + } + + #[test] + fn fan_out_serialises_once_for_every_window() { + let event = diagnostics("/p/a.rs", "rust-analyzer", "unused"); + let counting = CountingEvent { event: &event, serialised: AtomicUsize::new(0) }; + let peers: Vec = (1..=7).map(|n| format!("comparison-project-1-{n}")).collect(); + let mut sent = Vec::new(); + + fan_out(&counting, &peers, "project-1", |window, json| { + sent.push((window.to_owned(), json)); + Ok(()) + }) + .unwrap(); + + assert_eq!(counting.serialised.load(Ordering::Relaxed), 1); + let expected = serde_json::to_string(&event).unwrap(); + let windows: Vec<&str> = sent.iter().map(|(window, _)| window.as_str()).collect(); + assert_eq!(windows.len(), 8); + assert_eq!(windows.last(), Some(&"project-1")); + assert_eq!(windows[..7], peers.iter().map(String::as_str).collect::>()[..]); + assert!(sent.iter().all(|(_, json)| json == &expected)); + } + + #[test] + fn fan_out_ignores_peer_failures_and_returns_the_owners_result() { + let event = task("indexing", false); + let peers = vec!["comparison-project-1-1".to_owned(), "comparison-project-1-2".to_owned()]; + let mut reached = Vec::new(); + + let result = fan_out(&event, &peers, "project-1", |window, _| { + reached.push(window.to_owned()); + if window == "project-1" { Err(tauri::Error::WebviewNotFound) } else { Err(tauri::Error::WindowNotFound) } + }); + + assert!(matches!(result, Err(tauri::Error::WebviewNotFound))); + assert_eq!(reached, ["comparison-project-1-1", "comparison-project-1-2", "project-1"]); + } + #[tokio::test] async fn coalesces_diagnostics_by_path_and_source() { let (sink, events) = setup();