Skip to content

Commit

Permalink
c
Browse files Browse the repository at this point in the history
  • Loading branch information
nameexhaustion committed Sep 10, 2024
1 parent fe04390 commit 2eafff2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion crates/polars-io/src/mmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl std::ops::Deref for ReaderBytes<'_> {
impl ReaderBytes<'static> {
pub fn into_mem_slice(self) -> MemSlice {
match self {
ReaderBytes::Borrowed(v) => MemSlice::from_slice(v),
ReaderBytes::Borrowed(v) => MemSlice::from_static(v),
ReaderBytes::Owned(v) => MemSlice::from_vec(v),
ReaderBytes::Mapped(v, _) => MemSlice::from_mmap(Arc::new(v)),
}
Expand Down
21 changes: 14 additions & 7 deletions crates/polars-utils/src/mmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ mod private {
}

impl MemSlice {
pub const fn empty() -> Self {
Self::from_static(&[])
}

/// Copy the contents into a new owned `Vec`
#[inline(always)]
pub fn to_vec(self) -> Vec<u8> {
Expand Down Expand Up @@ -101,8 +105,9 @@ mod private {

/// Construct a `MemSlice` that simply wraps around a `&[u8]`.
#[inline]
pub fn from_slice(slice: &'static [u8]) -> Self {
Self::from_bytes(bytes::Bytes::from_static(slice))
pub const fn from_static(slice: &'static [u8]) -> Self {
let inner = MemSliceInner::Bytes(bytes::Bytes::from_static(slice));
Self { slice, inner }
}

/// Attempt to prefetch the memory belonging to to this [`MemSlice`]
Expand Down Expand Up @@ -170,7 +175,7 @@ impl MemReader {
/// slice outlives the returned `MemSlice`.
#[inline]
pub fn from_slice(slice: &'static [u8]) -> Self {
Self::new(MemSlice::from_slice(slice))
Self::new(MemSlice::from_static(slice))
}

#[inline(always)]
Expand Down Expand Up @@ -377,8 +382,9 @@ mod tests {
let slice = vec.as_slice();
let ptr = slice.as_ptr();

let mem_slice =
MemSlice::from_slice(unsafe { std::mem::transmute::<&[u8], &'static [u8]>(slice) });
let mem_slice = MemSlice::from_static(unsafe {
std::mem::transmute::<&[u8], &'static [u8]>(slice)
});
let ptr_out = mem_slice.as_ptr();

assert_eq!(ptr_out, ptr);
Expand All @@ -393,8 +399,9 @@ mod tests {
let vec = vec![1u8, 2, 3, 4, 5];
let slice = vec.as_slice();

let mem_slice =
MemSlice::from_slice(unsafe { std::mem::transmute::<&[u8], &'static [u8]>(slice) });
let mem_slice = MemSlice::from_static(unsafe {
std::mem::transmute::<&[u8], &'static [u8]>(slice)
});

let out = &*mem_slice.slice(3..5);
assert_eq!(out, &slice[3..5]);
Expand Down

0 comments on commit 2eafff2

Please sign in to comment.