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

simplify alternatives handling #70520

Closed
wants to merge 1 commit into from
Closed
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
123 changes: 54 additions & 69 deletions turbopack/crates/turbopack-core/src/resolve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ impl ResolveResult {
}
}

pub fn merge_alternatives(&mut self, other: &ResolveResult) {
pub fn merge_alternatives(mut self, other: &ResolveResult) -> ResolveResult {
for (k, v) in other.primary.iter() {
if !self.primary.contains_key(k) {
self.primary.insert(k.clone(), v.clone());
Expand All @@ -637,6 +637,8 @@ impl ResolveResult {
.filter(|source| !set.contains(source))
.copied(),
);

self
}

pub fn is_unresolvable_ref(&self) -> bool {
Expand Down Expand Up @@ -843,55 +845,19 @@ impl ResolveResult {
Ok(ResolveResult::unresolvable_with_affecting_sources(affecting_sources).into())
}

/// See [alternatives_inner]
#[turbo_tasks::function]
pub async fn alternatives(results: Vec<Vc<ResolveResult>>) -> Result<Vc<Self>> {
if results.len() == 1 {
return Ok(results.into_iter().next().unwrap());
}
let mut iter = results.into_iter().try_join().await?.into_iter();
if let Some(current) = iter.next() {
let mut current = current.clone_value();
for result in iter {
// For clippy -- This explicit deref is necessary
let other = &*result;
current.merge_alternatives(other);
}
Ok(Self::cell(current))
} else {
Ok(Self::cell(ResolveResult::unresolvable()))
}
alternatives_inner(results, None).await
}

/// See [alternatives_inner]
#[turbo_tasks::function]
pub async fn alternatives_with_affecting_sources(
results: Vec<Vc<ResolveResult>>,
affecting_sources: Vec<ResolvedVc<Box<dyn Source>>>,
affecting_sources: Vec<Vc<Box<dyn Source>>>,
) -> Result<Vc<Self>> {
if affecting_sources.is_empty() {
return Ok(Self::alternatives(results));
}
if results.len() == 1 {
return Ok(results
.into_iter()
.next()
.unwrap()
.with_affecting_sources(affecting_sources.into_iter().map(|src| *src).collect()));
}
let mut iter = results.into_iter().try_join().await?.into_iter();
if let Some(current) = iter.next() {
let mut current = current.clone_value();
for result in iter {
// For clippy -- This explicit deref is necessary
let other = &*result;
current.merge_alternatives(other);
}
current.affecting_sources.extend(affecting_sources);
Ok(Self::cell(current))
} else {
Ok(Self::cell(
ResolveResult::unresolvable_with_affecting_sources(affecting_sources),
))
}
alternatives_inner(results, Some(affecting_sources)).await
}

#[turbo_tasks::function]
Expand Down Expand Up @@ -1023,6 +989,43 @@ impl ResolveResult {
}
}

/// Create a new [ResolveResult] from a list of [ResolveResult]s.
///
/// If the list contains only one [ResolveResult], it will be returned
/// directly. Otherwise, a new [ResolveResult] will be created with the
/// alternatives of the list.
///
/// If the list is empty, an unresolveable [ResolveResult] will be returned.
///
/// Note: Some([]) and None are treated the same, just avoiding allocation
async fn alternatives_inner(
results: Vec<Vc<ResolveResult>>,
affecting_sources: Option<Vec<Vc<Box<dyn Source>>>>,
) -> Result<Vc<ResolveResult>> {
let start = match (results.as_slice(), affecting_sources) {
// optimisation to delay resolution for a single result
([result], Some(af)) => return Ok(result.with_affecting_sources(af)),
([result], None) => return Ok(*result),
(_, Some(af)) => ResolveResult::unresolvable_with_affecting_sources(
af.into_iter().map(Vc::to_resolved).try_join().await?,
),
(_, None) => ResolveResult::unresolvable(),
};

let value = results
.into_iter()
.try_join()
.await?
.into_iter()
.fold(start, |current, result| {
// For clippy -- This explicit deref is necessary
let result = &*result;
current.merge_alternatives(result)
});

Ok(ResolveResult::cell(value))
}

#[turbo_tasks::value(transparent)]
pub struct ResolveResultOption(Option<Vc<ResolveResult>>);

Expand Down Expand Up @@ -1366,32 +1369,14 @@ async fn find_package(
}

fn merge_results(results: Vec<Vc<ResolveResult>>) -> Vc<ResolveResult> {
match results.len() {
0 => ResolveResult::unresolvable().into(),
1 => results.into_iter().next().unwrap(),
_ => ResolveResult::alternatives(results),
}
ResolveResult::alternatives(results)
}

fn merge_results_with_affecting_sources(
results: Vec<Vc<ResolveResult>>,
affecting_sources: Vec<ResolvedVc<Box<dyn Source>>>,
affecting_sources: Vec<Vc<Box<dyn Source>>>,
) -> Vc<ResolveResult> {
if affecting_sources.is_empty() {
return merge_results(results);
}
match results.len() {
0 => ResolveResult::unresolvable_with_affecting_sources(affecting_sources).cell(),
1 => results
.into_iter()
.next()
.unwrap()
.with_affecting_sources(affecting_sources.into_iter().map(|src| *src).collect()),
_ => ResolveResult::alternatives_with_affecting_sources(
results,
affecting_sources.into_iter().map(|src| *src).collect(),
),
}
ResolveResult::alternatives_with_affecting_sources(results, affecting_sources)
}

#[turbo_tasks::function]
Expand Down Expand Up @@ -2484,9 +2469,11 @@ async fn resolve_module_request(
}
}

let module_result =
merge_results_with_affecting_sources(results, result.affecting_sources.clone())
.with_replaced_request_key(".".into(), Value::new(RequestKey::new(module.into())));
let module_result = merge_results_with_affecting_sources(
results,
result.affecting_sources.iter().map(|src| **src).collect(),
)
.with_replaced_request_key(".".into(), Value::new(RequestKey::new(module.into())));

if options_value.prefer_relative {
let module_prefix: RcStr = format!("./{module}").into();
Expand Down Expand Up @@ -2818,9 +2805,7 @@ async fn handle_exports_imports_field(
// other options do not apply anymore when an exports field exist
Ok(merge_results_with_affecting_sources(
resolved_results,
vec![ResolvedVc::upcast(
FileSource::new(package_json_path).to_resolved().await?,
)],
vec![Vc::upcast(FileSource::new(package_json_path))],
))
}

Expand Down
Loading