Skip to content

Commit 2cf8bfe

Browse files
committed
Reduce allocations in GarbageCollector
1 parent b1afb84 commit 2cf8bfe

1 file changed

Lines changed: 6 additions & 5 deletions

File tree

crates/intern/src/gc.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,20 @@ pub trait GcInternedSliceVisit: SliceInternable {
8787
#[derive(Default)]
8888
pub struct GarbageCollector {
8989
alive: FxHashSet<usize>,
90-
storages: Vec<Box<dyn Storage + Send + Sync>>,
90+
storages: Vec<&'static (dyn Storage + Send + Sync)>,
9191
}
9292

9393
impl GarbageCollector {
9494
pub fn add_storage<T: Internable + GcInternedVisit>(&mut self) {
9595
const { assert!(T::USE_GC) };
9696

97-
self.storages.push(Box::new(InternedStorage::<T>(PhantomData)));
97+
self.storages.push(&InternedStorage::<T>(PhantomData));
9898
}
9999

100100
pub fn add_slice_storage<T: SliceInternable + GcInternedSliceVisit>(&mut self) {
101101
const { assert!(T::USE_GC) };
102102

103-
self.storages.push(Box::new(InternedSliceStorage::<T>(PhantomData)));
103+
self.storages.push(&InternedSliceStorage::<T>(PhantomData));
104104
}
105105

106106
/// # Safety
@@ -111,11 +111,12 @@ impl GarbageCollector {
111111
/// - [`GcInternedVisit`] and [`GcInternedSliceVisit`] must mark all values reachable from the node.
112112
pub unsafe fn collect(mut self) {
113113
let total_nodes = self.storages.iter().map(|storage| storage.len()).sum();
114-
self.alive = FxHashSet::with_capacity_and_hasher(total_nodes, FxBuildHasher);
114+
self.alive.clear();
115+
self.alive.reserve(total_nodes);
115116

116117
let storages = std::mem::take(&mut self.storages);
117118

118-
for storage in &storages {
119+
for &storage in &storages {
119120
storage.mark(&mut self);
120121
}
121122

0 commit comments

Comments
 (0)