Skip to content

Commit f7b4a8f

Browse files
authored
chore: Update patricia_tree dependency, remove manual serde impls (#85785)
Recent versions of `patricia_tree` have their own serde implementation which is more efficient than what we can do because it serializes the internal nodes in the tree, rather than the public observable key/value entry API.
1 parent ed41ac1 commit f7b4a8f

File tree

3 files changed

+9
-70
lines changed

3 files changed

+9
-70
lines changed

Cargo.lock

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

turbopack/crates/turbopack-core/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ data-encoding = { workspace = true }
2323
either = { workspace = true }
2424
indexmap = { workspace = true }
2525
once_cell = { workspace = true }
26-
patricia_tree = "0.5.5"
26+
patricia_tree = { version = "0.10.1", features = ["serde"] }
2727
petgraph = { workspace = true, features = ["serde-1"] }
2828
roaring = { workspace = true, features = ["serde"] }
2929
ref-cast = "1.0.20"

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

Lines changed: 5 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,7 @@ use std::{
66

77
use anyhow::Result;
88
use patricia_tree::PatriciaMap;
9-
use serde::{
10-
Deserialize, Deserializer, Serialize, Serializer,
11-
de::{MapAccess, Visitor},
12-
ser::SerializeMap,
13-
};
14-
use serde_bytes::{ByteBuf, Bytes};
9+
use serde::{Deserialize, Serialize};
1510
use turbo_rcstr::RcStr;
1611
use turbo_tasks::{
1712
NonLocalValue,
@@ -29,7 +24,8 @@ use super::pattern::Pattern;
2924
///
3025
/// If the pattern does not have a wildcard character, it will only match the
3126
/// exact string, and return the template as-is.
32-
#[derive(Clone)]
27+
#[derive(Clone, Serialize, Deserialize)]
28+
#[serde(transparent)]
3329
pub struct AliasMap<T> {
3430
map: PatriciaMap<BTreeMap<AliasKey, T>>,
3531
}
@@ -55,63 +51,6 @@ where
5551

5652
impl<T> Eq for AliasMap<T> where T: Eq {}
5753

58-
impl<T> Serialize for AliasMap<T>
59-
where
60-
T: Serialize,
61-
{
62-
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
63-
where
64-
S: Serializer,
65-
{
66-
let mut map = serializer.serialize_map(Some(self.map.len()))?;
67-
for (prefix, value) in self.map.iter() {
68-
let key = ByteBuf::from(prefix);
69-
map.serialize_entry(&key, value)?;
70-
}
71-
map.end()
72-
}
73-
}
74-
75-
struct AliasMapVisitor<T> {
76-
marker: std::marker::PhantomData<T>,
77-
}
78-
79-
impl<'de, T> Visitor<'de> for AliasMapVisitor<T>
80-
where
81-
T: Deserialize<'de>,
82-
{
83-
type Value = AliasMap<T>;
84-
85-
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
86-
formatter.write_str("a map of alias patterns to templates")
87-
}
88-
89-
fn visit_map<M>(self, mut access: M) -> Result<Self::Value, M::Error>
90-
where
91-
M: MapAccess<'de>,
92-
{
93-
let mut map = AliasMap::new();
94-
while let Some((key, value)) = access.next_entry::<&Bytes, _>()? {
95-
map.map.insert(key, value);
96-
}
97-
Ok(map)
98-
}
99-
}
100-
101-
impl<'a, T> Deserialize<'a> for AliasMap<T>
102-
where
103-
T: Deserialize<'a>,
104-
{
105-
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
106-
where
107-
D: Deserializer<'a>,
108-
{
109-
deserializer.deserialize_map(AliasMapVisitor {
110-
marker: std::marker::PhantomData,
111-
})
112-
}
113-
}
114-
11554
impl<T> TraceRawVcs for AliasMap<T>
11655
where
11756
T: TraceRawVcs,
@@ -392,7 +331,7 @@ impl<'a, T> IntoIterator for &'a AliasMap<T> {
392331
///
393332
/// [PATTERN_KEY_COMPARE]: https://nodejs.org/api/esm.html#resolver-algorithm-specification
394333
pub struct AliasMapIntoIter<T> {
395-
iter: patricia_tree::map::IntoIter<BTreeMap<AliasKey, T>>,
334+
iter: patricia_tree::map::IntoIter<Vec<u8>, BTreeMap<AliasKey, T>>,
396335
current_prefix_iterator: Option<AliasMapIntoIterItem<T>>,
397336
}
398337

@@ -457,7 +396,7 @@ impl<T> Iterator for AliasMapIntoIter<T> {
457396
///
458397
/// [PATTERN_KEY_COMPARE]: https://nodejs.org/api/esm.html#resolver-algorithm-specification
459398
pub struct AliasMapIter<'a, T> {
460-
iter: patricia_tree::map::Iter<'a, BTreeMap<AliasKey, T>>,
399+
iter: patricia_tree::map::Iter<'a, Vec<u8>, BTreeMap<AliasKey, T>>,
461400
current_prefix_iterator: Option<AliasMapIterItem<'a, T>>,
462401
}
463402

0 commit comments

Comments
 (0)