Skip to content

Commit

Permalink
reduce branchiness
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas Markeffsky committed Jan 17, 2024
1 parent 53a3f8a commit c809b58
Showing 1 changed file with 22 additions and 31 deletions.
53 changes: 22 additions & 31 deletions library/alloc/src/collections/vec_deque/drain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ impl<T, A: Allocator> Drop for Drain<'_, T, A> {

let source_deque = unsafe { self.0.deque.as_mut() };

let drain_start = source_deque.len();
let drain_len = self.0.drain_len;
let new_len = self.0.new_len;

Expand All @@ -119,40 +118,32 @@ impl<T, A: Allocator> Drop for Drain<'_, T, A> {
return;
}

let head_len = drain_start;
let head_len = source_deque.len();
let tail_len = new_len - head_len;

match (head_len, tail_len) {
(0, 0) => {
source_deque.head = 0;
source_deque.len = 0;
}
(0, _) => {
source_deque.head = source_deque.to_physical_idx(drain_len);
source_deque.len = new_len;
}
(_, 0) => {
source_deque.len = new_len;
if head_len != 0 && tail_len != 0 {
let (src, dst, len);
if head_len < tail_len {
src = source_deque.head;
dst = source_deque.to_physical_idx(drain_len);
len = head_len;
} else {
src = source_deque.to_physical_idx(head_len + drain_len);
dst = source_deque.to_physical_idx(head_len);
len = tail_len;
};

unsafe {
source_deque.wrap_copy(src, dst, len);
}
_ => unsafe {
if head_len <= tail_len {
source_deque.wrap_copy(
source_deque.head,
source_deque.to_physical_idx(drain_len),
head_len,
);
source_deque.head = source_deque.to_physical_idx(drain_len);
source_deque.len = new_len;
} else {
source_deque.wrap_copy(
source_deque.to_physical_idx(head_len + drain_len),
source_deque.to_physical_idx(head_len),
tail_len,
);
source_deque.len = new_len;
}
},
}

if new_len == 0 {
source_deque.head = 0;
} else if head_len < tail_len {
source_deque.head = source_deque.to_physical_idx(drain_len);
}
source_deque.len = new_len;
}
}

Expand Down

0 comments on commit c809b58

Please sign in to comment.