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: Branchless Parquet Prefiltering #19190

Open
wants to merge 32 commits into
base: main
Choose a base branch
from

Conversation

coastalwhite
Copy link
Collaborator

@coastalwhite coastalwhite commented Oct 11, 2024

Change the Parquet decoders to use branchless filtering for the prefiltered methods.

This rewrites most of the decoders.

@github-actions github-actions bot added performance Performance issues or improvements python Related to Python Polars rust Related to Rust Polars labels Oct 11, 2024
i64: num_traits::AsPrimitive<P>,
D: DecoderFunction<P, T>,
{
let mut num_read = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this loop can be written more efficiently.

let num_valid = validity.count_ones();
let mut num_written = 0;

while filter != 0 {
    let offset = filter.trailing_zeros();
    validity >>= offset;
    
    let idx = num_valid - validity.count_ones();
    let v = if validity & 1 != 0 {
        dfn.decode(unsafe { values.get_unchecked(idx) })
    } else {
        T::zeroed()
    };

    *out.add(num_written) = v;

    num_written += 1;
    filter >>= offset + 1;
    validity >>= 1;
}

(num_valid, num_written)

Copy link
Collaborator

@orlp orlp Oct 11, 2024

Choose a reason for hiding this comment

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

Even better I believe:

let num_available = validity.count_ones();
let mut num_written = 0;

while filter != 0 {
    let offset = filter.trailing_zeros();
    let idx_to_read = num_available - (validity >> offset).count_ones();
    let v = if validity & 1 != 0 {
        dfn.decode(unsafe { values.get_unchecked(idx_to_read) })
    } else {
        T::zeroed()
    };

    *out.add(num_written) = v;
    num_written += 1;
    filter &= filter - 1; // Clear least significant bit.
}

(num_available, num_written)


let mut v = v;
let mut f = f;
let mut num_read = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Idem.

// @NOTE: We have to cast to u64 here to avoid the `shr` overflow on the filter.
let mut f = f as u64;
let mut v = v as u64;
let mut num_read = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Idem.

@coastalwhite coastalwhite force-pushed the perf/branchless-pq-prefiltering branch 2 times, most recently from 678577f to 45a3078 Compare October 18, 2024 13:51
@coastalwhite coastalwhite force-pushed the perf/branchless-pq-prefiltering branch from 290d953 to 36b8d51 Compare October 24, 2024 09:32
Copy link

codecov bot commented Oct 24, 2024

Codecov Report

Attention: Patch coverage is 79.79107% with 561 lines in your changes missing coverage. Please review.

Project coverage is 80.16%. Comparing base (69c9c3a) to head (e395c93).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...et/src/arrow/read/deserialize/fixed_size_binary.rs 69.86% 91 Missing ⚠️
...lars-parquet/src/arrow/read/deserialize/boolean.rs 62.50% 87 Missing ⚠️
...quet/src/arrow/read/deserialize/primitive/plain.rs 76.47% 80 Missing ⚠️
...lars-parquet/src/arrow/read/deserialize/binview.rs 79.58% 78 Missing ⚠️
...t/src/arrow/read/deserialize/utils/dict_encoded.rs 90.35% 68 Missing ⚠️
...rs-parquet/src/arrow/read/deserialize/utils/mod.rs 68.15% 50 Missing ⚠️
crates/polars-arrow/src/types/aligned_bytes.rs 0.00% 28 Missing ⚠️
...parquet/src/arrow/read/deserialize/nested_utils.rs 90.30% 22 Missing ⚠️
crates/polars-arrow/src/bitmap/bitmask.rs 46.15% 14 Missing ⚠️
...et/src/arrow/read/deserialize/primitive/integer.rs 82.89% 13 Missing ⚠️
... and 7 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #19190      +/-   ##
==========================================
+ Coverage   80.03%   80.16%   +0.12%     
==========================================
  Files        1532     1531       -1     
  Lines      210749   210193     -556     
  Branches     2442     2442              
==========================================
- Hits       168676   168494     -182     
+ Misses      41518    41144     -374     
  Partials      555      555              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Performance issues or improvements python Related to Python Polars rust Related to Rust Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants