Skip to content

Commit 1f72be4

Browse files
Slightly more efficient creation of new UDDSketch
1 parent e5b0aa5 commit 1f72be4

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

crates/udd-sketch/src/lib.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -353,22 +353,34 @@ impl UDDSketch {
353353
// This constructor is used to recreate a UddSketch from its component data
354354
pub fn new_from_data(
355355
metadata: &UDDSketchMetadata,
356-
mut keys: impl Iterator<Item = SketchHashKey>,
356+
keys: impl Iterator<Item = SketchHashKey>,
357357
mut counts: impl Iterator<Item = u64>,
358358
) -> Self {
359359
let capacity = metadata.buckets as usize;
360360
let mut sketch = UDDSketch {
361361
buckets: SketchHashMap::with_capacity(capacity),
362362
alpha: metadata.current_error,
363363
gamma: gamma(metadata.current_error),
364-
compactions: u8::try_from(metadata.compactions).expect("compactions cannot be higher than 65"),
365-
max_buckets: NonZeroU32::new(metadata.max_buckets).expect("max buckets should be greater than zero"),
364+
compactions: u8::try_from(metadata.compactions)
365+
.expect("compactions cannot be higher than 65"),
366+
max_buckets: NonZeroU32::new(metadata.max_buckets)
367+
.expect("max buckets should be greater than zero"),
366368
num_values: metadata.values,
367369
values_sum: metadata.sum,
368370
};
369371

372+
let mut keys = keys.into_iter().peekable();
373+
if let Some(key) = keys.peek() {
374+
sketch.buckets.head = *key;
375+
}
376+
377+
// This assumes the keys are unique and sorted
370378
while let (Some(key), Some(count)) = (keys.next(), counts.next()) {
371-
sketch.buckets.entry_upsert(key, count);
379+
let next = keys.peek().map(|k| *k).unwrap_or(Invalid);
380+
sketch
381+
.buckets
382+
.map
383+
.insert(key, SketchHashEntry { next, count });
372384
}
373385
debug_assert_eq!(sketch.buckets.map.len(), capacity);
374386

0 commit comments

Comments
 (0)