Unverified Commit c450169e authored by Benjamin Woodruff's avatar Benjamin Woodruff Committed by GitHub

Turbopack: Switch RequestKey's `conditions` field from BTreeMap to FrozenMap (#87099)

Doing this as a small trial of the new `FrozenMap` API. @lukesandberg pointed this out as a potentially good candidate.
parent 5437319d
...@@ -9674,6 +9674,7 @@ dependencies = [ ...@@ -9674,6 +9674,7 @@ dependencies = [
"tokio", "tokio",
"tracing", "tracing",
"turbo-bincode", "turbo-bincode",
"turbo-frozenmap",
"turbo-prehash", "turbo-prehash",
"turbo-rcstr", "turbo-rcstr",
"turbo-tasks", "turbo-tasks",
......
...@@ -381,6 +381,17 @@ impl<K, V> FrozenMap<K, V> { ...@@ -381,6 +381,17 @@ impl<K, V> FrozenMap<K, V> {
inner: slice.iter(), inner: slice.iter(),
} }
} }
/// Extend this [`FrozenMap`] by constructing a new map with the additional entries. New entries
/// with overlapping keys will overwrite existing ones.
#[must_use]
pub fn extend(&self, entries: impl IntoIterator<Item = (K, V)>) -> Self
where
K: Clone + Ord,
V: Clone,
{
self.as_slice().iter().cloned().chain(entries).collect()
}
} }
// Manual implementation because the derive would add unnecessary `K: Default, V: Default` bounds. // Manual implementation because the derive would add unnecessary `K: Default, V: Default` bounds.
......
...@@ -39,6 +39,7 @@ swc_sourcemap = { workspace = true } ...@@ -39,6 +39,7 @@ swc_sourcemap = { workspace = true }
swc_core = { workspace = true, features = ["ecma_preset_env", "common"] } swc_core = { workspace = true, features = ["ecma_preset_env", "common"] }
tracing = { workspace = true } tracing = { workspace = true }
turbo-bincode = { workspace = true } turbo-bincode = { workspace = true }
turbo-frozenmap = { workspace = true }
turbo-prehash = { workspace = true } turbo-prehash = { workspace = true }
turbo-rcstr = { workspace = true } turbo-rcstr = { workspace = true }
turbo-tasks = { workspace = true } turbo-tasks = { workspace = true }
...@@ -51,7 +52,6 @@ url = { workspace = true } ...@@ -51,7 +52,6 @@ url = { workspace = true }
urlencoding = { workspace = true } urlencoding = { workspace = true }
uuid = { workspace = true} uuid = { workspace = true}
[dev-dependencies] [dev-dependencies]
rstest = { workspace = true } rstest = { workspace = true }
tokio = { workspace = true } tokio = { workspace = true }
......
...@@ -15,6 +15,7 @@ use rustc_hash::{FxHashMap, FxHashSet}; ...@@ -15,6 +15,7 @@ use rustc_hash::{FxHashMap, FxHashSet};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use smallvec::SmallVec; use smallvec::SmallVec;
use tracing::{Instrument, Level}; use tracing::{Instrument, Level};
use turbo_frozenmap::FrozenMap;
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ use turbo_tasks::{
FxIndexMap, FxIndexSet, NonLocalValue, ReadRef, ResolvedVc, SliceMap, TaskInput, FxIndexMap, FxIndexSet, NonLocalValue, ReadRef, ResolvedVc, SliceMap, TaskInput,
...@@ -477,7 +478,7 @@ pub enum ResolveResultItem { ...@@ -477,7 +478,7 @@ pub enum ResolveResultItem {
#[turbo_tasks::value] #[turbo_tasks::value]
pub struct RequestKey { pub struct RequestKey {
pub request: Option<RcStr>, pub request: Option<RcStr>,
pub conditions: BTreeMap<String, bool>, pub conditions: FrozenMap<RcStr, bool>,
} }
impl Display for RequestKey { impl Display for RequestKey {
...@@ -756,19 +757,26 @@ impl ResolveResult { ...@@ -756,19 +757,26 @@ impl ResolveResult {
} }
} }
pub fn add_conditions(&mut self, conditions: impl IntoIterator<Item = (RcStr, bool)>) { pub fn with_conditions(&self, new_conditions: &[(RcStr, bool)]) -> Self {
let mut primary = std::mem::take(&mut self.primary); let primary = self
for (k, v) in conditions { .primary
for (key, _) in primary.iter_mut() { .iter()
key.conditions.insert(k.to_string(), v); .map(|(k, v)| {
} (
} RequestKey {
// Deduplicate request: k.request.clone(),
self.primary = IntoIterator::into_iter(primary) conditions: k.conditions.extend(new_conditions.iter().cloned()),
.collect::<FxIndexMap<_, _>>() },
v.clone(),
)
})
.collect::<FxIndexMap<_, _>>() // Deduplicate
.into_iter() .into_iter()
.collect::<Vec<_>>() .collect();
.into_boxed_slice(); ResolveResult {
primary,
affecting_sources: self.affecting_sources.clone(),
}
} }
} }
...@@ -3119,8 +3127,7 @@ async fn handle_exports_imports_field( ...@@ -3119,8 +3127,7 @@ async fn handle_exports_imports_field(
}; };
let resolve_result = if !conditions.is_empty() { let resolve_result = if !conditions.is_empty() {
let mut resolve_result = resolve_result.owned().await?; let resolve_result = resolve_result.await?.with_conditions(&conditions);
resolve_result.add_conditions(conditions);
resolve_result.cell() resolve_result.cell()
} else { } else {
resolve_result resolve_result
......
...@@ -15,7 +15,7 @@ use turbopack_core::{ ...@@ -15,7 +15,7 @@ use turbopack_core::{
}, },
reference_type::{ReferenceType, TypeScriptReferenceSubType}, reference_type::{ReferenceType, TypeScriptReferenceSubType},
resolve::{ resolve::{
AliasPattern, ModuleResolveResult, handle_resolve_error, AliasPattern, ModuleResolveResult, RequestKey, handle_resolve_error,
node::node_cjs_resolve_options, node::node_cjs_resolve_options,
options::{ options::{
ConditionValue, ImportMap, ImportMapping, ResolveIntoPackage, ResolveModules, ConditionValue, ImportMap, ImportMapping, ResolveIntoPackage, ResolveModules,
...@@ -479,9 +479,14 @@ pub async fn type_resolve( ...@@ -479,9 +479,14 @@ pub async fn type_resolve(
pub async fn as_typings_result(result: Vc<ModuleResolveResult>) -> Result<Vc<ModuleResolveResult>> { pub async fn as_typings_result(result: Vc<ModuleResolveResult>) -> Result<Vc<ModuleResolveResult>> {
let mut result = result.owned().await?; let mut result = result.owned().await?;
result.primary = IntoIterator::into_iter(take(&mut result.primary)) result.primary = IntoIterator::into_iter(take(&mut result.primary))
.map(|(mut k, v)| { .map(|(k, v)| {
k.conditions.insert("types".to_string(), true); (
(k, v) RequestKey {
request: k.request.clone(),
conditions: k.conditions.extend([(rcstr!("types"), true)]),
},
v,
)
}) })
.collect(); .collect();
Ok(result.cell()) Ok(result.cell())
......
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