Skip to content

Commit a79f891

Browse files
committed
fast path for when the buffer is empty
this happens a lot if you toggle from empty to non-empty a lot!
1 parent 38567b0 commit a79f891

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/lib.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,15 @@ impl<T> SortedIndexBuffer<T> {
251251

252252
/// Insert value at index.
253253
pub fn insert(&mut self, index: u64, value: T) {
254-
let (min1, max1) = if self.is_empty() {
255-
(index, index + 1)
254+
if self.is_empty() {
255+
self.min = index;
256+
self.max = index + 1;
257+
self.data.push(Some(value));
256258
} else {
257-
(self.min.min(index), self.max.max(index + 1))
258-
};
259-
self.resize(min1, max1);
260-
self.insert0(index, value);
259+
let (min1, max1) = (self.min.min(index), self.max.max(index + 1));
260+
self.resize(min1, max1);
261+
self.insert0(index, value);
262+
}
261263
self.check_invariants();
262264
}
263265

@@ -298,6 +300,7 @@ impl<T> SortedIndexBuffer<T> {
298300
/// Insert value at index, assuming the buffer already covers that index.
299301
///
300302
/// The resulting buffer may violate the invariants.
303+
#[inline(always)]
301304
fn insert0(&mut self, index: u64, value: T) {
302305
let base = base(self.min, self.max);
303306
let offset = (index - base) as usize;
@@ -307,6 +310,7 @@ impl<T> SortedIndexBuffer<T> {
307310
/// Remove value at index without resizing the buffer.
308311
///
309312
/// The resulting buffer may violate the invariants.
313+
#[inline(always)]
310314
fn remove0(&mut self, index: u64) -> Option<T> {
311315
if index < self.min || index >= self.max {
312316
return None;
@@ -318,6 +322,7 @@ impl<T> SortedIndexBuffer<T> {
318322

319323
/// Resize the buffer to cover the range [`min1`, `max1`), while preserving existing
320324
/// elements.
325+
#[inline(always)]
321326
fn resize(&mut self, min1: u64, max1: u64) {
322327
if min1 == self.min && max1 == self.max {
323328
// nothing to do
@@ -374,10 +379,12 @@ impl<T> SortedIndexBuffer<T> {
374379
self.max = max1;
375380
}
376381

382+
#[inline(always)]
377383
fn buf(&self) -> &[Option<T>] {
378384
&self.data
379385
}
380386

387+
#[inline(always)]
381388
fn buf_mut(&mut self) -> &mut [Option<T>] {
382389
&mut self.data
383390
}

0 commit comments

Comments
 (0)