Unverified Commit f1908dad authored by xusd320's avatar xusd320 Committed by GitHub

perf(turbopack): optimize resolve plugin handling (#88639)

parent 06421057
...@@ -45,7 +45,7 @@ use crate::{ ...@@ -45,7 +45,7 @@ use crate::{
origin::ResolveOrigin, origin::ResolveOrigin,
parse::{Request, stringify_data_uri}, parse::{Request, stringify_data_uri},
pattern::{Pattern, PatternMatch, read_matches}, pattern::{Pattern, PatternMatch, read_matches},
plugin::{AfterResolvePlugin, BeforeResolvePlugin}, plugin::{AfterResolvePlugin, AfterResolvePluginCondition, BeforeResolvePlugin},
remap::{ExportsField, ImportsField, ReplacedSubpathValueResult}, remap::{ExportsField, ImportsField, ReplacedSubpathValueResult},
}, },
source::{OptionSource, Source, Sources}, source::{OptionSource, Source, Sources},
...@@ -67,6 +67,12 @@ pub use remap::{ResolveAliasMap, SubpathValue}; ...@@ -67,6 +67,12 @@ pub use remap::{ResolveAliasMap, SubpathValue};
use crate::{error::PrettyPrintError, issue::IssueSeverity}; use crate::{error::PrettyPrintError, issue::IssueSeverity};
/// Type alias for a resolved after-resolve plugin paired with its condition.
type AfterResolvePluginWithCondition = (
ResolvedVc<Box<dyn AfterResolvePlugin>>,
Vc<AfterResolvePluginCondition>,
);
#[turbo_tasks::value(shared)] #[turbo_tasks::value(shared)]
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum ModuleResolveResultItem { pub enum ModuleResolveResultItem {
...@@ -1180,7 +1186,8 @@ enum ImportsFieldResult { ...@@ -1180,7 +1186,8 @@ enum ImportsFieldResult {
#[turbo_tasks::function] #[turbo_tasks::function]
async fn imports_field(lookup_path: FileSystemPath) -> Result<Vc<ImportsFieldResult>> { async fn imports_field(lookup_path: FileSystemPath) -> Result<Vc<ImportsFieldResult>> {
// We don't need to collect affecting sources here because we don't use them // We don't need to collect affecting sources here because we don't use them
let package_json_context = find_context_file(lookup_path, package_json(), false).await?; let package_json_context =
find_context_file(lookup_path, package_json().resolve().await?, false).await?;
let FindContextFileResult::Found(package_json_path, _refs) = &*package_json_context else { let FindContextFileResult::Found(package_json_path, _refs) = &*package_json_context else {
return Ok(ImportsFieldResult::None.cell()); return Ok(ImportsFieldResult::None.cell());
}; };
...@@ -1598,13 +1605,24 @@ pub async fn resolve_inline( ...@@ -1598,13 +1605,24 @@ pub async fn resolve_inline(
} }
async { async {
let before_plugins_result = handle_before_resolve_plugins( // Pre-fetch options once to avoid repeated await calls
lookup_path.clone(), let options_value = options.await?;
reference_type.clone(),
request, // Fast path: skip plugin handling if no plugins are configured
options, let has_before_plugins = !options_value.before_resolve_plugins.is_empty();
) let has_after_plugins = !options_value.after_resolve_plugins.is_empty();
.await?;
let before_plugins_result = if has_before_plugins {
handle_before_resolve_plugins(
lookup_path.clone(),
reference_type.clone(),
request,
options,
)
.await?
} else {
None
};
let raw_result = match before_plugins_result { let raw_result = match before_plugins_result {
Some(result) => result, Some(result) => result,
...@@ -1615,9 +1633,13 @@ pub async fn resolve_inline( ...@@ -1615,9 +1633,13 @@ pub async fn resolve_inline(
} }
}; };
let result = let result = if has_after_plugins {
handle_after_resolve_plugins(lookup_path, reference_type, request, options, raw_result) handle_after_resolve_plugins(lookup_path, reference_type, request, options, raw_result)
.await?; .await?
} else {
raw_result
};
Ok(result) Ok(result)
} }
.instrument(span) .instrument(span)
...@@ -1685,7 +1707,9 @@ async fn handle_before_resolve_plugins( ...@@ -1685,7 +1707,9 @@ async fn handle_before_resolve_plugins(
request: Vc<Request>, request: Vc<Request>,
options: Vc<ResolveOptions>, options: Vc<ResolveOptions>,
) -> Result<Option<Vc<ResolveResult>>> { ) -> Result<Option<Vc<ResolveResult>>> {
for plugin in &options.await?.before_resolve_plugins { let options_value = options.await?;
for plugin in &options_value.before_resolve_plugins {
let condition = plugin.before_resolve_condition().resolve().await?; let condition = plugin.before_resolve_condition().resolve().await?;
if !*condition.matches(request).await? { if !*condition.matches(request).await? {
continue; continue;
...@@ -1709,15 +1733,25 @@ async fn handle_after_resolve_plugins( ...@@ -1709,15 +1733,25 @@ async fn handle_after_resolve_plugins(
options: Vc<ResolveOptions>, options: Vc<ResolveOptions>,
result: Vc<ResolveResult>, result: Vc<ResolveResult>,
) -> Result<Vc<ResolveResult>> { ) -> Result<Vc<ResolveResult>> {
// Pre-fetch options to avoid repeated await calls in the inner loop
let options_value = options.await?;
// Pre-resolve all plugin conditions once to avoid repeated resolve calls in the loop
let resolved_conditions = options_value
.after_resolve_plugins
.iter()
.map(async |p| Ok((*p, p.after_resolve_condition().resolve().await?)))
.try_join()
.await?;
async fn apply_plugins_to_path( async fn apply_plugins_to_path(
path: FileSystemPath, path: FileSystemPath,
lookup_path: FileSystemPath, lookup_path: FileSystemPath,
reference_type: ReferenceType, reference_type: ReferenceType,
request: Vc<Request>, request: Vc<Request>,
options: Vc<ResolveOptions>, plugins_with_conditions: &[AfterResolvePluginWithCondition],
) -> Result<Option<Vc<ResolveResult>>> { ) -> Result<Option<Vc<ResolveResult>>> {
for plugin in &options.await?.after_resolve_plugins { for (plugin, after_resolve_condition) in plugins_with_conditions {
let after_resolve_condition = plugin.after_resolve_condition().resolve().await?;
if *after_resolve_condition.matches(path.clone()).await? if *after_resolve_condition.matches(path.clone()).await?
&& let Some(result) = *plugin && let Some(result) = *plugin
.after_resolve( .after_resolve(
...@@ -1748,7 +1782,7 @@ async fn handle_after_resolve_plugins( ...@@ -1748,7 +1782,7 @@ async fn handle_after_resolve_plugins(
lookup_path.clone(), lookup_path.clone(),
reference_type.clone(), reference_type.clone(),
request, request,
options, &resolved_conditions,
) )
.await? .await?
{ {
...@@ -2646,7 +2680,8 @@ enum FindSelfReferencePackageResult { ...@@ -2646,7 +2680,8 @@ enum FindSelfReferencePackageResult {
async fn find_self_reference( async fn find_self_reference(
lookup_path: FileSystemPath, lookup_path: FileSystemPath,
) -> Result<Vc<FindSelfReferencePackageResult>> { ) -> Result<Vc<FindSelfReferencePackageResult>> {
let package_json_context = find_context_file(lookup_path, package_json(), false).await?; let package_json_context =
find_context_file(lookup_path, package_json().resolve().await?, false).await?;
if let FindContextFileResult::Found(package_json_path, _refs) = &*package_json_context { if let FindContextFileResult::Found(package_json_path, _refs) = &*package_json_context {
let read = let read =
read_package_json(Vc::upcast(FileSource::new(package_json_path.clone()))).await?; read_package_json(Vc::upcast(FileSource::new(package_json_path.clone()))).await?;
......
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