Skip to content

Commit 38567b0

Browse files
committed
A few tweaks to make going from size 1 to 0 faster
We don't want to reallocate a vec when going to 0, but keep the buffer! So no mk_empty ever!
1 parent 9f5f61f commit 38567b0

File tree

1 file changed

+9
-16
lines changed

1 file changed

+9
-16
lines changed

src/lib.rs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,9 @@ impl<T> SortedIndexBuffer<T> {
325325
}
326326
if min1 >= max1 {
327327
// resizing to empty buffer
328-
*self = Self::new();
328+
self.data.clear();
329+
self.min = 0;
330+
self.max = 0;
329331
return;
330332
}
331333
let len0 = self.buf().len();
@@ -345,14 +347,11 @@ impl<T> SortedIndexBuffer<T> {
345347
self.buf_mut().rotate_left(shift);
346348
}
347349
} else if len0 < len1 {
350+
for _ in len0..len1 {
351+
self.data.push(None);
352+
}
348353
// Grow
349-
if len0 == 0 {
350-
// buffer was empty before.
351-
self.data = mk_empty(len1);
352-
} else {
353-
self.data
354-
.extend(std::iter::repeat_with(|| None).take(len1 - len0));
355-
354+
if len0 != 0 {
356355
let start0 = (self.min - base0) as usize;
357356
let start1 = (self.min - base1) as usize;
358357
let count = (self.max - self.min) as usize;
@@ -418,22 +417,16 @@ impl<T> SortedIndexBuffer<T> {
418417
}
419418
}
420419

421-
fn mk_empty<T>(n: usize) -> Vec<Option<T>> {
422-
let mut res = Vec::with_capacity(n);
423-
for _ in 0..n {
424-
res.push(None);
425-
}
426-
res
427-
}
428-
429420
/// Compute the minimum buffer length needed to cover [min, max) even in the
430421
/// case where min..max go over a page boundary.
422+
#[inline(always)]
431423
fn buf_len(min: u64, max: u64) -> usize {
432424
let page_size = (max - min).next_power_of_two() as usize;
433425
page_size * 2
434426
}
435427

436428
/// Compute the base index for the buffer covering [min, max).
429+
#[inline(always)]
437430
fn base(min: u64, max: u64) -> u64 {
438431
let buf_len = buf_len(min, max);
439432
let mask = (buf_len as u64) / 2 - 1;

0 commit comments

Comments
 (0)