about summary refs log tree commit diff
path: root/library/std/src/alloc.rs
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2020-08-18 05:42:05 +0000
committerbors <bors@rust-lang.org>2020-08-18 05:42:05 +0000
commit515c9fa505e18a65d7f61bc3e9eb833b79a68618 (patch)
tree086afaac3181e4bde71c5a31ab5fdb048f5bbe8e /library/std/src/alloc.rs
parentb97e9b5dc74e705cd01603c5f735652a04460a60 (diff)
parentc48f7844185cadc51af6ac5fd4db48324fd02882 (diff)
downloadrust-515c9fa505e18a65d7f61bc3e9eb833b79a68618.tar.gz
rust-515c9fa505e18a65d7f61bc3e9eb833b79a68618.zip
Auto merge of #75621 - TimDiekmann:no-fast-realloc, r=Amanieu
Remove fast path in reallocation for same layout sizes

r? @Amanieu

Before merging a perf-run should be done.

Closes https://github.com/rust-lang/wg-allocators/issues/70
Diffstat (limited to 'library/std/src/alloc.rs')
-rw-r--r--library/std/src/alloc.rs27
1 files changed, 10 insertions, 17 deletions
diff --git a/library/std/src/alloc.rs b/library/std/src/alloc.rs
index 37a8f514aa1..74427199346 100644
--- a/library/std/src/alloc.rs
+++ b/library/std/src/alloc.rs
@@ -181,16 +181,14 @@ unsafe impl AllocRef for System {
         );
 
         // SAFETY: `new_size` must be non-zero, which is checked in the match expression.
+        // If `new_size` is zero, then `old_size` has to be zero as well.
         // Other conditions must be upheld by the caller
         unsafe {
             match layout.size() {
-                old_size if old_size == new_size => {
-                    Ok(NonNull::slice_from_raw_parts(ptr, new_size))
-                }
                 0 => self.alloc(Layout::from_size_align_unchecked(new_size, layout.align())),
                 old_size => {
-                    // `realloc` probably checks for `new_size > size` or something similar.
-                    intrinsics::assume(new_size > old_size);
+                    // `realloc` probably checks for `new_size >= size` or something similar.
+                    intrinsics::assume(new_size >= old_size);
                     let raw_ptr = GlobalAlloc::realloc(&System, ptr.as_ptr(), layout, new_size);
                     let ptr = NonNull::new(raw_ptr).ok_or(AllocErr)?;
                     Ok(NonNull::slice_from_raw_parts(ptr, new_size))
@@ -212,16 +210,14 @@ unsafe impl AllocRef for System {
         );
 
         // SAFETY: `new_size` must be non-zero, which is checked in the match expression.
+        // If `new_size` is zero, then `old_size` has to be zero as well.
         // Other conditions must be upheld by the caller
         unsafe {
             match layout.size() {
-                old_size if old_size == new_size => {
-                    Ok(NonNull::slice_from_raw_parts(ptr, new_size))
-                }
                 0 => self.alloc_zeroed(Layout::from_size_align_unchecked(new_size, layout.align())),
                 old_size => {
-                    // `realloc` probably checks for `new_size > size` or something similar.
-                    intrinsics::assume(new_size > old_size);
+                    // `realloc` probably checks for `new_size >= size` or something similar.
+                    intrinsics::assume(new_size >= old_size);
                     let raw_ptr = GlobalAlloc::realloc(&System, ptr.as_ptr(), layout, new_size);
                     raw_ptr.add(old_size).write_bytes(0, new_size - old_size);
                     let ptr = NonNull::new(raw_ptr).ok_or(AllocErr)?;
@@ -244,11 +240,8 @@ unsafe impl AllocRef for System {
             "`new_size` must be smaller than or equal to `layout.size()`"
         );
 
-        let ptr = if new_size == old_size {
-            ptr
-        } else if new_size == 0 {
-            // SAFETY: `layout` is non-zero in size as `old_size` != `new_size`
-            // Other conditions must be upheld by the caller
+        let ptr = if new_size == 0 {
+            // SAFETY: conditions must be upheld by the caller
             unsafe {
                 self.dealloc(ptr, layout);
             }
@@ -257,8 +250,8 @@ unsafe impl AllocRef for System {
             // SAFETY: new_size is not zero,
             // Other conditions must be upheld by the caller
             let raw_ptr = unsafe {
-                // `realloc` probably checks for `new_size < old_size` or something similar.
-                intrinsics::assume(new_size < old_size);
+                // `realloc` probably checks for `new_size <= old_size` or something similar.
+                intrinsics::assume(new_size <= old_size);
                 GlobalAlloc::realloc(&System, ptr.as_ptr(), layout, new_size)
             };
             NonNull::new(raw_ptr).ok_or(AllocErr)?