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: bitpacking exclude sign bit if not needed #2696

Merged
merged 3 commits into from
Aug 8, 2024

Conversation

albertlockett
Copy link
Contributor

If bitpacking a signed type (e.g. i32), if all the values to be encoded are positive then there's no need to include the sign bit.

@github-actions github-actions bot added the bug Something isn't working label Aug 6, 2024
@codecov-commenter
Copy link

codecov-commenter commented Aug 6, 2024

Codecov Report

Attention: Patch coverage is 98.14815% with 1 line in your changes missing coverage. Please review.

Project coverage is 79.41%. Comparing base (d1f7d3d) to head (3751059).
Report is 2 commits behind head on main.

Files Patch % Lines
...t/lance-encoding/src/encodings/physical/bitpack.rs 97.87% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2696      +/-   ##
==========================================
- Coverage   79.52%   79.41%   -0.12%     
==========================================
  Files         227      227              
  Lines       66754    66880     +126     
  Branches    66754    66880     +126     
==========================================
+ Hits        53089    53112      +23     
- Misses      10538    10637      +99     
- Partials     3127     3131       +4     
Flag Coverage Δ
unittests 79.41% <98.14%> (-0.12%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

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

Copy link
Contributor

@wjones127 wjones127 left a comment

Choose a reason for hiding this comment

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

Should this be a fix (bugfix) or perf: (performance improvement)?

@albertlockett albertlockett changed the title fix: bitpacking exclude sign bit if not needed perf: bitpacking exclude sign bit if not needed Aug 8, 2024
Copy link
Contributor

@westonpace westonpace left a comment

Choose a reason for hiding this comment

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

If I wanted to be super annoying (which I usually do) I would point out that you also don't need the signed bit if all values are negative 😆 (but you would need to change BitpackParams::signed into enum Signedness { AllNegative, Mixed, AllPositive }.

Please feel free to defer to follow-up or even "good first issue" for someone else in the future.

Comment on lines 103 to 110
return min_leading_bits
// +1 added here for the sign bit
.map(|leading_bits| arr.data_type().byte_width() as u64 * 8 - leading_bits + 1);
.map(|leading_bits| arr.data_type().byte_width() as u64 * 8 - leading_bits)
.map(|bits| bits + if add_signed_bit { 1 } else { 0 })
.map(|bits| bits.max(1)) // cannot bitpack into < 1 bit
.map(|bits| BitpackParams {
num_bits: bits,
signed: add_signed_bit,
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Very minor nit: All this mapping is fine but you could avoid it and do something like...

let mut min_leading_bits = arr.data_type().byte_width() as u64 * 8 - min_leading_bits?;
if add_signed_bit {
  // Need extra sign bit
  min_leading_bits += 1;
} 
// cannot bitpack into <1 bit
let num_bits = min_leading_bits.max(1);
Some(BitpackParams {
  num_bits,
  signed: add_signed_bit
})

This might be slightly more readable.

@albertlockett albertlockett merged commit b9990d9 into main Aug 8, 2024
25 checks passed
@albertlockett albertlockett deleted the albertlockett/optimize_signed_bitpack branch August 8, 2024 18:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants