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

Performance Improvements (4) #6055

Merged
merged 4 commits into from
Sep 29, 2023
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
7 changes: 5 additions & 2 deletions crates/turbopack-core/src/issue/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,12 @@ impl Issue for ResolvingIssue {
request_type = self.request_type,
)?;
if let Some(import_map) = &self.resolve_options.await?.import_map {
let result = import_map.lookup(self.file_path, self.request);
let result = import_map
.await?
.lookup(self.file_path, self.request)
.await?;

match result.to_string().await {
match result.cell().to_string().await {
Ok(str) => writeln!(detail, "Import map: {}", str)?,
Err(err) => {
writeln!(
Expand Down
63 changes: 37 additions & 26 deletions crates/turbopack-core/src/resolve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1092,11 +1092,10 @@ async fn resolve_internal(

// Apply import mappings if provided
if let Some(import_map) = &options_value.import_map {
let result_ref = import_map.lookup(lookup_path, request).await?;
let result = &*result_ref;
let result = import_map.await?.lookup(lookup_path, request).await?;
if !matches!(result, ImportMapResult::NoEntry) {
let resolved_result = resolve_import_map_result(
result,
&result,
lookup_path,
lookup_path,
request,
Expand Down Expand Up @@ -1137,7 +1136,7 @@ async fn resolve_internal(
lookup_path,
"".to_string(),
*force_in_lookup_dir,
Pattern::new(path.clone()),
Pattern::new(path.clone()).resolve().await?,
)
.await?;

Expand All @@ -1162,21 +1161,25 @@ async fn resolve_internal(
query,
force_in_lookup_dir,
} => {
let mut patterns = vec![path.clone()];
let mut requests = vec![Request::raw(
Value::new(path.clone()),
*query,
*force_in_lookup_dir,
)];
for ext in options_value.extensions.iter() {
let mut path = path.clone();
path.push(ext.clone().into());
patterns.push(path);
requests.push(Request::raw(Value::new(path), *query, *force_in_lookup_dir));
}

// This ensures the order of the patterns (extensions) is
// This ensures the order of the requests (extensions) is
// preserved, `Pattern::Alternatives` inside a `Request::Raw` does not preserve
// the order
let mut results = Vec::new();
for pattern in patterns {
for request in requests {
results.push(resolve_internal(
lookup_path,
Request::raw(Value::new(pattern), *query, *force_in_lookup_dir),
request.resolve().await?,
options,
));
}
Expand Down Expand Up @@ -1212,7 +1215,11 @@ async fn resolve_internal(
.cell()
.emit();

resolve_internal(lookup_path.root(), relative, options)
resolve_internal(
lookup_path.root().resolve().await?,
relative.resolve().await?,
options,
)
}
Request::Windows { path: _, query: _ } => {
ResolvingIssue {
Expand Down Expand Up @@ -1280,10 +1287,9 @@ async fn resolve_internal(
// Apply fallback import mappings if provided
if let Some(import_map) = &options_value.fallback_import_map {
if *result.is_unresolveable().await? {
let result_ref = import_map.lookup(lookup_path, request).await?;
let result = &*result_ref;
let result = import_map.await?.lookup(lookup_path, request).await?;
let resolved_result = resolve_import_map_result(
result,
&result,
lookup_path,
lookup_path,
request,
Expand Down Expand Up @@ -1318,7 +1324,11 @@ async fn resolve_into_folder(
)
})?;
let request = Request::parse(Value::new(str.into()));
return Ok(resolve_internal(package_path, request, options));
return Ok(resolve_internal(
package_path,
request.resolve().await?,
options,
));
}
ResolveIntoPackage::MainField(name) => {
if let Some(package_json) = &*read_package_json(package_json_path).await? {
Expand Down Expand Up @@ -1500,7 +1510,11 @@ async fn resolve_module_request(
new_pat.push_front(".".to_string().into());

let relative = Request::relative(Value::new(new_pat), query, true);
results.push(resolve_internal(*package_path, relative, options));
results.push(resolve_internal(
*package_path,
relative.resolve().await?,
options,
));
}

Ok(merge_results_with_affecting_sources(
Expand Down Expand Up @@ -1565,17 +1579,14 @@ fn resolve_import_map_result_boxed<'a>(
options: Vc<ResolveOptions>,
query: Vc<String>,
) -> Pin<Box<dyn Future<Output = ResolveImportMapResult> + Send + 'a>> {
Box::pin(async move {
resolve_import_map_result(
result,
lookup_path,
original_lookup_path,
original_request,
options,
query,
)
.await
})
Box::pin(resolve_import_map_result(
result,
lookup_path,
original_lookup_path,
original_request,
options,
query,
))
}

async fn resolve_alias_field_result(
Expand Down
18 changes: 8 additions & 10 deletions crates/turbopack-core/src/resolve/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,28 +345,26 @@ fn import_mapping_to_result_boxed(
Box::pin(async move { import_mapping_to_result(mapping, lookup_path, request).await })
}

#[turbo_tasks::value_impl]
impl ImportMap {
#[turbo_tasks::function]
// Not a turbo-tasks function: the map lookup should be cheaper than the cache
// lookup
pub async fn lookup(
self: Vc<Self>,
&self,
lookup_path: Vc<FileSystemPath>,
request: Vc<Request>,
) -> Result<Vc<ImportMapResult>> {
let this = self.await?;
) -> Result<ImportMapResult> {
// TODO lookup pattern
if let Some(request_string) = request.await?.request() {
if let Some(result) = this.map.lookup(&request_string).next() {
return Ok(import_mapping_to_result(
if let Some(result) = self.map.lookup(&request_string).next() {
return import_mapping_to_result(
result.try_join_into_self().await?.into_owned(),
lookup_path,
request,
)
.await?
.into());
.await;
}
}
Ok(ImportMapResult::NoEntry.into())
Ok(ImportMapResult::NoEntry)
}
}

Expand Down
16 changes: 10 additions & 6 deletions crates/turbopack-core/src/resolve/origin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,16 @@ async fn resolve_asset(
if let Some(asset) = *resolve_origin.get_inner_asset(request).await? {
return Ok(ModuleResolveResult::module(asset).cell());
}
Ok(resolve_origin.asset_context().resolve_asset(
resolve_origin.origin_path(),
request,
options,
reference_type,
))
Ok(resolve_origin
.asset_context()
.resolve()
.await?
.resolve_asset(
resolve_origin.origin_path().resolve().await?,
request,
options,
reference_type,
))
}

/// A resolve origin for some path and context without additional modifications.
Expand Down
8 changes: 4 additions & 4 deletions crates/turbopack-ecmascript/src/references/esm/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,19 +151,19 @@ impl EsmAssetReference {
#[turbo_tasks::value_impl]
impl ModuleReference for EsmAssetReference {
#[turbo_tasks::function]
fn resolve_reference(&self) -> Vc<ModuleResolveResult> {
async fn resolve_reference(&self) -> Result<Vc<ModuleResolveResult>> {
let ty = Value::new(match &self.export_name {
Some(part) => EcmaScriptModulesReferenceSubType::ImportPart(*part),
None => EcmaScriptModulesReferenceSubType::Undefined,
});

esm_resolve(
self.get_origin(),
Ok(esm_resolve(
self.get_origin().resolve().await?,
self.request,
ty,
OptionIssueSource::none(),
IssueSeverity::Error.cell(),
)
))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl CodeGenerateableWithAvailabilityInfo for EsmAsyncAssetReference {
self.origin,
self.request,
Value::new(EcmaScriptModulesReferenceSubType::Undefined),
OptionIssueSource::some(self.issue_source),
OptionIssueSource::some(self.issue_source).resolve().await?,
try_to_severity(self.in_try),
),
Value::new(EsmAsync(availability_info.into_value())),
Expand Down
22 changes: 18 additions & 4 deletions crates/turbopack/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ impl AssetContext for ModuleAssetContext {
let context_path = origin_path.parent().resolve().await?;

let result = resolve(context_path, request, resolve_options);
let mut result = self.process_resolve_result(result, reference_type);
let mut result = self.process_resolve_result(result.resolve().await?, reference_type);

if *self.is_types_resolving_enabled().await? {
let types_reference = TypescriptTypesAssetReference::new(
Expand All @@ -464,22 +464,36 @@ impl AssetContext for ModuleAssetContext {
result: Vc<ResolveResult>,
reference_type: Value<ReferenceType>,
) -> Result<Vc<ModuleResolveResult>> {
let this = self.await?;
let transition = this.transition;
Ok(result
.await?
.map_module(
|source| {
let reference_type = reference_type.clone();
async move {
Ok(Vc::upcast(
self.process(source, reference_type).resolve().await?,
))
if let Some(transition) = transition {
Ok(Vc::upcast(
transition
.process(source, self, reference_type)
.resolve()
.await?,
))
} else {
Ok(Vc::upcast(
self.process_default(source, reference_type)
.resolve()
.await?,
))
}
}
},
|i| async move { Ok(Vc::upcast(AffectingResolvingAssetReference::new(i))) },
)
.await?
.into())
}

#[turbo_tasks::function]
async fn process(
self: Vc<Self>,
Expand Down
Loading