Skip to content

Commit

Permalink
perf: Shrink MemSliceInner enum (#17991)
Browse files Browse the repository at this point in the history
  • Loading branch information
nameexhaustion authored Aug 1, 2024
1 parent 1ad9b03 commit a10f9b7
Showing 1 changed file with 17 additions and 19 deletions.
36 changes: 17 additions & 19 deletions crates/polars-utils/src/mmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ mod private {

use crate::mem::prefetch_l2;

/// A read-only reference to a slice of memory.
/// A read-only reference to a slice of memory that can potentially be memory-mapped.
///
/// This maintains a reference count to the underlying buffer to ensure the memory is kept
/// alive. [`MemSlice::slice`] can be used to slice the memory in a zero-copy manner.
/// A reference count is kept to the underlying buffer to ensure the memory is kept alive.
/// [`MemSlice::slice`] can be used to slice the memory in a zero-copy manner.
///
/// This still owns the all the original memory and therefore should probably not be a long-lasting
/// structure.
Expand All @@ -33,10 +33,8 @@ mod private {
#[derive(Clone, Debug)]
#[allow(unused)]
enum MemSliceInner {
Vec(Arc<Vec<u8>>),
Bytes(bytes::Bytes),
Mmap(Arc<Mmap>),
Unbacked,
}

impl Deref for MemSlice {
Expand All @@ -50,10 +48,7 @@ mod private {

impl Default for MemSlice {
fn default() -> Self {
Self {
slice: &[],
inner: MemSliceInner::Unbacked,
}
Self::from_bytes(bytes::Bytes::new())
}
}

Expand All @@ -67,12 +62,7 @@ mod private {
/// Construct a `MemSlice` from an existing `Vec<u8>`. This is zero-copy.
#[inline]
pub fn from_vec(v: Vec<u8>) -> Self {
let arc_vec = Arc::new(v);

Self {
slice: unsafe { std::mem::transmute::<&[u8], &'static [u8]>(arc_vec.as_slice()) },
inner: MemSliceInner::Vec(arc_vec),
}
Self::from_bytes(bytes::Bytes::from(v))
}

/// Construct a `MemSlice` from [`bytes::Bytes`]. This is zero-copy.
Expand All @@ -97,10 +87,7 @@ mod private {
/// Construct a `MemSlice` that simply wraps around a `&[u8]`.
#[inline]
pub fn from_slice(slice: &'static [u8]) -> Self {
Self {
slice,
inner: MemSliceInner::Unbacked,
}
Self::from_bytes(bytes::Bytes::from_static(slice))
}

/// Attempt to prefetch the memory belonging to to this [`MemSlice`]
Expand Down Expand Up @@ -267,6 +254,17 @@ mod tests {
assert_eq!(ptr_out, ptr);
}

{
let mut vec = vec![1u8, 2, 3, 4, 5];
vec.truncate(2);
let ptr = vec.as_ptr();

let mem_slice = MemSlice::from_vec(vec);
let ptr_out = mem_slice.as_ptr();

assert_eq!(ptr_out, ptr);
}

{
let bytes = bytes::Bytes::from(vec![1u8, 2, 3, 4, 5]);
let ptr = bytes.as_ptr();
Expand Down

0 comments on commit a10f9b7

Please sign in to comment.