nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: fitzgen/bumpalo
Pull request #73 (2020-04-26 00:03:55)

Simplify code handling ZSTs
Allocation of ZSTs is very rare, so remove checks for zero-sized
layouts in the fast path and instead just use the normal code path
which will bump the pointer by zero bytes.


Patch:
diff --git a/src/lib.rs b/src/lib.rs
index 2e034cc..4ea60ea 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -816,19 +816,11 @@ impl Bump {
 
     #[inline(always)]
     fn try_alloc_layout_fast(&self, layout: Layout) -> Option<NonNull<u8>> {
+        // We don't need to check for ZSTs here since they will automatically
+        // be handled properly: the pointer will be bumped by zero bytes,
+        // modulo alignment. This keeps the fast path optimized for non-ZSTs,
+        // which are much more common.
         unsafe {
-            if layout.size() == 0 {
-                // We want to use NonNull::dangling here, but that function uses mem::align_of::<T>
-                // internally. For our use-case we cannot call dangling::<T>, since we are not generic
-                // over T; we only have access to the Layout of T. Instead we re-implement the
-                // functionality here.
-                //
-                // See https://github.com/rust-lang/rust/blob/9966af3/src/libcore/ptr/non_null.rs#L70
-                // for the reference implementation.
-                let ptr = layout.align() as *mut u8;
-                return Some(NonNull::new_unchecked(ptr));
-            }
-
             let footer = self.current_chunk_footer.get();
             let footer = footer.as_ref();
             let ptr = footer.ptr.get().as_ptr() as usize;
@@ -1081,7 +1073,7 @@ unsafe impl<'a> alloc::Alloc for &'a Bump {
     unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
         // If the pointer is the last allocation we made, we can reuse the bytes,
         // otherwise they are simply leaked -- at least until somebody calls reset().
-        if layout.size() != 0 && self.is_last_allocation(ptr) {
+        if self.is_last_allocation(ptr) {
             let ptr = NonNull::new_unchecked(ptr.as_ptr().add(layout.size()));
             self.current_chunk_footer.get().as_ref().ptr.set(ptr);
         }


Test patch:
diff --git a/tests/alloc_fill.rs b/tests/alloc_fill.rs
index 275d27d..70bd84f 100644
--- a/tests/alloc_fill.rs
+++ b/tests/alloc_fill.rs
@@ -16,10 +16,11 @@ fn alloc_slice_fill_zero() {
     b.alloc_slice_fill_copy(0, 42u64);
     b.alloc_slice_fill_clone(0, &"hello".to_string());
     b.alloc_slice_fill_default::<String>(0);
-    b.alloc(MyZeroSizedType);
+    let ptr2 = b.alloc(MyZeroSizedType);
+    assert_eq!(ptr1.as_ptr() as usize & !7, ptr2 as *mut _ as usize);
 
-    let ptr2 = b.alloc_layout(layout);
-    assert_eq!(ptr1.as_ptr() as usize, ptr2.as_ptr() as usize + 1);
+    let ptr3 = b.alloc_layout(layout);
+    assert_eq!(ptr2 as *mut _ as usize, ptr3.as_ptr() as usize + 1);
 }
 
 #[test]
Repository: fitzgen/bumpalo
Pull request #184 (2022-11-03 02:02:26)

allocated_bytes_with_headers function
We are implementing memory profiler, and for such profiler we need to know allocation overhead as precise as possible (especially for small bumps where header size overhead might be significant).

`allocated_bytes` function does not include header size.

So add `allocated_bytes_with_headers` which returns the amount of memory requested from rust allocator.

stepancheg: Applied the suggestions.
stepancheg: No problem! Added quickcheck test.
fitzgen: Looks like MIRI tests are failing in CI. I suspect this is due to a change in MIRI and existing code here, but I don't have the time to investigate at the moment.

Patch:
diff --git a/src/lib.rs b/src/lib.rs
index 74dfcd4..edf5019 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1696,6 +1696,16 @@ impl Bump {
         unsafe { footer.as_ref().allocated_bytes }
     }
 
+    /// Calculates the number of bytes requested from the Rust allocator for this `Bump`.
+    ///
+    /// This number is equal to the [`allocated_bytes()`](Self::allocated_bytes) plus
+    /// the size of the bump metadata.
+    pub fn allocated_bytes_including_metadata(&self) -> usize {
+        let metadata_size =
+            unsafe { self.iter_allocated_chunks_raw().count() * mem::size_of::<ChunkFooter>() };
+        self.allocated_bytes() + metadata_size
+    }
+
     #[inline]
     unsafe fn is_last_allocation(&self, ptr: NonNull<u8>) -> bool {
         let footer = self.current_chunk_footer.get();


Test patch:
diff --git a/tests/all/quickchecks.rs b/tests/all/quickchecks.rs
index a89f352..e99278a 100644
--- a/tests/all/quickchecks.rs
+++ b/tests/all/quickchecks.rs
@@ -284,4 +284,26 @@ quickcheck! {
 
         bump.allocated_bytes() <= limit
     }
+
+    fn allocated_bytes_including_metadata(allocs: Vec<usize>) -> () {
+        let b = Bump::new();
+        let mut slice_bytes = 0;
+        let allocs_len = allocs.len();
+        for len in allocs {
+            const MAX_LEN: usize = 512;
+            let len = len % MAX_LEN;
+            b.alloc_slice_fill_copy(len, 0);
+            slice_bytes += len;
+            let allocated_bytes = b.allocated_bytes();
+            let allocated_bytes_including_metadata = b.allocated_bytes_including_metadata();
+            if slice_bytes == 0 {
+                assert_eq!(allocated_bytes, 0);
+                assert_eq!(allocated_bytes_including_metadata, 0);
+            } else {
+                assert!(allocated_bytes >= slice_bytes);
+                assert!(allocated_bytes_including_metadata > allocated_bytes);
+                assert!(allocated_bytes_including_metadata < allocated_bytes + allocs_len * 100);
+            }
+        }
+    }
 }
Repository: fitzgen/bumpalo
Pull request #209 (2023-07-07 22:23:27)

Fix chunk_capacity to return the remaining capacity, not the total
`chunk_capacity` is documented as returning "the remaining capacity in the current chunk", but actually returns the *total* capacity for the current chunk.

(Arguably, the actual behavior is itself totally sensible, and this is a documentation bug, though in that case I would propose adding a new function that returns the remaining capacity. I would like it for debugging/measurement purposes.)


Patch:
diff --git a/src/lib.rs b/src/lib.rs
index c25e03c..76ab208 100755
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1455,7 +1455,7 @@ impl Bump {
         let current_footer = self.current_chunk_footer.get();
         let current_footer = unsafe { current_footer.as_ref() };
 
-        current_footer as *const _ as usize - current_footer.data.as_ptr() as usize
+        current_footer.ptr.get().as_ptr() as usize - current_footer.data.as_ptr() as usize
     }
 
     /// Slow path allocation for when we need to allocate a new chunk from the


Test patch:
diff --git a/tests/all/tests.rs b/tests/all/tests.rs
index 206cdb8..2ee3720 100644
--- a/tests/all/tests.rs
+++ b/tests/all/tests.rs
@@ -201,3 +201,11 @@ fn test_alignment() {
         }
     }
 }
+
+#[test]
+fn test_chunk_capacity() {
+    let b = Bump::with_capacity(512);
+    let orig_capacity = b.chunk_capacity();
+    b.alloc(true);
+    assert!(b.chunk_capacity() < orig_capacity);
+}