Skip to content

Commit

Permalink
c
Browse files Browse the repository at this point in the history
  • Loading branch information
nameexhaustion committed Oct 21, 2024
1 parent f666b95 commit 75e9ad4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
20 changes: 7 additions & 13 deletions crates/polars-io/src/mmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,14 @@ impl std::ops::Deref for ReaderBytes<'_> {
}
}

/// Require 'static to force the caller to do any transmute as it's usually much
/// clearer to see there whether it's sound.
/// There are some places that perform manual lifetime management after transmuting `ReaderBytes`
/// to have a `'static` inner lifetime. The advantage to doing this is that it lets you construct a
/// `MemSlice` from the `ReaderBytes` in a zero-copy manner regardless of the underlying enum
/// variant.
impl ReaderBytes<'static> {
pub fn to_static_slice(&self) -> MemSlice {
// This function isn't marked as unsafe, since you can only call it after transmuting
// `ReaderBytes` to have a `'static` inner lifetime.
/// # Safety
/// `Self` outlives the returned `MemSlice` if this enum variant is an `Owned(Vec<u8>)`.
pub unsafe fn to_static_slice(&self) -> MemSlice {
match self {
ReaderBytes::Borrowed(v) => MemSlice::from_static(v),
ReaderBytes::Owned(v) => MemSlice::from_static(unsafe {
Expand All @@ -98,14 +100,6 @@ impl ReaderBytes<'static> {
},
}
}

pub fn into_mem_slice(self) -> MemSlice {
match self {
ReaderBytes::Borrowed(v) => MemSlice::from_static(v),
ReaderBytes::Owned(v) => MemSlice::from_vec(v),
ReaderBytes::Mapped(v, _) => MemSlice::from_mmap(Arc::new(v)),
}
}
}

impl<'a, T: 'a + MmapBytesReader> From<&'a mut T> for ReaderBytes<'a> {
Expand Down
11 changes: 6 additions & 5 deletions crates/polars-io/src/parquet/read/read_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -908,10 +908,9 @@ pub fn read_parquet<R: MmapBytesReader>(
}

let reader = ReaderBytes::from(&mut reader);
let store = mmap::ColumnStore::Local(
unsafe { std::mem::transmute::<ReaderBytes<'_>, ReaderBytes<'static>>(reader) }
.into_mem_slice(),
);
let store = mmap::ColumnStore::Local(unsafe {
std::mem::transmute::<ReaderBytes<'_>, ReaderBytes<'static>>(reader).to_static_slice()
});

let dfs = rg_to_dfs(
&store,
Expand Down Expand Up @@ -959,7 +958,9 @@ impl FetchRowGroupsFromMmapReader {

fn fetch_row_groups(&mut self, _row_groups: Range<usize>) -> PolarsResult<ColumnStore> {
// @TODO: we can something smarter here with mmap
Ok(mmap::ColumnStore::Local(self.0.to_static_slice()))
Ok(mmap::ColumnStore::Local(unsafe {
self.0.to_static_slice()
}))
}
}

Expand Down

0 comments on commit 75e9ad4

Please sign in to comment.