Skip to content

Commit d79ea56

Browse files
committed
UnusedReferences.contains_reference
1 parent c5150ba commit d79ea56

File tree

3 files changed

+92
-38
lines changed

3 files changed

+92
-38
lines changed

crates/next-core/src/next_client_reference/visit_client_reference.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ impl Visit<FindServerEntriesNode> for FindServerEntries {
192192

193193
let referenced_modules = referenced_modules
194194
.iter()
195-
.flat_map(|(chunking_type, _, _, modules)| match chunking_type {
195+
.flat_map(|(_, chunking_type, _, _, modules)| match chunking_type {
196196
ChunkingType::Traced => None,
197197
_ => Some(modules.iter()),
198198
})

turbopack/crates/turbopack-core/src/module_graph/import_usage.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,24 @@ use turbo_tasks::{ResolvedVc, TryJoinIterExt, Vc};
55
use crate::{
66
module::Module,
77
module_graph::{GraphEdgeIndex, ModuleGraph, export_usage::compute_export_usage_info},
8+
reference::ModuleReference,
89
resolve::ImportUsage,
910
};
1011

1112
#[turbo_tasks::value]
1213
#[derive(Clone, Default, Debug)]
13-
pub struct UnusedReferences(#[turbo_tasks(trace_ignore)] FxHashSet<GraphEdgeIndex>);
14+
pub struct UnusedReferences {
15+
references: FxHashSet<ResolvedVc<Box<dyn ModuleReference>>>,
16+
#[turbo_tasks(trace_ignore)]
17+
edges: FxHashSet<GraphEdgeIndex>,
18+
}
1419

1520
impl UnusedReferences {
16-
pub fn contains(&self, edge: &GraphEdgeIndex) -> bool {
17-
self.0.contains(edge)
21+
pub fn contains_edge(&self, edge: &GraphEdgeIndex) -> bool {
22+
self.edges.contains(edge)
23+
}
24+
pub fn contains_reference(&self, reference: &ResolvedVc<Box<dyn ModuleReference>>) -> bool {
25+
self.references.contains(reference)
1826
}
1927
}
2028

@@ -29,8 +37,9 @@ pub async fn compute_import_usage_info(
2937
.read_strongly_consistent()
3038
.await?;
3139

32-
let mut unused_references_name = FxHashSet::<_>::default();
33-
let mut unused_references = FxHashSet::<_>::default();
40+
let mut unused_references_name = FxHashSet::default();
41+
let mut edges = FxHashSet::default();
42+
let mut references = FxHashSet::default();
3443
let graph = graph.read_graphs().await?;
3544
graph.traverse_all_edges_unordered(|(source, ref_data, edge), target| {
3645
match &ref_data.import {
@@ -46,7 +55,9 @@ pub async fn compute_import_usage_info(
4655
.all(|e| !source_used_exports.is_export_used(e))
4756
{
4857
unused_references_name.insert((source, ref_data.export.clone(), target));
49-
unused_references.insert(edge);
58+
59+
edges.insert(edge);
60+
references.insert(ref_data.reference);
5061
}
5162
}
5263
}
@@ -62,7 +73,8 @@ pub async fn compute_import_usage_info(
6273
.try_join()
6374
.await?
6475
);
65-
println!("unused_references: {:#?}", unused_references);
76+
println!("edges: {:#?}", edges);
77+
println!("references: {:#?}", references);
6678

67-
Ok(UnusedReferences(unused_references).cell())
79+
Ok(UnusedReferences { edges, references }.cell())
6880
}

turbopack/crates/turbopack-core/src/module_graph/mod.rs

Lines changed: 71 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use crate::{
3535
style_groups::{StyleGroups, StyleGroupsConfig, compute_style_groups},
3636
traced_di_graph::TracedDiGraph,
3737
},
38-
reference::primary_chunkable_referenced_modules,
38+
reference::{ModuleReference, primary_chunkable_referenced_modules},
3939
resolve::{ExportUsage, ImportUsage},
4040
};
4141

@@ -227,6 +227,7 @@ pub struct RefData {
227227
pub chunking_type: ChunkingType,
228228
pub export: ExportUsage,
229229
pub import: ImportUsage,
230+
pub reference: ResolvedVc<Box<dyn ModuleReference>>,
230231
}
231232

232233
impl SingleModuleGraph {
@@ -247,13 +248,14 @@ impl SingleModuleGraph {
247248
to: SingleModuleGraphBuilderNode::new_module(emit_spans, e).await?,
248249
export: ExportUsage::All,
249250
import: ImportUsage::Global,
251+
reference: None,
250252
})
251253
})
252254
.try_join()
253255
.await?;
254256

255257
let (children_nodes_iter, visited_nodes) = AdjacencyMap::new()
256-
.skip_duplicates_with_key(|node: &(SingleModuleGraphBuilderNode, _, _)| &node.0)
258+
.skip_duplicates_with_key(|node: &(SingleModuleGraphBuilderNode, _, _, _)| &node.0)
257259
.visit(
258260
root_edges,
259261
SingleModuleGraphBuilder {
@@ -280,7 +282,7 @@ impl SingleModuleGraph {
280282
FxHashMap::with_capacity_and_hasher(node_count, Default::default());
281283
{
282284
let _span = tracing::info_span!("build module graph").entered();
283-
for (parent, (current, export, import)) in
285+
for (parent, (current, reference, export, import)) in
284286
children_nodes_iter.into_breadth_first_edges()
285287
{
286288
let parent_edge = match parent.map(|v| v.0) {
@@ -290,6 +292,7 @@ impl SingleModuleGraph {
290292
chunking_type: COMMON_CHUNKING_TYPE,
291293
export,
292294
import,
295+
reference: reference.unwrap(),
293296
},
294297
)),
295298
Some(SingleModuleGraphBuilderNode::ChunkableReference { .. }) => {
@@ -483,7 +486,9 @@ impl SingleModuleGraph {
483486
visit_stack.push(VisitStep::AfterVisit(node));
484487
let mut neighbors = self.graph.neighbors(node).detach();
485488
while let Some((edge, succ)) = neighbors.next(&self.graph) {
486-
if unused_references.contains(&GraphEdgeIndex::new(graph_idx, edge)) {
489+
if unused_references
490+
.contains_edge(&GraphEdgeIndex::new(graph_idx, edge))
491+
{
487492
continue;
488493
}
489494

@@ -874,14 +879,13 @@ impl ModuleGraph {
874879
/// In particular, this removes ChunkableModuleReference-s that list only unused exports in the
875880
/// `import_usage()`
876881
#[turbo_tasks::function]
877-
pub async fn without_unused_references(self: ResolvedVc<Self>) -> Result<Vc<Self>> {
878-
let import_usage = compute_import_usage_info(self)
879-
.resolve_strongly_consistent()
880-
.await?;
881-
882+
pub async fn without_unused_references(
883+
self: ResolvedVc<Self>,
884+
unused_references: ResolvedVc<UnusedReferences>,
885+
) -> Result<Vc<Self>> {
882886
Ok(Self {
883887
graphs: self.await?.graphs.clone(),
884-
unused_references: Some(import_usage),
888+
unused_references: Some(unused_references),
885889
}
886890
.cell())
887891
}
@@ -1150,7 +1154,7 @@ impl ModuleGraphRef {
11501154
let idx = GraphEdgeIndex::new(graph_idx as u32, edge.id());
11511155
// TODO this ignores the edges directly ignored by unused_references, but not the
11521156
// subgraphs that become disconnected because of that.
1153-
if self.unused_references.contains(&idx) {
1157+
if self.unused_references.contains_edge(&idx) {
11541158
continue;
11551159
}
11561160
let source = graph.node_weight(edge.source()).unwrap().module();
@@ -1399,7 +1403,7 @@ fn iter_graphs_neighbors_rev<'a>(
13991403
let mut walker = graph.neighbors(node.node_idx).detach();
14001404
std::iter::from_fn(move || {
14011405
while let Some((edge_idx, succ_idx)) = walker.next(graph) {
1402-
if unused_references.contains(&GraphEdgeIndex::new(node.graph_idx, edge_idx)) {
1406+
if unused_references.contains_edge(&GraphEdgeIndex::new(node.graph_idx, edge_idx)) {
14031407
// Don't just return None here, that would end the iterator
14041408
continue;
14051409
}
@@ -1558,6 +1562,7 @@ struct SingleModuleGraphBuilderEdge {
15581562
to: SingleModuleGraphBuilderNode,
15591563
export: ExportUsage,
15601564
import: ImportUsage,
1565+
reference: Option<ResolvedVc<Box<dyn ModuleReference>>>,
15611566
}
15621567

15631568
/// The chunking type that occurs most often, is handled more efficiently by not creating
@@ -1575,8 +1580,13 @@ struct SingleModuleGraphBuilder<'a> {
15751580
/// Whether to walk ChunkingType::Traced references
15761581
include_traced: bool,
15771582
}
1578-
impl Visit<(SingleModuleGraphBuilderNode, ExportUsage, ImportUsage)>
1579-
for SingleModuleGraphBuilder<'_>
1583+
impl
1584+
Visit<(
1585+
SingleModuleGraphBuilderNode,
1586+
Option<ResolvedVc<Box<dyn ModuleReference>>>,
1587+
ExportUsage,
1588+
ImportUsage,
1589+
)> for SingleModuleGraphBuilder<'_>
15801590
{
15811591
type Edge = SingleModuleGraphBuilderEdge;
15821592
type EdgesIntoIter = Vec<Self::Edge>;
@@ -1585,22 +1595,32 @@ impl Visit<(SingleModuleGraphBuilderNode, ExportUsage, ImportUsage)>
15851595
fn visit(
15861596
&mut self,
15871597
edge: Self::Edge,
1588-
) -> VisitControlFlow<(SingleModuleGraphBuilderNode, ExportUsage, ImportUsage)> {
1598+
) -> VisitControlFlow<(
1599+
SingleModuleGraphBuilderNode,
1600+
Option<ResolvedVc<Box<dyn ModuleReference>>>,
1601+
ExportUsage,
1602+
ImportUsage,
1603+
)> {
15891604
match edge.to {
15901605
SingleModuleGraphBuilderNode::Module { .. } => {
1591-
VisitControlFlow::Continue((edge.to, edge.export, edge.import))
1606+
VisitControlFlow::Continue((edge.to, edge.reference, edge.export, edge.import))
15921607
}
15931608
SingleModuleGraphBuilderNode::ChunkableReference { ref ref_data, .. } => {
15941609
match &ref_data.chunking_type {
15951610
ChunkingType::Traced => {
1596-
VisitControlFlow::Skip((edge.to, edge.export, edge.import))
1611+
VisitControlFlow::Skip((edge.to, edge.reference, edge.export, edge.import))
15971612
}
1598-
_ => VisitControlFlow::Continue((edge.to, edge.export, edge.import)),
1613+
_ => VisitControlFlow::Continue((
1614+
edge.to,
1615+
edge.reference,
1616+
edge.export,
1617+
edge.import,
1618+
)),
15991619
}
16001620
}
16011621
// Module was already visited previously
16021622
SingleModuleGraphBuilderNode::VisitedModule { .. } => {
1603-
VisitControlFlow::Skip((edge.to, edge.export, edge.import))
1623+
VisitControlFlow::Skip((edge.to, edge.reference, edge.export, edge.import))
16041624
}
16051625
}
16061626
}
@@ -1609,7 +1629,12 @@ impl Visit<(SingleModuleGraphBuilderNode, ExportUsage, ImportUsage)>
16091629
&mut self,
16101630
// The `skip_duplicates_with_key()` above ensures only a single `edges()` call per module
16111631
// (and not per `(module, export)` pair), so the export must not be read here!
1612-
(node, ..): &(SingleModuleGraphBuilderNode, ExportUsage, ImportUsage),
1632+
(node, ..): &(
1633+
SingleModuleGraphBuilderNode,
1634+
Option<ResolvedVc<Box<dyn ModuleReference>>>,
1635+
ExportUsage,
1636+
ImportUsage,
1637+
),
16131638
) -> Self::EdgesFuture {
16141639
// Destructure beforehand to not have to clone the whole node when entering the async block
16151640
let (module, chunkable_ref_target) = match node {
@@ -1618,7 +1643,12 @@ impl Visit<(SingleModuleGraphBuilderNode, ExportUsage, ImportUsage)>
16181643
target, ref_data, ..
16191644
} => (
16201645
None,
1621-
Some((*target, ref_data.export.clone(), ref_data.import.clone())),
1646+
Some((
1647+
*target,
1648+
ref_data.reference,
1649+
ref_data.export.clone(),
1650+
ref_data.import.clone(),
1651+
)),
16221652
),
16231653
// These are always skipped in `visit()`
16241654
SingleModuleGraphBuilderNode::VisitedModule { .. } => unreachable!(),
@@ -1638,12 +1668,12 @@ impl Visit<(SingleModuleGraphBuilderNode, ExportUsage, ImportUsage)>
16381668
};
16391669

16401670
refs.iter()
1641-
.flat_map(|(ty, export, import, modules)| {
1642-
modules
1643-
.iter()
1644-
.map(|m| (ty.clone(), export.clone(), import.clone(), *m))
1671+
.flat_map(|(reference, ty, export, import, modules)| {
1672+
modules.iter().map(|m| {
1673+
(*reference, ty.clone(), export.clone(), import.clone(), *m)
1674+
})
16451675
})
1646-
.map(async |(ty, export, import, target)| {
1676+
.map(async |(reference, ty, export, import, target)| {
16471677
let to = if ty == COMMON_CHUNKING_TYPE {
16481678
if let Some(idx) = visited_modules.get(&target) {
16491679
SingleModuleGraphBuilderNode::new_visited_module(target, *idx)
@@ -1660,16 +1690,22 @@ impl Visit<(SingleModuleGraphBuilderNode, ExportUsage, ImportUsage)>
16601690
chunking_type: ty,
16611691
export: export.clone(),
16621692
import: import.clone(),
1693+
reference,
16631694
},
16641695
)
16651696
.await?
16661697
};
1667-
Ok(SingleModuleGraphBuilderEdge { to, export, import })
1698+
Ok(SingleModuleGraphBuilderEdge {
1699+
to,
1700+
export,
1701+
import,
1702+
reference: Some(reference),
1703+
})
16681704
})
16691705
.try_join()
16701706
.await?
16711707
}
1672-
(None, Some((chunkable_ref_target, export, import))) => {
1708+
(None, Some((chunkable_ref_target, reference, export, import))) => {
16731709
vec![SingleModuleGraphBuilderEdge {
16741710
to: if let Some(idx) = visited_modules.get(&chunkable_ref_target) {
16751711
SingleModuleGraphBuilderNode::new_visited_module(
@@ -1685,6 +1721,7 @@ impl Visit<(SingleModuleGraphBuilderNode, ExportUsage, ImportUsage)>
16851721
},
16861722
export,
16871723
import,
1724+
reference: Some(reference),
16881725
}]
16891726
}
16901727
_ => unreachable!(),
@@ -1694,7 +1731,12 @@ impl Visit<(SingleModuleGraphBuilderNode, ExportUsage, ImportUsage)>
16941731

16951732
fn span(
16961733
&mut self,
1697-
(node, ..): &(SingleModuleGraphBuilderNode, ExportUsage, ImportUsage),
1734+
(node, ..): &(
1735+
SingleModuleGraphBuilderNode,
1736+
Option<ResolvedVc<Box<dyn ModuleReference>>>,
1737+
ExportUsage,
1738+
ImportUsage,
1739+
),
16981740
) -> tracing::Span {
16991741
if !self.emit_spans {
17001742
return Span::current();

0 commit comments

Comments
 (0)