Skip to content

Commit 62de494

Browse files
kdy1mischnic
andauthored
perf(turbopack): Implement ShrinkToFit for AutoMap (#79218)
### What? Implement `ShrinkToFit` for `AutoMap`, which can be used in a `Vc` ### Why? For consistency and to reduce max RSS --------- Co-authored-by: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com>
1 parent 0c0262e commit 62de494

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

Cargo.lock

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

turbopack/crates/turbo-tasks-auto-hash-map/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ hashbrown = { workspace = true, features = ["serde"]}
1313
rustc-hash = { workspace = true }
1414
serde = { workspace = true, features = ["derive"] }
1515
smallvec = { workspace = true }
16+
shrink-to-fit = { workspace = true, features = ["hashbrown"] }

turbopack/crates/turbo-tasks-auto-hash-map/src/map.rs

+20
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use serde::{
1212
de::{MapAccess, Visitor},
1313
ser::SerializeMap,
1414
};
15+
use shrink_to_fit::ShrinkToFit;
1516
use smallvec::SmallVec;
1617

1718
use crate::{MAX_LIST_SIZE, MIN_HASH_SIZE};
@@ -900,6 +901,25 @@ where
900901
}
901902
}
902903

904+
impl<K, V, H, const I: usize> ShrinkToFit for AutoMap<K, V, H, I>
905+
where
906+
K: Eq + Hash,
907+
V: Eq,
908+
H: BuildHasher + Default,
909+
{
910+
fn shrink_to_fit(&mut self) {
911+
if self.len() < MIN_HASH_SIZE {
912+
self.convert_to_list();
913+
}
914+
915+
match self {
916+
AutoMap::List(list) => list.shrink_to_fit(),
917+
AutoMap::Map(map) => {
918+
hashbrown::HashMap::shrink_to_fit(map);
919+
}
920+
}
921+
}
922+
}
903923
#[cfg(test)]
904924
mod tests {
905925
use super::*;

turbopack/crates/turbo-tasks-auto-hash-map/src/set.rs

+11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::{
66

77
use rustc_hash::FxHasher;
88
use serde::{Deserialize, Serialize};
9+
use shrink_to_fit::ShrinkToFit;
910

1011
use crate::AutoMap;
1112

@@ -240,6 +241,16 @@ where
240241
}
241242
}
242243

244+
impl<K, H, const I: usize> ShrinkToFit for AutoSet<K, H, I>
245+
where
246+
K: Eq + Hash,
247+
H: BuildHasher + Default,
248+
{
249+
fn shrink_to_fit(&mut self) {
250+
self.map.shrink_to_fit();
251+
}
252+
}
253+
243254
#[cfg(test)]
244255
mod tests {
245256
use super::*;

0 commit comments

Comments
 (0)