mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 08:03:19 +00:00
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>
This commit is contained in:
+73
-7
@@ -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<T: serde::Serialize>(
|
||||
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<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||
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<String> = (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::<Vec<_>>()[..]);
|
||||
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();
|
||||
|
||||
Reference in New Issue
Block a user