about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLukas Markeffsky <@>2023-11-23 16:03:21 +0000
committerLukas Markeffsky <@>2024-02-16 13:10:52 +0100
commit1e3849aed010c3ff2a4c7b7b644ca67d087ab4c1 (patch)
treecb41e989c02f6cbdee81b268c23aa663e1bf933f
parent500ef678770d9a0840e913ec02fab801ea455187 (diff)
downloadrust-1e3849aed010c3ff2a4c7b7b644ca67d087ab4c1.tar.gz
rust-1e3849aed010c3ff2a4c7b7b644ca67d087ab4c1.zip
reduce branchiness
-rw-r--r--library/alloc/src/collections/vec_deque/drain.rs53
1 files changed, 22 insertions, 31 deletions
diff --git a/library/alloc/src/collections/vec_deque/drain.rs b/library/alloc/src/collections/vec_deque/drain.rs
index aa5cc0fb1ce..7a413ddf971 100644
--- a/library/alloc/src/collections/vec_deque/drain.rs
+++ b/library/alloc/src/collections/vec_deque/drain.rs
@@ -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;
 
@@ -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;
             }
         }