Unverified Commit 7d451fe0 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

Add module count field to module graph tracing spans (#91697)

### What?

Adds a `modules` field to two tracing spans that cover module graph
construction:

- `"module graph for endpoint"` span (per-page graph, in `app.rs`) —
records the total number of modules across all `SingleModuleGraph`s
built for that endpoint.
- `"whole app module graph"` span (new info-level span wrapping
`whole_app_module_graph_operation` in `project.rs`) — records the
combined module count of the base graph and the additional-entries
graph.

Also adds a `module_count()` method to `SingleModuleGraph` (as a
`#[turbo_tasks::function]` returning `Vc<u64>`) that exposes
`number_of_modules`.

### Why?

Module graph size directly affects build and HMR performance. Without a
count in the trace, it is impossible to correlate span duration with
graph size or spot regressions where the graph grows unexpectedly.
Surfacing the count as a structured span field makes it queryable
without requiring log parsing.

### How?

- `SingleModuleGraph::module_count()` is a cheap
`#[turbo_tasks::function]` that wraps the already-computed
`number_of_modules: usize` field as a `Vc<u64>`.
- Both call sites follow the deferred-field pattern already used in
`ProjectContainer::initialize`: the span is created with `modules =
Empty` before the `async move` block, `span.record("modules", count)` is
called once the graphs are ready, and `span_clone` is passed to
`.instrument()` so the field is recorded on the correct span.
- Module counts are summed across all graphs that contribute to the
endpoint/app.

---------
Co-authored-by: 's avatarTobias Koppers <sokra@users.noreply.github.com>
Co-authored-by: 's avatarClaude <noreply@anthropic.com>
Co-authored-by: 's avatarVercel <vercel[bot]@users.noreply.github.com>
parent 25646b92
...@@ -35,7 +35,7 @@ use next_core::{ ...@@ -35,7 +35,7 @@ use next_core::{
segment_config::{NextSegmentConfig, ParseSegmentMode}, segment_config::{NextSegmentConfig, ParseSegmentMode},
util::{NextRuntime, app_function_name, module_styles_rule_condition, styles_rule_condition}, util::{NextRuntime, app_function_name, module_styles_rule_condition, styles_rule_condition},
}; };
use tracing::Instrument; use tracing::{Instrument, field::Empty};
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ use turbo_tasks::{
Completion, NonLocalValue, ResolvedVc, TryJoinIterExt, ValueToString, Vc, fxindexset, Completion, NonLocalValue, ResolvedVc, TryJoinIterExt, ValueToString, Vc, fxindexset,
...@@ -884,6 +884,8 @@ impl AppProject { ...@@ -884,6 +884,8 @@ impl AppProject {
// Implements layout segment optimization to compute a graph "chain" for each layout // Implements layout segment optimization to compute a graph "chain" for each layout
// segment // segment
let span = tracing::info_span!("module graph for endpoint", modules = Empty);
let span_clone = span.clone();
async move { async move {
let rsc_entry_chunk_group = ChunkGroupEntry::Entry(vec![rsc_entry]); let rsc_entry_chunk_group = ChunkGroupEntry::Entry(vec![rsc_entry]);
...@@ -974,6 +976,14 @@ impl AppProject { ...@@ -974,6 +976,14 @@ impl AppProject {
); );
graphs.push(additional_module_graph); graphs.push(additional_module_graph);
if !span.is_disabled() {
let mut module_count = 0u64;
for g in &graphs {
module_count += g.connect().module_count().untracked().owned().await?;
}
span.record("modules", module_count);
}
let remove_unused_imports = *self let remove_unused_imports = *self
.project .project
.next_config() .next_config()
...@@ -1004,7 +1014,7 @@ impl AppProject { ...@@ -1004,7 +1014,7 @@ impl AppProject {
} }
.cell()) .cell())
} }
.instrument(tracing::info_span!("module graph for endpoint")) .instrument(span_clone)
.await .await
} else { } else {
Ok(self.project.whole_app_module_graphs()) Ok(self.project.whole_app_module_graphs())
......
...@@ -1482,7 +1482,6 @@ impl Project { ...@@ -1482,7 +1482,6 @@ impl Project {
pub async fn whole_app_module_graphs( pub async fn whole_app_module_graphs(
self: ResolvedVc<Self>, self: ResolvedVc<Self>,
) -> Result<Vc<BaseAndFullModuleGraph>> { ) -> Result<Vc<BaseAndFullModuleGraph>> {
async move {
let module_graphs_op = whole_app_module_graph_operation(self); let module_graphs_op = whole_app_module_graph_operation(self);
let module_graphs_vc = if self.next_mode().await?.is_production() { let module_graphs_vc = if self.next_mode().await?.is_production() {
module_graphs_op.connect() module_graphs_op.connect()
...@@ -1506,9 +1505,6 @@ impl Project { ...@@ -1506,9 +1505,6 @@ impl Project {
Ok(module_graphs_vc) Ok(module_graphs_vc)
} }
.instrument(tracing::info_span!("module graph for app"))
.await
}
#[turbo_tasks::function] #[turbo_tasks::function]
pub(super) async fn server_compile_time_info(self: Vc<Self>) -> Result<Vc<CompileTimeInfo>> { pub(super) async fn server_compile_time_info(self: Vc<Self>) -> Result<Vc<CompileTimeInfo>> {
...@@ -2435,6 +2431,9 @@ impl Project { ...@@ -2435,6 +2431,9 @@ impl Project {
async fn whole_app_module_graph_operation( async fn whole_app_module_graph_operation(
project: ResolvedVc<Project>, project: ResolvedVc<Project>,
) -> Result<Vc<BaseAndFullModuleGraph>> { ) -> Result<Vc<BaseAndFullModuleGraph>> {
let span = tracing::info_span!("whole app module graph", modules = Empty);
let span_clone = span.clone();
async move {
let next_mode = project.next_mode(); let next_mode = project.next_mode();
let next_mode_ref = next_mode.await?; let next_mode_ref = next_mode.await?;
let should_trace = next_mode_ref.is_production(); let should_trace = next_mode_ref.is_production();
...@@ -2454,8 +2453,8 @@ async fn whole_app_module_graph_operation( ...@@ -2454,8 +2453,8 @@ async fn whole_app_module_graph_operation(
.await?; .await?;
let base = if turbopack_remove_unused_imports { let base = if turbopack_remove_unused_imports {
// TODO suboptimal that we do compute_binding_usage_info twice (once for the base graph // TODO suboptimal that we do compute_binding_usage_info twice (once for the base
// and later for the full graph) // graph and later for the full graph)
let binding_usage_info = compute_binding_usage_info(base, true); let binding_usage_info = compute_binding_usage_info(base, true);
ModuleGraph::from_single_graph_without_unused_references( ModuleGraph::from_single_graph_without_unused_references(
base_single_module_graph, base_single_module_graph,
...@@ -2477,6 +2476,22 @@ async fn whole_app_module_graph_operation( ...@@ -2477,6 +2476,22 @@ async fn whole_app_module_graph_operation(
should_read_binding_usage, should_read_binding_usage,
); );
if !span.is_disabled() {
let base_module_count = base_single_module_graph
.connect()
.module_count()
.untracked()
.owned()
.await?;
let additional_module_count = additional_module_graph
.connect()
.module_count()
.untracked()
.owned()
.await?;
span.record("modules", base_module_count + additional_module_count);
}
let graphs = vec![base_single_module_graph, additional_module_graph]; let graphs = vec![base_single_module_graph, additional_module_graph];
let (full, binding_usage_info) = if turbopack_remove_unused_imports { let (full, binding_usage_info) = if turbopack_remove_unused_imports {
...@@ -2496,6 +2511,9 @@ async fn whole_app_module_graph_operation( ...@@ -2496,6 +2511,9 @@ async fn whole_app_module_graph_operation(
binding_usage_info, binding_usage_info,
} }
.cell()) .cell())
}
.instrument(span_clone)
.await
} }
#[turbo_tasks::value(shared)] #[turbo_tasks::value(shared)]
......
...@@ -1522,6 +1522,11 @@ impl SingleModuleGraph { ...@@ -1522,6 +1522,11 @@ impl SingleModuleGraph {
) )
.await .await
} }
#[turbo_tasks::function]
pub async fn module_count(&self) -> Result<Vc<u64>> {
Ok(Vc::cell(self.number_of_modules as u64))
}
} }
#[derive(Clone, Debug, Serialize, Deserialize, TraceRawVcs, NonLocalValue)] #[derive(Clone, Debug, Serialize, Deserialize, TraceRawVcs, NonLocalValue)]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment