Unverified Commit 16c8ea5d authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

Prefer using join over try_join when you don't want a vec (#98547)

Adopt `join` over `try_join` in a number of places to avoid temporary
Vec allocations

`try_join` always copies a `Vec<Result<T>>` to a `Result<Vec<T>>` which
is sometimes convenient but at least sometimes wasteful if you are
immediately transforming into another collection type.

Adopt it to turn `primary_modules` into a `SmallVec` since in the vast
majority of cases there is only one module and so we can save a bunch of
allocations of tiny arrays

---------
Co-authored-by: 's avatarClaude Opus 5 (1M context) <noreply@anthropic.com>
parent 93bb22ad
......@@ -10496,6 +10496,7 @@ dependencies = [
"either",
"erased-serde",
"indexmap 2.14.0",
"itertools 0.10.5",
"num-bigint",
"patricia_tree",
"petgraph 0.8.3",
......
......@@ -24,8 +24,8 @@ bincode = { workspace = true }
byteorder = { workspace = true }
either = { workspace = true }
futures = { workspace = true }
indexmap = { workspace = true }
itertools = { workspace = true }
indexmap = { workspace = true }
next-core = { workspace = true }
regex = { workspace = true }
roaring = { workspace = true }
......
......@@ -8,7 +8,7 @@ use rustc_hash::{FxHashMap, FxHashSet};
use serde::Serialize;
use turbo_rcstr::RcStr;
use turbo_tasks::{
FxIndexSet, ResolvedVc, TryFlatJoinIterExt, TryJoinIterExt, ValueToString, ValueToStringRef, Vc,
FxIndexSet, JoinIterExt, ResolvedVc, TryFlatJoinIterExt, ValueToString, ValueToStringRef, Vc,
};
use turbo_tasks_fs::{
File, FileContent, FileSystemPath,
......@@ -555,19 +555,19 @@ pub async fn analyze_module_graphs(module_graph: Vc<ModuleGraph>) -> Result<Vc<F
Ok(Some((from_ident, to_ident)))
}
let all_modules = all_modules
let modules = all_modules
.iter()
.copied()
.map(async |module| {
let ident = module.ident().to_string().owned().await?;
let path = module.ident().await?.path.to_string_ref().await?;
Ok((ident, path))
anyhow::Ok((ident, path))
})
.try_join()
.await?;
for (ident, path) in &all_modules {
builder.ensure_module(ident, path);
.join()
.await;
for module in modules {
let (ident, path) = module?;
builder.ensure_module(&ident, &path);
}
let all_edges = all_edges
......
......@@ -6,7 +6,7 @@ use next_core::{
};
use rustc_hash::FxHashMap;
use turbo_tasks::{
NonLocalValue, ResolvedVc, TryFlatJoinIterExt, Vc, debug::ValueDebugFormat, trace::TraceRawVcs,
JoinIterExt, NonLocalValue, ResolvedVc, Vc, debug::ValueDebugFormat, trace::TraceRawVcs,
};
use turbopack_core::{module::Module, module_graph::ModuleGraphLayer};
use turbopack_css::chunk::CssChunkPlaceable;
......@@ -65,10 +65,11 @@ pub async fn map_client_references(
Ok(None)
}
})
.try_flat_join()
.await?
.join()
.await
.into_iter()
.collect::<FxHashMap<_, _>>();
.filter_map(Result::transpose)
.collect::<Result<FxHashMap<_, _>>>()?;
Ok(Vc::cell(manifest))
}
......@@ -2,10 +2,11 @@ use std::fmt::Display;
use anyhow::Result;
use bincode::{Decode, Encode};
use itertools::Itertools;
use next_core::app_structure::FileSystemPathVec;
use turbo_rcstr::RcStr;
use turbo_tasks::{
Completion, FxIndexMap, FxIndexSet, NonLocalValue, OperationVc, ResolvedVc, TryFlatJoinIterExt,
Completion, FxIndexMap, FxIndexSet, JoinIterExt, NonLocalValue, OperationVc, ResolvedVc,
TryJoinIterExt, Vc, debug::ValueDebugFormat, trace::TraceRawVcs,
};
use turbopack_core::{
......@@ -187,13 +188,15 @@ async fn output_of_endpoints(endpoints: Vec<Vc<Box<dyn Endpoint>>>) -> Result<Vc
async fn module_graphs_of_endpoints(
endpoints: Vec<Vc<Box<dyn Endpoint>>>,
) -> Result<Vc<ModuleGraphs>> {
// Deduplicate while preserving first-seen order, then hand back a `Vec`.
let module_graphs = endpoints
.iter()
.map(async |endpoint| Ok(endpoint.module_graphs().await?.into_iter()))
.try_flat_join()
.await?
.map(async |endpoint| anyhow::Ok(endpoint.module_graphs().await?.into_iter()))
.join()
.await
.into_iter()
.collect::<FxIndexSet<_>>()
.flatten_ok()
.collect::<Result<FxIndexSet<_>>>()?
.into_iter()
.collect::<Vec<_>>();
Ok(Vc::cell(module_graphs))
......
......@@ -5,7 +5,7 @@ use regex::Regex;
use smallvec::smallvec;
use tracing::Instrument;
use turbo_rcstr::rcstr;
use turbo_tasks::{FxIndexMap, FxIndexSet, ResolvedVc, TryJoinIterExt, ValueToString, Vc};
use turbo_tasks::{FxIndexMap, FxIndexSet, JoinIterExt, ResolvedVc, ValueToString, Vc};
use turbo_tasks_fs::{FileContent, rope::Rope};
use turbopack::{ModuleAssetContext, module_options::CustomModuleType};
use turbopack_core::{
......@@ -202,10 +202,10 @@ impl EcmascriptChunkPlaceable for RawEcmascriptModule {
},
))
})
.try_join()
.await?
.join()
.await
.into_iter()
.collect::<FxIndexMap<_, _>>()
.collect::<Result<FxIndexMap<_, _>>>()?
)
)?;
code += "};\n";
......
......@@ -18,8 +18,7 @@ use swc_core::{
};
use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{
NonLocalValue, ResolvedVc, TryJoinIterExt, ValueDefault, Vc, trace::TraceRawVcs,
util::WrapFuture,
JoinIterExt, NonLocalValue, ResolvedVc, ValueDefault, Vc, trace::TraceRawVcs, util::WrapFuture,
};
use turbo_tasks_fs::FileSystemPath;
use turbopack_core::{
......@@ -1362,11 +1361,11 @@ async fn parse_segment_config_from_loader_tree_internal(
.parallel_routes
.values()
.map(|loader_tree| Box::pin(parse_segment_config_from_loader_tree_internal(loader_tree)))
.try_join()
.await?;
.join()
.await;
for tree in parallel_configs {
config.apply_parallel_config(&tree)?;
config.apply_parallel_config(&tree?)?;
}
let modules = &loader_tree.modules;
......
......@@ -25,6 +25,7 @@ data-encoding = { workspace = true }
either = { workspace = true }
erased-serde = { workspace = true }
indexmap = { workspace = true }
itertools = { workspace = true }
num-bigint = "0.4"
patricia_tree = { version = "0.10.1", features = ["serde"] }
petgraph = { workspace = true, features = ["serde-1"] }
......
use anyhow::Result;
use bincode::{Decode, Encode};
use turbo_tasks::{
FxIndexSet, OperationVc, ReadRef, ResolvedVc, TryJoinIterExt, ValueToString, Vc,
FxIndexSet, JoinIterExt, OperationVc, ReadRef, ResolvedVc, ValueToString, Vc,
trace::TraceRawVcs, turbofmt,
};
use turbo_tasks_hash::Xxh3Hash64Hasher;
......@@ -100,9 +100,10 @@ impl AvailableModules {
.await?
.iter()
.map(async |&module| module.ident_strings().await)
.try_join()
.await?;
.join()
.await;
for idents in item_idents {
let idents = idents?;
match idents {
IdentStrings::Single(ident) => hasher.write_value(ident),
IdentStrings::Multiple(idents) => {
......
......@@ -6,7 +6,8 @@ use rustc_hash::{FxHashMap, FxHashSet};
use tracing::Instrument;
use turbo_rcstr::rcstr;
use turbo_tasks::{
FxIndexSet, OperationVc, ResolvedVc, TryFlatJoinIterExt, TryJoinIterExt, Vc, trace::TraceRawVcs,
FxIndexSet, JoinIterExt, OperationVc, ResolvedVc, TryFlatJoinIterExt, TryJoinIterExt, Vc,
trace::TraceRawVcs,
};
use super::{
......@@ -90,11 +91,11 @@ pub async fn make_chunk_group(
*chunking_context,
)
})
.try_join()
.await?
.join()
.await
.into_iter()
.flatten()
.collect::<Vec<_>>();
.filter_map(Result::transpose)
.collect::<Result<Vec<_>>>()?;
let chunk_item_batch_groups = batch_groups
.iter()
......
......@@ -18,7 +18,7 @@ use serde::{Deserialize, Serialize};
use tracing::{Instrument, Level, Span};
use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{
CollectiblesSource, FxIndexMap, NonLocalValue, OperationVc, ReadRef, ResolvedVc,
CollectiblesSource, FxIndexMap, JoinIterExt, NonLocalValue, OperationVc, ReadRef, ResolvedVc,
TryFlatJoinIterExt, TryJoinIterExt, ValueToString, Vc,
debug::ValueDebugFormat,
graph::{AdjacencyMap, GraphTraversal, Visit, VisitControlFlow},
......@@ -1184,13 +1184,12 @@ impl ModuleGraphSnapshot {
/// Returns a map of all modules in the graphs to their identifiers.
/// This is primarily useful for debugging.
pub async fn get_ids(&self) -> Result<FxHashMap<ResolvedVc<Box<dyn Module>>, ReadRef<RcStr>>> {
Ok(self
.iter_nodes()
self.iter_nodes()
.map(async |n| Ok((n, n.ident().to_string().await?)))
.try_join()
.await?
.join()
.await
.into_iter()
.collect::<FxHashMap<_, _>>())
.collect()
}
/// Traverses all reachable nodes exactly once and calls the visitor.
......
......@@ -7,14 +7,15 @@ use std::{
use anyhow::{Context, Result, bail};
use bincode::{Decode, Encode};
use either::Either;
use itertools::Itertools;
use petgraph::graph::{DiGraph, EdgeIndex, NodeIndex};
use rustc_hash::{FxHashMap, FxHashSet, FxHasher};
use serde::{Deserialize, Serialize};
use tracing::Instrument;
use turbo_prehash::BuildHasherExt;
use turbo_tasks::{
FxIndexMap, FxIndexSet, NonLocalValue, ResolvedVc, TryJoinIterExt, ValueToString, Vc,
trace::TraceRawVcs, turbobail,
FxIndexMap, FxIndexSet, JoinIterExt, NonLocalValue, ResolvedVc, TryJoinIterExt, ValueToString,
Vc, trace::TraceRawVcs, turbobail,
};
use crate::{
......@@ -848,7 +849,7 @@ pub async fn compute_module_batches(
.into_iter()
.map(async |(key, items)| {
if items.len() == 1 {
Ok(Either::Left(std::iter::empty()))
anyhow::Ok(Either::Left(std::iter::empty()))
} else {
let batch_group = ModuleBatchGroup::new(items.clone(), (*key).clone())
.to_resolved()
......@@ -858,11 +859,11 @@ pub async fn compute_module_batches(
))
}
})
.try_join()
.await?
.join()
.await
.into_iter()
.flatten()
.collect::<FxHashMap<_, _>>();
.flatten_ok()
.collect::<Result<FxHashMap<_, _>>>()?;
// Insert batches into the graph and store the NodeIndices
let mut batches_count = 0;
......
use anyhow::Result;
use rustc_hash::{FxHashMap, FxHashSet};
use turbo_tasks::{ResolvedVc, TryJoinIterExt, Vc};
use turbo_tasks::{JoinIterExt, ResolvedVc, Vc};
use crate::{
module::{Module, ModuleSideEffects},
......@@ -62,10 +62,10 @@ async fn compute_side_effect_free_module_info_single(
),
})
})
.try_join()
.await?
.join()
.await
.into_iter()
.collect::<FxHashMap<_, _>>();
.collect::<Result<FxHashMap<_, _>>>()?;
// Modules are categorized as side-effectful, locally side effect free and side effect free.
// So we are really just interested in determining what modules that are locally side effect
......
......@@ -2,6 +2,7 @@ use std::collections::HashSet;
use anyhow::Result;
use bincode::{Decode, Encode};
use smallvec::SmallVec;
use turbo_rcstr::RcStr;
use turbo_tasks::{
NonLocalValue, ResolvedVc, TryFlatJoinIterExt, TryJoinIterExt, ValueToString, Vc,
......@@ -135,9 +136,8 @@ pub async fn referenced_modules_and_affecting_sources(
let trait_ref = reference.into_trait_ref().await?;
let resolve_result = reference.resolve_reference().await?;
if let Some(chunking_type) = &trait_ref.chunking_type() {
let mut modules = resolve_result
.primary_modules_raw_iter()
.collect::<Vec<_>>();
let mut modules: SmallVec<[_; 2]> =
resolve_result.primary_modules_raw_iter().collect();
modules.extend(
resolve_result
.affecting_sources_iter()
......@@ -225,7 +225,7 @@ pub async fn primary_referenced_modules(module: Vc<Box<dyn Module>>) -> Result<V
pub struct ResolvedReference {
pub chunking_type: ChunkingType,
pub binding_usage: BindingUsage,
pub modules: Vec<ResolvedVc<Box<dyn Module>>>,
pub modules: SmallVec<[ResolvedVc<Box<dyn Module>>; 2]>,
}
#[turbo_tasks::value(transparent)]
......
......@@ -17,8 +17,8 @@ use tracing::{Instrument, Level};
use turbo_frozenmap::{FrozenMap, FrozenSet};
use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{
FxIndexMap, NonLocalValue, ReadRef, ResolvedVc, TryFlatJoinIterExt, TryJoinIterExt,
ValueToString, ValueToStringRef, Vc, trace::TraceRawVcs,
FxIndexMap, JoinIterExt, NonLocalValue, ReadRef, ResolvedVc, TryFlatJoinIterExt,
TryJoinIterExt, ValueToString, ValueToStringRef, Vc, trace::TraceRawVcs,
};
use turbo_tasks_fs::{FileSystemEntryType, FileSystemPath, RealPathErrorType};
use turbo_unix_path::normalize_request;
......@@ -346,12 +346,15 @@ impl ModuleResolveResult {
/// Returns primary modules (no duplicates). Emits errors for Unknown items.
/// Duplicates are already marked at construction time so no extra dedup is
/// needed here.
pub async fn primary_modules(&self) -> Result<Vec<ResolvedVc<Box<dyn Module>>>> {
pub async fn primary_modules(&self) -> Result<SmallVec<[ResolvedVc<Box<dyn Module>>; 2]>> {
self.primary
.iter()
.map(async |(_, item)| item.as_module().await)
.try_flat_join()
.join()
.await
.into_iter()
.filter_map(Result::transpose)
.collect()
}
/// Returns the first module in the result, or None.
......@@ -3991,7 +3994,7 @@ mod tests {
// primary_modules() yields each module exactly once, in first-seen order.
let modules = result.primary_modules().await?;
assert_eq!(modules, vec![m_a, m_b]);
assert_eq!(modules.as_slice(), [m_a, m_b]);
Ok(Vc::cell(snapshot_primary(&result).await?))
}
......@@ -4025,7 +4028,7 @@ mod tests {
.await?;
assert_eq!(result.first_module().await?, Some(m));
assert_eq!(result.primary_modules().await?, vec![m]);
assert_eq!(result.primary_modules().await?.as_slice(), [m]);
Ok(Vc::cell(snapshot_primary(&result).await?))
}
tt.run_once(async move {
......@@ -4067,7 +4070,7 @@ mod tests {
ModuleResolveResultItem::Module(m),
);
let result: ModuleResolveResult = builder.into();
assert_eq!(result.primary_modules().await?, vec![m]);
assert_eq!(result.primary_modules().await?.as_slice(), [m]);
Ok(Vc::cell(snapshot_primary(&result).await?))
}
tt.run_once(async move {
......@@ -4104,7 +4107,7 @@ mod tests {
let r2 = *ModuleResolveResult::module(m_b);
let merged = ModuleResolveResult::alternatives(vec![r1, r2]).await?;
assert_eq!(merged.primary_modules().await?, vec![m_a, m_b]);
assert_eq!(merged.primary_modules().await?.as_slice(), [m_a, m_b]);
// Verify every Duplicate(i) is well-formed
for (i, (_, item)) in merged.primary.iter().enumerate() {
......
......@@ -262,9 +262,7 @@ impl EcmascriptChunkPlaceable for EcmascriptCollectModuleWithChunkGroup {
))
})
.try_join()
.await?
.into_iter()
.collect::<Vec<_>>();
.await?;
let mut code = RopeBuilder::default();
......
......@@ -68,8 +68,8 @@ use tokio::sync::OnceCell;
use tracing::Instrument;
use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{
FxIndexMap, FxIndexSet, NonLocalValue, PrettyPrintError, ReadRef, ResolvedVc, TaskInput,
TryJoinIterExt, Upcast, ValueToString, Vc, trace::TraceRawVcs, turbofmt,
FxIndexMap, FxIndexSet, JoinIterExt, NonLocalValue, PrettyPrintError, ReadRef, ResolvedVc,
TaskInput, TryJoinIterExt, Upcast, ValueToString, Vc, trace::TraceRawVcs, turbofmt,
};
use turbo_tasks_fs::FileSystemPath;
use turbopack_core::{
......@@ -3117,11 +3117,11 @@ where
)
.to_resolved()
})
.try_join()
.await?;
.join()
.await;
for resolved_dir_ref in resolved_dirs {
analysis.add_reference(resolved_dir_ref);
analysis.add_reference(resolved_dir_ref?);
}
return Ok(());
......
......@@ -3,7 +3,7 @@ use rustc_hash::{FxHashMap, FxHashSet};
use smallvec::SmallVec;
use tracing::Instrument;
use turbo_rcstr::RcStr;
use turbo_tasks::{ReadRef, ResolvedVc, TryJoinIterExt, ValueToString, Vc};
use turbo_tasks::{JoinIterExt, ReadRef, ResolvedVc, ValueToString, Vc};
use turbo_tasks_hash::hash_xxh3_hash64;
use turbopack_core::{
chunk::{
......@@ -55,10 +55,10 @@ pub async fn get_global_module_id_strategy(
let hash = hash_xxh3_hash64(&ident_str);
Ok((ident, (ident_str, hash)))
})
.try_join()
.await?
.join()
.await
.into_iter()
.collect::<FxHashMap<_, _>>();
.collect::<Result<FxHashMap<_, _>>>()?;
finalize_module_ids(&mut module_id_map);
......
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