Skip to content

Commit b098dcd

Browse files
committed
fix for RoaringBitmap#130 append perf 10x slower than collect
1 parent 435f495 commit b098dcd

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

src/bitmap/iter.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,35 @@ impl RoaringBitmap {
184184
&mut self,
185185
iterator: I,
186186
) -> Result<u64, NonSortedIntegers> {
187-
let mut count = 0;
187+
// Name shadowed to prevent accidentally referencing the param
188+
let mut iterator = iterator.into_iter();
189+
190+
let mut prev: u32 = match iterator.next() {
191+
None => return Ok(0),
192+
Some(first) => {
193+
if let Some(max) = self.max() {
194+
if first <= max {
195+
return Err(NonSortedIntegers { valid_until: 0 });
196+
}
197+
}
198+
199+
first
200+
}
201+
};
202+
203+
self.insert(prev);
204+
let mut count = 1;
205+
206+
// It is now guaranteed that so long as the values are iterator are monotonically
207+
// increasing they must also be the greatest in the set.
188208

189209
for value in iterator {
190-
if self.push(value) {
191-
count += 1;
192-
} else {
210+
if value <= prev {
193211
return Err(NonSortedIntegers { valid_until: count });
212+
} else {
213+
self.insert(value);
214+
prev = value;
215+
count += 1;
194216
}
195217
}
196218

0 commit comments

Comments
 (0)