Skip to content

Commit

Permalink
perf: Use branchless uleb128 decoding for parquet
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed May 21, 2024
1 parent 1cdab6f commit e3f5dc3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
30 changes: 30 additions & 0 deletions crates/polars-parquet/src/parquet/encoding/uleb128.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,34 @@
// Reads an uleb128 encoded integer with at most 56 bits (8 bytes with 7 bits worth of payload each).
/// Returns the integer and the number of bytes that made up this integer.
/// If the returned length is bigger than 8 this means the integer required more than 8 bytes and the remaining bytes need to be read sequentially and combined with the return value.
/// Safety: `data` needs to contain at least 8 bytes.
#[target_feature(enable = "bmi2")]
#[cfg(target_feature = "bmi2")]
pub unsafe fn decode_uleb_bmi2(data: &[u8]) -> (u64, usize) {
const CONT_MARKER: u64 = 0x80808080_80808080;
debug_assert!(data.len() >= 8);

unsafe {
let word = data.as_ptr().cast::<u64>().read_unaligned();
// mask indicating continuation bytes
let mask = std::arch::x86_64::_pext_u64(word, CONT_MARKER);
let len = (!mask).trailing_zeros() + 1;
// which payload bits to extract
let ext = std::arch::x86_64::_bzhi_u64(!CONT_MARKER, 8 * len);
let payload = std::arch::x86_64::_pext_u64(word, ext);

(payload, len as _)
}
}

pub fn decode(values: &[u8]) -> (u64, usize) {
#[cfg(target_feature = "bmi2")]
{
if polars_utils::cpuid::has_fast_bmi2() && values.len() >= 8 {
return unsafe { decode_uleb_bmi2(values) };
}
}

let mut result = 0;
let mut shift = 0;

Expand Down
2 changes: 1 addition & 1 deletion crates/polars-utils/src/cpuid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn detect_fast_bmi2() -> bool {
}
}

#[inline]
#[inline(always)]
pub fn has_fast_bmi2() -> bool {
#[cfg(target_feature = "bmi2")]
{
Expand Down

0 comments on commit e3f5dc3

Please sign in to comment.