From 9b9d93f36b7fb3861d22924af62306af7925d926 Mon Sep 17 00:00:00 2001 From: zerob13 Date: Sun, 22 Nov 2026 10:43:49 +0800 Subject: [PATCH] perf(runtime): avoid scanning diagnostics for opening progress --- crates/dim-agent-runtime/src/assembly.rs | 52 +++++++++++-------- crates/dim-agent-runtime/src/config.rs | 2 +- .../dim-agent-runtime/src/timeline_context.rs | 2 +- .../tests/opening_progress.rs | 10 +++- 4 files changed, 39 insertions(+), 27 deletions(-) diff --git a/crates/dim-agent-runtime/src/assembly.rs b/crates/dim-agent-runtime/src/assembly.rs index fd1b06275f..6ef4c67658 100644 --- a/crates/dim-agent-runtime/src/assembly.rs +++ b/crates/dim-agent-runtime/src/assembly.rs @@ -26,8 +26,12 @@ pub const SESSION_GROUP: &str = "session"; /// Instanced loader group of one branch, nested in [`SESSION_GROUP`]. pub const BRANCH_GROUP: &str = "branch"; -#[derive(serde::Serialize, serde::Deserialize)] -pub(crate) struct OpeningAdvanced(pub PluginId); +#[derive(Clone, Eq, Hash, PartialEq, serde::Serialize, serde::Deserialize)] +#[serde(untagged)] +pub(crate) enum OpeningAdvanced { + Plugin(PluginId), + Instance(String), +} impl rutis::Event for OpeningAdvanced { const NAME: &'static str = "dim/opening-advanced"; type Value = (); @@ -41,15 +45,8 @@ impl rutis::Event for OpeningAdvanced { /// work runs on a blocking thread reports from there. A context whose plugin /// has no live fiber reports nothing: the report would be dropped anyway. pub fn opening_advanced(ctx: &Ctx) { - let instance = ctx.instance(); - let id = ctx - .diagnostics() - .plugins - .into_iter() - .find(|plugin| plugin.instance == instance) - .map(|plugin| plugin.id); - if let Some(id) = id { - let _ = ctx.emit(OpeningAdvanced(id)); + if !ctx.cancellation_token().is_cancelled() { + let _ = ctx.emit(OpeningAdvanced::Instance(ctx.instance().to_string())); } } @@ -68,7 +65,7 @@ pub struct StartupProgress { ctx: Ctx, started: tokio::time::Instant, timeout: Duration, - active: Arc>>, + active: Arc>>, report: Arc>>, listeners: Vec, } @@ -84,7 +81,7 @@ impl StartupProgress { if event.to == FiberState::Active { let inserted = { let mut changes = changes.lock().unwrap(); - match changes.entry(event.plugin_id) { + match changes.entry(OpeningAdvanced::Plugin(event.plugin_id)) { std::collections::hash_map::Entry::Vacant(entry) => { entry.insert(tokio::time::Instant::now()); true @@ -114,7 +111,7 @@ impl StartupProgress { changes .lock() .unwrap() - .insert(event.0, tokio::time::Instant::now()); + .insert((*event).clone(), tokio::time::Instant::now()); notify_progress(&reports); Ok(None) })?); @@ -150,15 +147,24 @@ impl StartupProgress { .into_iter() .flat_map(|id| subtree(&snapshot, id)) .collect(); - let last = self - .active - .lock() - .unwrap() - .iter() - .filter(|(id, _)| members.contains(id)) - .map(|(_, time)| *time) - .max() - .unwrap_or(self.started); + let last = { + let active = self.active.lock().unwrap(); + let by_plugin = members + .iter() + .filter_map(|&id| active.get(&OpeningAdvanced::Plugin(id))); + let by_instance = snapshot + .plugins + .iter() + .filter(|plugin| members.contains(&plugin.id)) + .filter_map(|plugin| { + active.get(&OpeningAdvanced::Instance(plugin.instance.to_string())) + }); + by_plugin + .chain(by_instance) + .copied() + .max() + .unwrap_or(self.started) + }; deadline = last + self.timeout; if deadline <= tokio::time::Instant::now() { return None; diff --git a/crates/dim-agent-runtime/src/config.rs b/crates/dim-agent-runtime/src/config.rs index 38e20edcba..c97921fe33 100644 --- a/crates/dim-agent-runtime/src/config.rs +++ b/crates/dim-agent-runtime/src/config.rs @@ -381,7 +381,7 @@ impl BranchLifecycle { let mut pending: FuturesUnordered<_> = receipts.into_iter().collect(); while let Some(result) = pending.next().await { result?; - ctx.emit(crate::assembly::OpeningAdvanced(scope))?; + ctx.emit(crate::assembly::OpeningAdvanced::Plugin(scope))?; } Ok(()) } diff --git a/crates/dim-agent-runtime/src/timeline_context.rs b/crates/dim-agent-runtime/src/timeline_context.rs index bcf2b01a12..c43aa46d49 100644 --- a/crates/dim-agent-runtime/src/timeline_context.rs +++ b/crates/dim-agent-runtime/src/timeline_context.rs @@ -187,7 +187,7 @@ impl TimelineContext { if dispatched > sequence { sequence = dispatched; if inner.lifecycle.phase() == BranchPhase::Loading - && let Err(error) = inner.ctx.emit(OpeningAdvanced(inner.view.id)) + && let Err(error) = inner.ctx.emit(OpeningAdvanced::Plugin(inner.view.id)) { inner.dispatcher.fail(error.into()); } diff --git a/crates/dim-agent-runtime/tests/opening_progress.rs b/crates/dim-agent-runtime/tests/opening_progress.rs index 83234e96ed..394d619f7a 100644 --- a/crates/dim-agent-runtime/tests/opening_progress.rs +++ b/crates/dim-agent-runtime/tests/opening_progress.rs @@ -36,6 +36,13 @@ async fn opened(reports: bool) -> dim_agent_runtime::Result<()> { gap: Duration::from_millis(60), reports, }); + if !reports { + root.plugin(Progressing { + steps: 6, + gap: Duration::from_millis(60), + reports: true, + }); + } let result = wait_active( &root, std::slice::from_ref(&view), @@ -56,9 +63,8 @@ async fn an_opening_that_reports_progress_outlives_its_interval() { .expect("reported progress renews the interval"); } -/// The same opening without reports ends at the interval. #[tokio::test] -async fn an_opening_that_reports_nothing_times_out() { +async fn progress_in_a_sibling_does_not_renew_an_opening() { let error = opened(false) .await .expect_err("an inactive opening must time out");