refactor(compiler-cli): attribute NgModule scope queries in perf summaries

In normal perf tracing mode, `ngp.rust.Summary` records top-5 `slowest` entries with `file` paths read from each query span's `file` field. That field was populated from `QueryKey::file_id()`, which returns `None` for `NgModuleExportsScope(ReferenceId)` and `NgModuleImportsScope(ReferenceId)`, leaving their `slowest` entries without file paths unless detail mode was enabled.

Add `QueryKey::perf_file_id()` to return `Some(reference_id.file)` for NgModule scope queries as well, and use it in `QueryEngine::execute` when creating the perf span. Keeping `QueryKey::file_id()` unchanged avoids marking NgModule files dirty or emitting them in `invalidatedPaths` when a downstream dependent evicts a cached scope key.
This commit is contained in:
Andrew Scott
2026-11-18 10:11:35 -08:00
committed by Alon Mishne
parent f8e9ae8312
commit 7b50ce3be2
3 changed files with 52 additions and 2 deletions
@@ -1,6 +1,8 @@
use super::layer::{Cat, Clock, NgPerfLayer, PerfTake, TraceEvent};
use super::{Counter, KindStats, SpanStats, ASYNC_TARGET, QUERY_TARGET};
use crate::query::keys::QueryKey;
use crate::query::ReferenceId;
use oxc_semantic::SymbolId;
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering::Relaxed};
use std::sync::Arc;
use tracing::Dispatch;
@@ -48,7 +50,7 @@ fn query(key: &QueryKey) -> tracing::Span {
target: QUERY_TARGET,
"query",
kind = key.kind_index() as u64,
file = key.file_id(),
file = key.perf_file_id(),
key = ?key,
done = tracing::field::Empty,
)
@@ -427,3 +429,37 @@ fn slowest_executions_serialize_into_the_summary() {
serde_json::json!([{"q": q, "busyUs": 3, "selfUs": 3, "wallUs": 4, "file": "/a.d.ts"}])
);
}
#[test]
fn ngmodule_scope_queries_attribute_declaring_file_without_changing_file_id() {
let exports_key = QueryKey::NgModuleExportsScope(ReferenceId::new(4, SymbolId::from(1)));
let imports_key = QueryKey::NgModuleImportsScope(ReferenceId::new(7, SymbolId::from(2)));
assert_eq!(exports_key.file_id(), None);
assert_eq!(imports_key.file_id(), None);
let h = Harness::new(false);
h.on_thread("a", || {
let exports_span = query(&exports_key);
h.at(10);
exports_span.in_scope(|| h.at(15));
exports_span.record("done", true);
let imports_span = query(&imports_key);
h.at(20);
imports_span.in_scope(|| h.at(28));
imports_span.record("done", true);
});
let take = h.layer.take(&|id| Some(format!("/src/f{id}.ts")));
assert_eq!(
take.summary.queries["NgModuleExportsScope"].slowest[0]
.file
.as_deref(),
Some("/src/f4.ts")
);
assert_eq!(
take.summary.queries["NgModuleImportsScope"].slowest[0]
.file
.as_deref(),
Some("/src/f7.ts")
);
}
@@ -1223,7 +1223,7 @@ impl QueryEngine {
target: crate::perf::QUERY_TARGET,
"query",
kind = key.kind_index() as u64,
file = key.file_id(),
file = key.perf_file_id(),
key = ?key,
done = tracing::field::Empty,
);
@@ -75,6 +75,20 @@ impl QueryKey {
}
}
/// The file to attribute this query to in perf summaries (`slowest` entries).
///
/// Includes [`Self::NgModuleExportsScope`] and [`Self::NgModuleImportsScope`] via the
/// NgModule's declaring file without widening [`Self::file_id`], which cache invalidation
/// uses to mark per-file results dirty.
pub fn perf_file_id(&self) -> Option<FileId> {
match *self {
Self::NgModuleExportsScope(ref_id) | Self::NgModuleImportsScope(ref_id) => {
Some(ref_id.file)
}
_ => self.file_id(),
}
}
/// Variant names, indexed by [`Self::kind_index`]. The perf layer maps a query span's
/// `kind` field back to its name here.
pub const KIND_NAMES: [&'static str; 13] = [