Skip to content

Commit c450169

Browse files
authored
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.
1 parent 5437319 commit c450169

File tree

5 files changed

+44
-20
lines changed

5 files changed

+44
-20
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

turbopack/crates/turbo-frozenmap/src/map.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,17 @@ impl<K, V> FrozenMap<K, V> {
381381
inner: slice.iter(),
382382
}
383383
}
384+
385+
/// Extend this [`FrozenMap`] by constructing a new map with the additional entries. New entries
386+
/// with overlapping keys will overwrite existing ones.
387+
#[must_use]
388+
pub fn extend(&self, entries: impl IntoIterator<Item = (K, V)>) -> Self
389+
where
390+
K: Clone + Ord,
391+
V: Clone,
392+
{
393+
self.as_slice().iter().cloned().chain(entries).collect()
394+
}
384395
}
385396

386397
// Manual implementation because the derive would add unnecessary `K: Default, V: Default` bounds.

turbopack/crates/turbopack-core/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ swc_sourcemap = { workspace = true }
3939
swc_core = { workspace = true, features = ["ecma_preset_env", "common"] }
4040
tracing = { workspace = true }
4141
turbo-bincode = { workspace = true }
42+
turbo-frozenmap = { workspace = true }
4243
turbo-prehash = { workspace = true }
4344
turbo-rcstr = { workspace = true }
4445
turbo-tasks = { workspace = true }
@@ -51,7 +52,6 @@ url = { workspace = true }
5152
urlencoding = { workspace = true }
5253
uuid = { workspace = true}
5354

54-
5555
[dev-dependencies]
5656
rstest = { workspace = true }
5757
tokio = { workspace = true }

turbopack/crates/turbopack-core/src/resolve/mod.rs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use rustc_hash::{FxHashMap, FxHashSet};
1515
use serde::{Deserialize, Serialize};
1616
use smallvec::SmallVec;
1717
use tracing::{Instrument, Level};
18+
use turbo_frozenmap::FrozenMap;
1819
use turbo_rcstr::{RcStr, rcstr};
1920
use turbo_tasks::{
2021
FxIndexMap, FxIndexSet, NonLocalValue, ReadRef, ResolvedVc, SliceMap, TaskInput,
@@ -477,7 +478,7 @@ pub enum ResolveResultItem {
477478
#[turbo_tasks::value]
478479
pub struct RequestKey {
479480
pub request: Option<RcStr>,
480-
pub conditions: BTreeMap<String, bool>,
481+
pub conditions: FrozenMap<RcStr, bool>,
481482
}
482483

483484
impl Display for RequestKey {
@@ -756,19 +757,26 @@ impl ResolveResult {
756757
}
757758
}
758759

759-
pub fn add_conditions(&mut self, conditions: impl IntoIterator<Item = (RcStr, bool)>) {
760-
let mut primary = std::mem::take(&mut self.primary);
761-
for (k, v) in conditions {
762-
for (key, _) in primary.iter_mut() {
763-
key.conditions.insert(k.to_string(), v);
764-
}
765-
}
766-
// Deduplicate
767-
self.primary = IntoIterator::into_iter(primary)
768-
.collect::<FxIndexMap<_, _>>()
760+
pub fn with_conditions(&self, new_conditions: &[(RcStr, bool)]) -> Self {
761+
let primary = self
762+
.primary
763+
.iter()
764+
.map(|(k, v)| {
765+
(
766+
RequestKey {
767+
request: k.request.clone(),
768+
conditions: k.conditions.extend(new_conditions.iter().cloned()),
769+
},
770+
v.clone(),
771+
)
772+
})
773+
.collect::<FxIndexMap<_, _>>() // Deduplicate
769774
.into_iter()
770-
.collect::<Vec<_>>()
771-
.into_boxed_slice();
775+
.collect();
776+
ResolveResult {
777+
primary,
778+
affecting_sources: self.affecting_sources.clone(),
779+
}
772780
}
773781
}
774782

@@ -3119,8 +3127,7 @@ async fn handle_exports_imports_field(
31193127
};
31203128

31213129
let resolve_result = if !conditions.is_empty() {
3122-
let mut resolve_result = resolve_result.owned().await?;
3123-
resolve_result.add_conditions(conditions);
3130+
let resolve_result = resolve_result.await?.with_conditions(&conditions);
31243131
resolve_result.cell()
31253132
} else {
31263133
resolve_result

turbopack/crates/turbopack-resolve/src/typescript.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use turbopack_core::{
1515
},
1616
reference_type::{ReferenceType, TypeScriptReferenceSubType},
1717
resolve::{
18-
AliasPattern, ModuleResolveResult, handle_resolve_error,
18+
AliasPattern, ModuleResolveResult, RequestKey, handle_resolve_error,
1919
node::node_cjs_resolve_options,
2020
options::{
2121
ConditionValue, ImportMap, ImportMapping, ResolveIntoPackage, ResolveModules,
@@ -479,9 +479,14 @@ pub async fn type_resolve(
479479
pub async fn as_typings_result(result: Vc<ModuleResolveResult>) -> Result<Vc<ModuleResolveResult>> {
480480
let mut result = result.owned().await?;
481481
result.primary = IntoIterator::into_iter(take(&mut result.primary))
482-
.map(|(mut k, v)| {
483-
k.conditions.insert("types".to_string(), true);
484-
(k, v)
482+
.map(|(k, v)| {
483+
(
484+
RequestKey {
485+
request: k.request.clone(),
486+
conditions: k.conditions.extend([(rcstr!("types"), true)]),
487+
},
488+
v,
489+
)
485490
})
486491
.collect();
487492
Ok(result.cell())

0 commit comments

Comments
 (0)