Skip to content

Commit df0ae7f

Browse files
Reserve HashMap capacity if length is known
1 parent 21cb95a commit df0ae7f

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

crates/udd-sketch/src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@ impl SketchHashMap {
124124
}
125125
}
126126

127+
fn with_capacity(capacity: usize) -> SketchHashMap {
128+
SketchHashMap {
129+
map: HashMap::with_capacity(capacity),
130+
head: SketchHashKey::Invalid,
131+
}
132+
}
133+
127134
/// Increment the count at a key, creating the entry if needed.
128135
fn increment(&mut self, key: SketchHashKey) {
129136
self.entry_upsert(key, 1);
@@ -273,7 +280,7 @@ impl UDDSketch {
273280
counts: impl Iterator<Item = u64>,
274281
) -> Self {
275282
let mut sketch = UDDSketch {
276-
buckets: SketchHashMap::new(),
283+
buckets: SketchHashMap::with_capacity(metadata.values as usize),
277284
alpha: metadata.current_error,
278285
gamma: gamma(metadata.current_error),
279286
compactions: metadata.compactions,

0 commit comments

Comments
 (0)