Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement memory pool for mutable buffer #82

Open
wants to merge 1 commit into
base: poc-memory-pool
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 86 additions & 4 deletions arrow-buffer/src/buffer/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use crate::{
bytes::Bytes,
native::{ArrowNativeType, ToByteSlice},
util::bit_util,
MemoryPool,
};

use super::Buffer;
Expand Down Expand Up @@ -57,6 +58,9 @@ pub struct MutableBuffer {
// invariant: len <= capacity
len: usize,
layout: Layout,

#[cfg(feature = "pool")]
reservation: std::sync::Mutex<Option<Box<dyn crate::MemoryReservation>>>,
}

impl MutableBuffer {
Expand Down Expand Up @@ -91,6 +95,8 @@ impl MutableBuffer {
data,
len: 0,
layout,
#[cfg(feature = "pool")]
reservation: std::sync::Mutex::new(None),
}
}

Expand All @@ -115,7 +121,13 @@ impl MutableBuffer {
NonNull::new(raw_ptr).unwrap_or_else(|| handle_alloc_error(layout))
}
};
Self { data, len, layout }
Self {
data,
len,
layout,
#[cfg(feature = "pool")]
reservation: std::sync::Mutex::new(None),
}
}

/// Create a [`MutableBuffer`] from the provided [`Vec`] without copying
Expand All @@ -136,7 +148,13 @@ impl MutableBuffer {
let data = bytes.ptr();
mem::forget(bytes);

Ok(Self { data, len, layout })
Ok(Self {
data,
len,
layout,
#[cfg(feature = "pool")]
reservation: std::sync::Mutex::new(None),
})
}

/// creates a new [MutableBuffer] with capacity and length capable of holding `len` bits.
Expand Down Expand Up @@ -211,7 +229,11 @@ impl MutableBuffer {
if self.layout.size() != 0 {
// Safety: data was allocated with layout
unsafe { std::alloc::dealloc(self.as_mut_ptr(), self.layout) };
self.layout = new_layout
self.layout = new_layout;
#[cfg(feature = "pool")]
if let Some(reservation) = self.reservation.lock().unwrap().as_mut() {
reservation.resize(0);
}
}
return;
}
Expand All @@ -224,6 +246,10 @@ impl MutableBuffer {
};
self.data = NonNull::new(data).unwrap_or_else(|| handle_alloc_error(new_layout));
self.layout = new_layout;
#[cfg(feature = "pool")]
if let Some(reservation) = self.reservation.lock().unwrap().as_mut() {
reservation.resize(capacity);
}
}

/// Truncates this buffer to `len` bytes
Expand Down Expand Up @@ -340,13 +366,30 @@ impl MutableBuffer {
self.into_buffer()
}

#[cfg(not(feature = "pool"))]
#[inline]
pub(super) fn into_buffer(self) -> Buffer {
let bytes = unsafe { Bytes::new(self.data, self.len, Deallocation::Standard(self.layout)) };
std::mem::forget(self);
Buffer::from_bytes(bytes)
}

#[cfg(feature = "pool")]
#[inline]
pub(super) fn into_buffer(self) -> Buffer {
let reservation = std::mem::take(&mut *self.reservation.lock().unwrap());
let bytes = unsafe {
Bytes::new_with_reservation(
self.data,
self.len,
Deallocation::Standard(self.layout),
reservation,
)
};
std::mem::forget(self);
Buffer::from_bytes(bytes)
}

/// View this buffer as a mutable slice of a specific type.
///
/// # Panics
Expand Down Expand Up @@ -481,6 +524,15 @@ impl MutableBuffer {
buffer.truncate(bit_util::ceil(len, 8));
buffer
}

#[cfg(feature = "pool")]
/// Register this [`MutableBuffer`] with the provided [`MemoryPool`], replacing any prior assignment
pub fn claim(&self, pool: &dyn MemoryPool) {
self.reservation
.lock()
.unwrap()
.replace(pool.register(self.capacity()));
}
}

#[inline]
Expand Down Expand Up @@ -518,7 +570,13 @@ impl<T: ArrowNativeType> From<Vec<T>> for MutableBuffer {
// This is based on `RawVec::current_memory`
let layout = unsafe { Layout::array::<T>(value.capacity()).unwrap_unchecked() };
mem::forget(value);
Self { data, len, layout }
Self {
data,
len,
layout,
#[cfg(feature = "pool")]
reservation: std::sync::Mutex::new(None),
}
}
}

Expand Down Expand Up @@ -1025,4 +1083,28 @@ mod tests {
let max_capacity = isize::MAX as usize - (isize::MAX as usize % ALIGNMENT);
let _ = MutableBuffer::with_capacity(max_capacity + 1);
}

#[cfg(feature = "pool")]
#[test]
fn test_memory_pool() {
use crate::TrackingMemoryPool;

let pool = TrackingMemoryPool::default();
let mut buffer = MutableBuffer::from_iter(std::iter::repeat(1u64).take(64));
buffer.claim(&pool);
assert_eq!(pool.allocated(), buffer.capacity());

buffer.reserve(128);
assert_eq!(pool.allocated(), buffer.capacity());

buffer.shrink_to_fit();
assert_eq!(pool.allocated(), buffer.capacity());

buffer.claim(&pool);
buffer.claim(&pool);
assert_eq!(pool.allocated(), buffer.capacity());

let immutable = buffer.into_buffer();
assert_eq!(pool.allocated(), immutable.capacity());
}
}
19 changes: 19 additions & 0 deletions arrow-buffer/src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,25 @@ impl Bytes {
}
}

/// Takes ownership of an allocated memory region with a reservation.
///
/// Similiar to [`Bytes::new`], but also takes a reservation that is used to track the memory usage.
#[cfg(feature = "pool")]
#[inline]
pub(crate) unsafe fn new_with_reservation(
ptr: NonNull<u8>,
len: usize,
deallocation: Deallocation,
reservation: Option<Box<dyn crate::MemoryReservation>>,
) -> Bytes {
Bytes {
ptr,
len,
deallocation,
reservation: std::sync::Mutex::new(reservation),
}
}

fn as_slice(&self) -> &[u8] {
self
}
Expand Down
2 changes: 1 addition & 1 deletion arrow-buffer/src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub trait MemoryPool {
}

/// A memory reservation within a [`MemoryPool`] that is freed on drop
pub trait MemoryReservation {
pub trait MemoryReservation: std::fmt::Debug {
/// Resize this reservation to `new` bytes
fn resize(&mut self, new: usize);
}
Expand Down