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

perf: Reduce memcopy in parquet #19350

Merged
merged 8 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
24 changes: 18 additions & 6 deletions crates/polars-io/src/mmap.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::fs::File;
use std::io::{BufReader, Cursor, Read, Seek};
use std::sync::Arc;

use polars_core::config::verbose;
use polars_utils::mmap::{MMapSemaphore, MemSlice};
Expand Down Expand Up @@ -82,14 +81,27 @@ 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 into_mem_slice(self) -> MemSlice {
/// Construct a `MemSlice` in a zero-copy manner from the underlying bytes, with the assumption
/// that the underlying bytes have a `'static` lifetime. This is marked as unsafe despite having
/// a `'static` inner lifetime, as the `Owned(Vec<u8>)` variant is not covered by the lifetime
/// guarantee.
///
/// # Safety
/// `Self` outlives the returned `MemSlice` if this enum variant is an `Owned(Vec<u8>)`.
pub unsafe fn to_static_slice(&self) -> MemSlice {
nameexhaustion marked this conversation as resolved.
Show resolved Hide resolved
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)),
ReaderBytes::Owned(v) => MemSlice::from_static(unsafe {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't safe if the self is dropped before the MemSlice.

I see that MemSlice has an inner that can have a bytes::Bytes variant. If we have an ReaderBytes::Owned we can move that vec into the MemSlice.

I think we should also change the type form Vec<u8> to Bytes so we can freely clone it.

std::mem::transmute::<&[u8], &'static [u8]>(v.as_slice())
}),
ReaderBytes::Mapped(v, _) => unsafe {
MemSlice::from_static(std::mem::transmute::<&[u8], &'static [u8]>(v.as_ref()))
},
}
}
}
Expand Down
14 changes: 6 additions & 8 deletions crates/polars-io/src/parquet/read/read_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use polars_parquet::parquet::statistics::Statistics;
use polars_parquet::read::{
self, ColumnChunkMetadata, FileMetadata, Filter, PhysicalType, RowGroupMetadata,
};
use polars_utils::mmap::MemSlice;
use rayon::prelude::*;

#[cfg(feature = "cloud")]
Expand Down Expand Up @@ -908,10 +907,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,9 +957,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(MemSlice::from_vec(
self.0.deref().to_vec(),
)))
Ok(mmap::ColumnStore::Local(unsafe {
self.0.to_static_slice()
}))
}
}

Expand Down
Loading