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

perf(turbopack): Migrate as many data structures to FxHash as possible #75546

Open
wants to merge 1 commit into
base: bgw/fxdashmap
Choose a base branch
from
Open
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
15 changes: 15 additions & 0 deletions Cargo.lock

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

8 changes: 4 additions & 4 deletions crates/napi/src/next_api/utils.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use std::{
collections::HashMap, future::Future, ops::Deref, path::PathBuf, sync::Arc, time::Duration,
};
use std::{future::Future, ops::Deref, path::PathBuf, sync::Arc, time::Duration};

use anyhow::{anyhow, Context, Result};
use napi::{
bindgen_prelude::{External, ToNapiValue},
threadsafe_function::{ThreadSafeCallContext, ThreadsafeFunction, ThreadsafeFunctionCallMode},
JsFunction, JsObject, JsUnknown, NapiRaw, NapiValue, Status,
};
use rustc_hash::FxHashMap;
use serde::Serialize;
use turbo_tasks::{
task_statistics::TaskStatisticsApi, trace::TraceRawVcs, OperationVc, ReadRef, TaskId,
Expand Down Expand Up @@ -412,7 +411,8 @@ impl From<SourcePos> for NapiSourcePos {
pub struct NapiDiagnostic {
pub category: String,
pub name: String,
pub payload: HashMap<String, String>,
#[napi(ts_type = "Record<string, string>")]
pub payload: FxHashMap<String, String>,
}

impl NapiDiagnostic {
Expand Down
5 changes: 3 additions & 2 deletions crates/next-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ indexmap = { workspace = true }
next-core = { workspace = true }
petgraph = { workspace = true, features = ["serde-1"]}
regex = { workspace = true }
rustc-hash = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
shadow-rs = { workspace = true }
swc_core = { workspace = true }
tracing = { workspace = true }
turbo-rcstr = { workspace = true }
turbo-tasks = { workspace = true }
Expand All @@ -34,11 +36,10 @@ turbopack = { workspace = true }
turbopack-browser = { workspace = true }
turbopack-cli-utils = { workspace = true }
turbopack-core = { workspace = true }
turbopack-env = { workspace = true }
turbopack-ecmascript = { workspace = true }
turbopack-env = { workspace = true }
turbopack-node = { workspace = true }
turbopack-nodejs = { workspace = true }
swc_core = { workspace = true }

[build-dependencies]
# It is not a mistake this dependency is specified in dep / build-dep both.
Expand Down
5 changes: 2 additions & 3 deletions crates/next-api/src/client_references.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use std::collections::HashMap;

use anyhow::Result;
use next_core::{
self,
next_client_reference::{CssClientReferenceModule, EcmascriptClientReferenceModule},
next_server_component::server_component_module::NextServerComponentModule,
};
use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};
use turbo_tasks::{
debug::ValueDebugFormat, trace::TraceRawVcs, NonLocalValue, ResolvedVc, TryFlatJoinIterExt, Vc,
Expand All @@ -26,7 +25,7 @@ pub enum ClientReferenceMapType {
}

#[turbo_tasks::value(transparent)]
pub struct ClientReferencesSet(HashMap<ResolvedVc<Box<dyn Module>>, ClientReferenceMapType>);
pub struct ClientReferencesSet(FxHashMap<ResolvedVc<Box<dyn Module>>, ClientReferenceMapType>);

#[turbo_tasks::function]
pub async fn map_client_references(
Expand Down
5 changes: 2 additions & 3 deletions crates/next-api/src/loadable_manifest.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::collections::HashMap;

use anyhow::Result;
use next_core::{next_manifests::LoadableManifest, util::NextRuntime};
use rustc_hash::FxHashMap;
use turbo_rcstr::RcStr;
use turbo_tasks::{ResolvedVc, TryFlatJoinIterExt, ValueToString, Vc};
use turbo_tasks_fs::{File, FileContent, FileSystemPath};
Expand All @@ -23,7 +22,7 @@ pub async fn create_react_loadable_manifest(
) -> Result<Vc<OutputAssets>> {
let dynamic_import_entries = &*dynamic_import_entries.await?;

let mut loadable_manifest: HashMap<RcStr, LoadableManifest> = Default::default();
let mut loadable_manifest: FxHashMap<RcStr, LoadableManifest> = FxHashMap::default();

for (_, (module_id, chunk_output)) in dynamic_import_entries.into_iter() {
let chunk_output = chunk_output.await?;
Expand Down
13 changes: 7 additions & 6 deletions crates/next-api/src/module_graph.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{borrow::Cow, collections::HashMap};
use std::borrow::Cow;

use anyhow::Result;
use next_core::{
Expand All @@ -9,6 +9,7 @@ use next_core::{
next_dynamic::NextDynamicEntryModule,
next_manifests::ActionLayer,
};
use rustc_hash::FxHashMap;
use tracing::Instrument;
use turbo_tasks::{
CollectiblesSource, FxIndexMap, FxIndexSet, ResolvedVc, TryFlatJoinIterExt, TryJoinIterExt, Vc,
Expand Down Expand Up @@ -58,8 +59,8 @@ impl NextDynamicGraph {
// This would clone the graph and allow changing the node weights. We can probably get away
// with keeping the sidecar information separate from the graph itself, though.
//
// let mut reduced_modules: HashMap<Vc<Box<dyn Module>>, NodeIndex<u32>> =
// HashMap::new(); let mut reduced_graph = DiGraph::new();
// let mut reduced_modules: FxHashMap<Vc<Box<dyn Module>>, NodeIndex<u32>> =
// FxHashMap::default(); let mut reduced_graph = DiGraph::new();
// for idx in graph.node_indices() {
// let weight = *graph.node_weight(idx).unwrap();
// let new_idx = reduced_graph.add_node(weight);
Expand Down Expand Up @@ -112,7 +113,7 @@ impl NextDynamicGraph {
let mut result = vec![];

// module -> the client reference entry (if any)
let mut state_map = HashMap::new();
let mut state_map = FxHashMap::default();
graph.traverse_edges_from_entries(entries, |parent_info, node| {
let module = node.module;
let Some((parent_node, _)) = parent_info else {
Expand Down Expand Up @@ -210,7 +211,7 @@ impl ServerActionsGraph {
return Ok(Vc::cell(Default::default()));
}

let mut result = HashMap::new();
let mut result = FxHashMap::default();
graph.traverse_from_entry(entry, |node| {
if let Some(node_data) = data.get(&node.module) {
result.insert(node.module, *node_data);
Expand Down Expand Up @@ -309,7 +310,7 @@ impl ClientReferencesGraph {
graph.traverse_edges_from_entries_topological(
entries,
// state_map is `module -> Option< the current so parent server component >`
&mut HashMap::new(),
&mut FxHashMap::default(),
|parent_info, node, state_map| {
let module = node.module;
let module_type = data.get(&module);
Expand Down
7 changes: 3 additions & 4 deletions crates/next-api/src/operation.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use anyhow::Result;
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};
use turbo_rcstr::RcStr;
use turbo_tasks::{
debug::ValueDebugFormat, get_effects, trace::TraceRawVcs, CollectiblesSource, NonLocalValue,
OperationVc, ResolvedVc, Vc,
debug::ValueDebugFormat, get_effects, trace::TraceRawVcs, CollectiblesSource, FxIndexMap,
NonLocalValue, OperationVc, ResolvedVc, Vc,
};
use turbopack_core::{diagnostics::Diagnostic, issue::IssueDescriptionExt};

Expand All @@ -23,7 +22,7 @@ use crate::{
/// This is needed to call `write_to_disk` which expects an `OperationVc<Endpoint>`.
#[turbo_tasks::value(shared)]
pub struct EntrypointsOperation {
pub routes: IndexMap<RcStr, RouteOperation>,
pub routes: FxIndexMap<RcStr, RouteOperation>,
pub middleware: Option<MiddlewareOperation>,
pub instrumentation: Option<InstrumentationOperation>,
pub pages_document_endpoint: OperationVc<Box<dyn Endpoint>>,
Expand Down
8 changes: 3 additions & 5 deletions crates/next-api/src/server_actions.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use std::{
collections::{BTreeMap, HashMap},
io::Write,
};
use std::{collections::BTreeMap, io::Write};

use anyhow::{bail, Context, Result};
use next_core::{
Expand All @@ -10,6 +7,7 @@ use next_core::{
},
util::NextRuntime,
};
use rustc_hash::FxHashMap;
use swc_core::{
atoms::Atom,
common::comments::Comments,
Expand Down Expand Up @@ -399,7 +397,7 @@ struct OptionActionMap(Option<ResolvedVc<ActionMap>>);
type LayerAndActions = (ActionLayer, ResolvedVc<ActionMap>);
/// A mapping of every module module containing Server Actions, mapping to its layer and actions.
#[turbo_tasks::value(transparent)]
pub struct AllModuleActions(HashMap<ResolvedVc<Box<dyn Module>>, LayerAndActions>);
pub struct AllModuleActions(FxHashMap<ResolvedVc<Box<dyn Module>>, LayerAndActions>);

#[turbo_tasks::function]
pub async fn map_server_actions(graph: Vc<SingleModuleGraph>) -> Result<Vc<AllModuleActions>> {
Expand Down
15 changes: 7 additions & 8 deletions crates/next-api/src/versioned_content_map.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::collections::{HashMap, HashSet};

use anyhow::{bail, Result};
use next_core::emit_assets;
use rustc_hash::{FxHashMap, FxHashSet};
use serde::{Deserialize, Serialize};
use turbo_rcstr::RcStr;
use turbo_tasks::{
Expand Down Expand Up @@ -31,7 +30,7 @@ use turbopack_core::{
struct MapEntry {
assets_operation: OperationVc<OutputAssets>,
/// Precomputed map for quick access to output asset by filepath
path_to_asset: HashMap<ResolvedVc<FileSystemPath>, ResolvedVc<Box<dyn OutputAsset>>>,
path_to_asset: FxHashMap<ResolvedVc<FileSystemPath>, ResolvedVc<Box<dyn OutputAsset>>>,
}

// HACK: This is technically incorrect because `path_to_asset` contains `ResolvedVc`...
Expand All @@ -49,15 +48,15 @@ pub struct PathToOutputOperation(
/// It may not be 100% correct for the key (`FileSystemPath`) to be in a `ResolvedVc` here, but
/// it's impractical to make it an `OperationVc`/`OperationValue`, and it's unlikely to
/// change/break?
HashMap<ResolvedVc<FileSystemPath>, FxIndexSet<OperationVc<OutputAssets>>>,
FxHashMap<ResolvedVc<FileSystemPath>, FxIndexSet<OperationVc<OutputAssets>>>,
);

// HACK: This is technically incorrect because the map's key is a `ResolvedVc`...
unsafe impl OperationValue for PathToOutputOperation {}

// A precomputed map for quick access to output asset by filepath
type OutputOperationToComputeEntry =
HashMap<OperationVc<OutputAssets>, OperationVc<OptionMapEntry>>;
FxHashMap<OperationVc<OutputAssets>, OperationVc<OptionMapEntry>>;

#[turbo_tasks::value]
pub struct VersionedContentMap {
Expand All @@ -77,8 +76,8 @@ impl VersionedContentMap {
// should be a singleton for each project.
pub fn new() -> ResolvedVc<Self> {
VersionedContentMap {
map_path_to_op: State::new(PathToOutputOperation(HashMap::new())),
map_op_to_compute_entry: State::new(HashMap::new()),
map_path_to_op: State::new(PathToOutputOperation(FxHashMap::default())),
map_op_to_compute_entry: State::new(FxHashMap::default()),
}
.resolved_cell()
}
Expand Down Expand Up @@ -142,7 +141,7 @@ impl VersionedContentMap {
let mut changed = false;

// get current map's keys, subtract keys that don't exist in operation
let mut stale_assets = map.0.keys().copied().collect::<HashSet<_>>();
let mut stale_assets = map.0.keys().copied().collect::<FxHashSet<_>>();

for (k, _) in entries.iter() {
let res = map.0.entry(*k).or_default().insert(assets_operation);
Expand Down
Loading
Loading