From 623949636a2d74f48ba85ae6a2f147dc102e27eb Mon Sep 17 00:00:00 2001 From: Aleksandar Zecevic Date: Thu, 5 Nov 2026 23:47:36 +0000 Subject: [PATCH 1/2] perf(arrow-buffer): use a static address for buffers without an allocation Buffers without an allocation used a dangling pointer in the unmapped first page: `ALIGNMENT` from `MutableBuffer::new(0)`, or the pointer of an empty `Vec` (`0x1` for `Vec`). Rust compares empty slices with `memcmp` and `bcmp` of length 0, and on x86-64 CPUs with AVX512VL and AVX512BW, glibc 2.35 and later make these calls slow at an unmapped address: up to about 100 ns instead of 2 ns. So each compare of values of an all-empty string or binary array was slow, for example in Parquet statistics and in the comparison and sort kernels. All such buffers now point at one static, aligned area, as Arrow C++ does (`zero_size_area`): `dangling_ptr()`, a `Vec` without an allocation in `From> for MutableBuffer`, and `MutableBuffer` after shrinking to size 0, which kept the pointer to the freed memory. --- arrow-buffer/src/buffer/mutable.rs | 51 +++++++++++++++++++----------- 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/arrow-buffer/src/buffer/mutable.rs b/arrow-buffer/src/buffer/mutable.rs index 40db1161da27..77de87344f98 100644 --- a/arrow-buffer/src/buffer/mutable.rs +++ b/arrow-buffer/src/buffer/mutable.rs @@ -415,6 +415,7 @@ impl MutableBuffer { if self.layout.size() != 0 { // Safety: data was allocated with layout unsafe { std::alloc::dealloc(self.as_mut_ptr(), self.layout) }; + self.data = dangling_ptr(); self.layout = new_layout; } return Ok(()); @@ -927,23 +928,23 @@ impl MutableBuffer { } } -/// Creates a non-null pointer with alignment of [`ALIGNMENT`] -/// -/// This is similar to [`NonNull::dangling`] +#[repr(align(256))] +struct ZeroSizeArea { + _data: [u8; 1], +} + +/// The address of all buffers without an allocation. A dangling pointer such as +/// [`NonNull::dangling`] is in the unmapped first page, and glibc's AVX-512 `memcmp` +/// and `bcmp` are slow at an unmapped address also for length 0, which Rust uses to +/// compare empty slices. Arrow C++ uses a static in the same way (`zero_size_area`). +static ZERO_SIZE_AREA: ZeroSizeArea = ZeroSizeArea { _data: [0] }; + +const _: () = assert!(mem::align_of::() >= ALIGNMENT); + +/// Returns the pointer of buffers without an allocation, see `ZERO_SIZE_AREA` #[inline] pub(crate) fn dangling_ptr() -> NonNull { - // SAFETY: ALIGNMENT is a non-zero usize which is then cast - // to a *mut u8. Therefore, `ptr` is not null and the conditions for - // calling new_unchecked() are respected. - #[cfg(miri)] - { - // Since miri implies a nightly rust version we can use the unstable strict_provenance feature - unsafe { NonNull::new_unchecked(std::ptr::without_provenance_mut(ALIGNMENT)) } - } - #[cfg(not(miri))] - { - unsafe { NonNull::new_unchecked(ALIGNMENT as *mut u8) } - } + NonNull::from(&ZERO_SIZE_AREA).cast() } impl Extend for MutableBuffer { @@ -956,9 +957,13 @@ impl Extend for MutableBuffer { impl From> for MutableBuffer { fn from(mut value: Vec) -> Self { - // Safety - // Vec::as_mut_ptr guaranteed to not be null and ArrowNativeType are trivially transmutable - let data = unsafe { NonNull::new_unchecked(value.as_mut_ptr().cast()) }; + let data = match value.capacity() { + // A Vec without an allocation has a dangling pointer, see `ZERO_SIZE_AREA` + 0 => dangling_ptr(), + // Safety + // Vec::as_mut_ptr guaranteed to not be null and ArrowNativeType are trivially transmutable + _ => unsafe { NonNull::new_unchecked(value.as_mut_ptr().cast()) }, + }; let len = value.len() * mem::size_of::(); // Safety // Vec guaranteed to have a valid layout matching that of `Layout::array` @@ -1290,6 +1295,16 @@ impl std::iter::FromIterator for MutableBuffer { mod tests { use super::*; + #[test] + fn test_buffers_without_allocation_point_to_zero_size_area() { + let area = (&raw const ZERO_SIZE_AREA).cast::(); + assert_eq!(MutableBuffer::new(0).as_ptr(), area); + assert_eq!(MutableBuffer::from(Vec::::new()).as_ptr(), area); + let mut shrunk = MutableBuffer::with_capacity(64); + shrunk.shrink_to_fit(); + assert_eq!(shrunk.as_ptr(), area); + } + #[test] fn test_mutable_new() { let buf = MutableBuffer::new(63); From 15d8e13af1ff227dc53d699e1cc9ecb107538765 Mon Sep 17 00:00:00 2001 From: Aleksandar Zecevic Date: Thu, 3 Dec 2026 20:27:02 +0100 Subject: [PATCH 2/2] Matt's feedback --- arrow-buffer/src/buffer/mutable.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/arrow-buffer/src/buffer/mutable.rs b/arrow-buffer/src/buffer/mutable.rs index 77de87344f98..dae6156fe724 100644 --- a/arrow-buffer/src/buffer/mutable.rs +++ b/arrow-buffer/src/buffer/mutable.rs @@ -930,6 +930,7 @@ impl MutableBuffer { #[repr(align(256))] struct ZeroSizeArea { + // One byte so that the static has storage of its own, see `ZERO_SIZE_AREA` _data: [u8; 1], }