Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce wrapper tasks by eagerly resolving Vcs #2933

Merged
merged 5 commits into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions crates/turbopack-core/src/resolve/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,7 @@ pub async fn read_matches(
} else {
context.try_join(str).await?
} {
let fs_path = fs_path.resolve().await?;
// This explicit deref of `context` is necessary
#[allow(clippy::explicit_auto_deref)]
let should_match =
Expand All @@ -675,23 +676,22 @@ pub async fn read_matches(
prefix.push_str(str);
match *fs_path.get_type().await? {
FileSystemEntryType::File => results
.push(PatternMatch::File(prefix.to_string(), *fs_path)),
FileSystemEntryType::Directory => results.push(
PatternMatch::Directory(prefix.to_string(), *fs_path),
),
.push(PatternMatch::File(prefix.to_string(), fs_path)),
FileSystemEntryType::Directory => results
.push(PatternMatch::Directory(prefix.to_string(), fs_path)),
FileSystemEntryType::Symlink => {
if let LinkContent::Link { link_type, .. } =
&*fs_path.read_link().await?
{
if link_type.contains(LinkType::DIRECTORY) {
results.push(PatternMatch::Directory(
prefix.clone(),
*fs_path,
fs_path,
));
} else {
results.push(PatternMatch::File(
prefix.clone(),
*fs_path,
fs_path,
))
}
}
Expand All @@ -710,10 +710,11 @@ pub async fn read_matches(
} else {
context.try_join(subpath).await?
} {
let fs_path = fs_path.resolve().await?;
let len = prefix.len();
prefix.push_str(subpath);
nested.push(read_matches(
*fs_path,
fs_path,
prefix.to_string(),
force_in_context,
pattern,
Expand Down
1 change: 1 addition & 0 deletions crates/turbopack-ecmascript/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ impl EcmascriptChunkItem for ModuleChunkItem {
}
}
for c in code_generation.await?.iter() {
let c = c.resolve().await?;
code_gens.push(c.code_generation(context));
}
// need to keep that around to allow references into that
Expand Down
62 changes: 36 additions & 26 deletions crates/turbopack-ecmascript/src/references/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,22 @@ impl AnalyzeEcmascriptModuleResultBuilder {
self.exports = exports;
}

/// Builds the final analysis result.
pub fn build(self) -> AnalyzeEcmascriptModuleResultVc {
AnalyzeEcmascriptModuleResultVc::cell(AnalyzeEcmascriptModuleResult {
references: AssetReferencesVc::cell(self.references),
code_generation: CodeGenerateablesVc::cell(self.code_gens),
exports: self.exports.into(),
})
/// Builds the final analysis result. Resolves internal Vcs for performance
/// in using them.
pub async fn build(mut self) -> Result<AnalyzeEcmascriptModuleResultVc> {
for r in self.references.iter_mut() {
*r = r.resolve().await?;
}
for c in self.code_gens.iter_mut() {
*c = c.resolve().await?;
}
Ok(AnalyzeEcmascriptModuleResultVc::cell(
AnalyzeEcmascriptModuleResult {
references: AssetReferencesVc::cell(self.references),
code_generation: CodeGenerateablesVc::cell(self.code_gens),
exports: self.exports.into(),
},
))
}
}

Expand All @@ -154,12 +163,6 @@ impl Default for AnalyzeEcmascriptModuleResultBuilder {
}
}

impl From<AnalyzeEcmascriptModuleResultBuilder> for AnalyzeEcmascriptModuleResultVc {
fn from(builder: AnalyzeEcmascriptModuleResultBuilder) -> Self {
builder.build()
}
}

#[turbo_tasks::function]
pub(crate) async fn analyze_ecmascript_module(
source: AssetVc,
Expand Down Expand Up @@ -273,6 +276,25 @@ pub(crate) async fn analyze_ecmascript_module(
title: None,
},
);
let var_graph = HANDLER.set(&handler, || {
GLOBALS.set(globals, || create_graph(program, eval_context))
});

for (src, annotations) in eval_context.imports.references() {
let r = EsmAssetReferenceVc::new(
origin,
RequestVc::parse(Value::new(src.to_string().into())),
Value::new(annotations.clone()),
);
import_references.push(r);
}
for r in import_references.iter_mut() {
// Resolving these references here avoids many resolve wrapper tasks when
// passing that to other turbo tasks functions later.
*r = r.resolve().await?;
analysis.add_reference(*r);
}

let (
mut var_graph,
webpack_runtime,
Expand All @@ -282,18 +304,6 @@ pub(crate) async fn analyze_ecmascript_module(
esm_star_exports,
) = HANDLER.set(&handler, || {
GLOBALS.set(globals, || {
let var_graph = create_graph(program, eval_context);

for (src, annotations) in eval_context.imports.references() {
let r = EsmAssetReferenceVc::new(
origin,
RequestVc::parse(Value::new(src.to_string().into())),
Value::new(annotations.clone()),
);
import_references.push(r);
analysis.add_reference(r);
}

// TODO migrate to effects
let mut visitor = AssetReferencesVisitor::new(
eval_context,
Expand Down Expand Up @@ -1176,7 +1186,7 @@ pub(crate) async fn analyze_ecmascript_module(
ParseResult::Unparseable | ParseResult::NotFound => {}
};

Ok(analysis.build())
analysis.build().await
}

fn analyze_amd_define(
Expand Down