diff --git a/roaring/src/bitmap/inherent.rs b/roaring/src/bitmap/inherent.rs index b2919b4c..499b5d23 100644 --- a/roaring/src/bitmap/inherent.rs +++ b/roaring/src/bitmap/inherent.rs @@ -50,15 +50,20 @@ impl RoaringBitmap { /// ``` #[inline] pub fn insert(&mut self, value: u32) -> bool { - let (key, index) = util::split(value); - let container = match self.containers.binary_search_by_key(&key, |c| c.key) { - Ok(loc) => &mut self.containers[loc], - Err(loc) => { - self.containers.insert(loc, Container::new(key)); - &mut self.containers[loc] - } - }; - container.insert(index) + if self.max().map_or(true, |max| value > max) { + self.push_unchecked(value); + true + } else { + let (key, index) = util::split(value); + let container = match self.containers.binary_search_by_key(&key, |c| c.key) { + Ok(loc) => &mut self.containers[loc], + Err(loc) => { + self.containers.insert(loc, Container::new(key)); + &mut self.containers[loc] + } + }; + container.insert(index) + } } /// Search for the specific container by the given key.