Skip to content

Commit

Permalink
Auto merge of #3027 - ttsugriy:range-map, r=RalfJung
Browse files Browse the repository at this point in the history
Avoid unnecessary Vec resize.

If `size > 0` current implementation will first create an empty vec and then push an element into it, which will cause a resize that can be easily avoided.

It's obviously not a big deal, but this also gets rid of `mut` local variable.
  • Loading branch information
bors committed Aug 16, 2023
2 parents 5a51542 + 6cd057e commit 4810142
Showing 1 changed file with 2 additions and 5 deletions.
7 changes: 2 additions & 5 deletions src/range_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,8 @@ impl<T> RangeMap<T> {
#[inline(always)]
pub fn new(size: Size, init: T) -> RangeMap<T> {
let size = size.bytes();
let mut map = RangeMap { v: Vec::new() };
if size > 0 {
map.v.push(Elem { range: 0..size, data: init });
}
map
let v = if size > 0 { vec![Elem { range: 0..size, data: init }] } else { Vec::new() };
RangeMap { v }
}

/// Finds the index containing the given offset.
Expand Down

0 comments on commit 4810142

Please sign in to comment.