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

chore(turbopack-ecmascript): Remove more _boxed async recursion helpers, and remove the async-recursion macro #69805

Merged
merged 1 commit into from
Sep 9, 2024
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
14 changes: 0 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ async-compression = { version = "0.3.13", default-features = false, features = [
"gzip",
"tokio",
] }
async-recursion = "1.0.2"
async-trait = "0.1.64"
atty = "0.2.14"
bytes = "1.1.0"
Expand Down
1 change: 0 additions & 1 deletion crates/next-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ workspace = true

[dependencies]
anyhow = { workspace = true }
async-recursion = { workspace = true }
async-trait = { workspace = true }
base64 = "0.21.0"
lazy-regex = "3.0.1"
Expand Down
4 changes: 1 addition & 3 deletions crates/next-core/src/app_segment_config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::ops::Deref;

use anyhow::{bail, Result};
use async_recursion::async_recursion;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use swc_core::{
Expand Down Expand Up @@ -478,7 +477,6 @@ pub async fn parse_segment_config_from_loader_tree(
.cell())
}

#[async_recursion]
pub async fn parse_segment_config_from_loader_tree_internal(
loader_tree: &LoaderTree,
) -> Result<NextSegmentConfig> {
Expand All @@ -488,7 +486,7 @@ pub async fn parse_segment_config_from_loader_tree_internal(
.parallel_routes
.values()
.map(|loader_tree| async move {
parse_segment_config_from_loader_tree_internal(loader_tree).await
Box::pin(parse_segment_config_from_loader_tree_internal(loader_tree)).await
})
.try_join()
.await?;
Expand Down
4 changes: 1 addition & 3 deletions crates/next-core/src/loader_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::{
};

use anyhow::Result;
use async_recursion::async_recursion;
use indexmap::IndexMap;
use indoc::formatdoc;
use turbo_tasks::{RcStr, Value, ValueToString, Vc};
Expand Down Expand Up @@ -355,7 +354,6 @@ impl LoaderTreeBuilder {
Ok(())
}

#[async_recursion]
async fn walk_tree(&mut self, loader_tree: &LoaderTree, root: bool) -> Result<()> {
use std::fmt::Write;

Expand Down Expand Up @@ -404,7 +402,7 @@ impl LoaderTreeBuilder {
// add parallel_routes
for (key, parallel_route) in parallel_routes.iter() {
write!(self.loader_tree_code, "{key}: ", key = StringifyJs(key))?;
self.walk_tree(parallel_route, false).await?;
Box::pin(self.walk_tree(parallel_route, false)).await?;
writeln!(self.loader_tree_code, ",")?;
}
writeln!(self.loader_tree_code, "}}, {{")?;
Expand Down
12 changes: 5 additions & 7 deletions turbopack/crates/turbopack-bench/src/util/prepared_app.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::{
future::Future,
path::{Path, PathBuf},
pin::Pin,
process::Child,
};

Expand All @@ -19,11 +18,10 @@ use url::Url;

use crate::{bundlers::Bundler, util::PageGuard, BINDING_NAME};

fn copy_dir_boxed(
from: PathBuf,
to: PathBuf,
) -> Pin<Box<dyn Future<Output = anyhow::Result<()>> + Sync + Send>> {
Box::pin(copy_dir(from, to))
// HACK: Needed so that `copy_dir`'s `Future` can be inferred as `Send`:
// https://github.com/rust-lang/rust/issues/123072
fn copy_dir_send(from: PathBuf, to: PathBuf) -> impl Future<Output = anyhow::Result<()>> + Send {
copy_dir(from, to)
}

async fn copy_dir(from: PathBuf, to: PathBuf) -> anyhow::Result<()> {
Expand All @@ -37,7 +35,7 @@ async fn copy_dir(from: PathBuf, to: PathBuf) -> anyhow::Result<()> {
if ty.is_dir() {
jobs.push(tokio::spawn(async move {
tokio::fs::create_dir(&to).await?;
copy_dir_boxed(entry.path(), to).await
copy_dir_send(entry.path(), to).await
}));
} else if ty.is_file() {
file_futures.push(async move {
Expand Down
1 change: 0 additions & 1 deletion turbopack/crates/turbopack-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ workspace = true

[dependencies]
anyhow = { workspace = true }
async-recursion = { workspace = true }
async-trait = { workspace = true }
auto-hash-map = { workspace = true }
browserslist-rs = { workspace = true }
Expand Down
20 changes: 7 additions & 13 deletions turbopack/crates/turbopack-core/src/chunk/chunking.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use std::{
borrow::Cow,
mem::{replace, take},
pin::Pin,
};

use anyhow::Result;
use futures::Future;
use indexmap::IndexMap;
use once_cell::sync::Lazy;
use regex::Regex;
Expand Down Expand Up @@ -255,16 +253,6 @@ async fn package_name_split(
Ok(())
}

/// A boxed version of [folder_split] for recursion.
fn folder_split_boxed<'a, 'b>(
chunk_items: Vec<ChunkItemWithInfo>,
location: usize,
name: Cow<'a, str>,
split_context: &'a mut SplitContext<'b>,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'a>> {
Box::pin(folder_split(chunk_items, location, name, split_context))
}

/// Split chunk items by folder structure.
#[tracing::instrument(level = Level::TRACE, skip_all, fields(name = display(&name), location))]
async fn folder_split(
Expand Down Expand Up @@ -306,7 +294,13 @@ async fn folder_split(
let mut key = format!("{}-{}", name, folder_name);
if !handle_split_group(&mut list, &mut key, split_context, Some(&mut remaining)).await? {
if let Some(new_location) = new_location {
folder_split_boxed(list, new_location, Cow::Borrowed(&name), split_context).await?;
Box::pin(folder_split(
list,
new_location,
Cow::Borrowed(&name),
split_context,
))
.await?;
} else {
make_chunk(list, &mut key, split_context).await?;
}
Expand Down
8 changes: 3 additions & 5 deletions turbopack/crates/turbopack-core/src/condition.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use anyhow::Result;
use async_recursion::async_recursion;
use futures::{stream, StreamExt};
use serde::{Deserialize, Serialize};
use turbo_tasks::{trace::TraceRawVcs, Vc};
Expand Down Expand Up @@ -31,7 +30,6 @@ impl ContextCondition {
ContextCondition::Not(Box::new(condition))
}

#[async_recursion]
/// Returns true if the condition matches the context.
pub async fn matches(&self, path: &FileSystemPath) -> Result<bool> {
match self {
Expand All @@ -40,7 +38,7 @@ impl ContextCondition {
#[allow(clippy::manual_try_fold)]
stream::iter(conditions)
.fold(Ok(true), |acc, c| async move {
Ok(acc? && c.matches(path).await?)
Ok(acc? && Box::pin(c.matches(path)).await?)
})
.await
}
Expand All @@ -49,11 +47,11 @@ impl ContextCondition {
#[allow(clippy::manual_try_fold)]
stream::iter(conditions)
.fold(Ok(false), |acc, c| async move {
Ok(acc? || c.matches(path).await?)
Ok(acc? || Box::pin(c.matches(path)).await?)
})
.await
}
ContextCondition::Not(condition) => condition.matches(path).await.map(|b| !b),
ContextCondition::Not(condition) => Box::pin(condition.matches(path)).await.map(|b| !b),
ContextCondition::InPath(other_path) => {
Ok(path.is_inside_or_equal_ref(&*other_path.await?))
}
Expand Down
50 changes: 13 additions & 37 deletions turbopack/crates/turbopack-core/src/resolve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::{
fmt::{Display, Formatter, Write},
future::Future,
iter::once,
pin::Pin,
};

use anyhow::{bail, Result};
Expand Down Expand Up @@ -1595,14 +1594,6 @@ async fn resolve_internal(
resolve_internal_inline(lookup_path, request, options).await
}

fn resolve_internal_boxed(
lookup_path: Vc<FileSystemPath>,
request: Vc<Request>,
options: Vc<ResolveOptions>,
) -> Pin<Box<dyn Future<Output = Result<Vc<ResolveResult>>> + Send>> {
Box::pin(resolve_internal_inline(lookup_path, request, options))
}

async fn resolve_internal_inline(
lookup_path: Vc<FileSystemPath>,
request: Vc<Request>,
Expand Down Expand Up @@ -1661,7 +1652,7 @@ async fn resolve_internal_inline(
Request::Alternatives { requests } => {
let results = requests
.iter()
.map(|req| resolve_internal_boxed(lookup_path, *req, options))
.map(|req| Box::pin(resolve_internal_inline(lookup_path, *req, options)))
.try_join()
.await?;

Expand Down Expand Up @@ -1790,11 +1781,11 @@ async fn resolve_internal_inline(
.emit();
}

resolve_internal_boxed(
Box::pin(resolve_internal_inline(
lookup_path.root().resolve().await?,
relative.resolve().await?,
options,
)
))
.await?
}
Request::Windows {
Expand Down Expand Up @@ -2425,8 +2416,12 @@ async fn resolve_module_request(
path.clone(),
]);
let relative = Request::relative(Value::new(pattern), query, fragment, true);
let relative_result =
resolve_internal_boxed(lookup_path, relative.resolve().await?, options).await?;
let relative_result = Box::pin(resolve_internal_inline(
lookup_path,
relative.resolve().await?,
options,
))
.await?;
let relative_result = relative_result
.with_replaced_request_key(module_prefix, Value::new(RequestKey::new(module.into())));

Expand Down Expand Up @@ -2545,14 +2540,14 @@ async fn resolve_import_map_result(
let results = list
.iter()
.map(|result| {
resolve_import_map_result_boxed(
Box::pin(resolve_import_map_result(
result,
lookup_path,
original_lookup_path,
original_request,
options,
query,
)
))
})
.try_join()
.await?;
Expand All @@ -2563,26 +2558,6 @@ async fn resolve_import_map_result(
})
}

type ResolveImportMapResult = Result<Option<Vc<ResolveResult>>>;

fn resolve_import_map_result_boxed<'a>(
result: &'a ImportMapResult,
lookup_path: Vc<FileSystemPath>,
original_lookup_path: Vc<FileSystemPath>,
original_request: Vc<Request>,
options: Vc<ResolveOptions>,
query: Vc<RcStr>,
) -> Pin<Box<dyn Future<Output = ResolveImportMapResult> + Send + 'a>> {
Box::pin(resolve_import_map_result(
result,
lookup_path,
original_lookup_path,
original_request,
options,
query,
))
}

#[tracing::instrument(level = Level::TRACE, skip_all)]
async fn resolved(
request_key: RequestKey,
Expand Down Expand Up @@ -2684,7 +2659,8 @@ async fn handle_exports_imports_field(
result_path,
])));

let resolve_result = resolve_internal_boxed(package_path, request, options).await?;
let resolve_result =
Box::pin(resolve_internal_inline(package_path, request, options)).await?;
if conditions.is_empty() {
resolved_results.push(resolve_result.with_request(path.into()));
} else {
Expand Down
14 changes: 1 addition & 13 deletions turbopack/crates/turbopack-core/src/resolve/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ async fn import_mapping_to_result(
}
ReplacedImportMapping::Alternatives(list) => ImportMapResult::Alternatives(
list.iter()
.map(|mapping| import_mapping_to_result_boxed(*mapping, lookup_path, request))
.map(|mapping| Box::pin(import_mapping_to_result(*mapping, lookup_path, request)))
.try_join()
.await?,
),
Expand Down Expand Up @@ -384,18 +384,6 @@ impl ValueToString for ImportMapResult {
}
}

// This cannot be inlined within `import_mapping_to_result`, otherwise we run
// into the following error:
// cycle detected when computing type of
// `resolve::options::import_mapping_to_result::{opaque#0}`
fn import_mapping_to_result_boxed(
mapping: Vc<ReplacedImportMapping>,
lookup_path: Vc<FileSystemPath>,
request: Vc<Request>,
) -> Pin<Box<dyn Future<Output = Result<ImportMapResult>> + Send>> {
Box::pin(async move { import_mapping_to_result(mapping, lookup_path, request).await })
}

impl ImportMap {
// Not a turbo-tasks function: the map lookup should be cheaper than the cache
// lookup
Expand Down
Loading
Loading