You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Parquet LevelEncoder is much too eager to write short rle runs #7739
Is your feature request related to a problem or challenge? Please describe what you are trying to do.
While looking into parquet loading performance, I noticed that the definition levels for simple nullable columns often contain very short rle runs. These seem to mess with branch prediction when decoding and decrease performance.
I managed to reproduce this in a benchmark by creating a custom RleEncoder for a bitwidth of 1, which only writes bitpacked runs. With 90% of bits being set, these are the results:
Note the decoder in this benchmark is the same for both cases, the only difference is how the levels are serialized.
I also hacked some statistics into the decoder. For the above benchmark these are the distributions of rle vs bitpacked encoded runs. On average, the rle runs are only 16 elements long, and the bitpacked only encoding even seems to be shorter:
I'd like the RleEncoder to be smarter about switching between bitpacked and rle runs. Maybe this is a one-line change in encodings/rle.rs, but I find the logic there a bit hard to follow. Alternatives could be a specialized implementation for single-bit levels, or making this configurable for the user.
For integer columns, Parquet first dictionary encodes and then
applies a hybrid of RLE and Bitpacking to the dictionary codes.
If the same value repeats ≥ 8 times consecutively, it uses RLE;
otherwise, it uses bitpacking. Interestingly, we found that the RLE-
threshold 8 is a non-configurable parameter hard-coded in every
implementation of Parquet. Although it saves Parquet a tuning
knob, such inflexibility could lead to suboptimal compression ratios
for specific data sets (e.g., when the common repetition length is 7).
so this magic number of 8 seems to be a convention and not part of the spec. means we can definitely tune it, but needs a interface to expose this configuration
Reacted by Andrew Lamb, Jörn Horstmann and Jigao Luo
so this magic number of 8 seems to be a convention and not part of the spec. means we can definitely tune it, but needs a interface to expose this configuration
When would it be bad to use the maximum RLE value rather than bit packing?
Like for example, if we could encode 1000 values into a single RLE tuple, wouldn't that always be better than trying to bit pack the 1000 values 🤔
When would it be bad to use the maximum RLE value rather than bit packing?
by default it will use the maximum RLE value.
The choice here is, for example, the encoder see only 7 consecutive same value, should it use RLE or bit packing?
The back of envelope calculation goes:
RLE needs 1 byte header and 1 byte actual value, so every RLE needs 2 bytes.
Bit packing needs 1 byte header and n * bit_width/8 bytes.
If bit_width=1, then any sequence longer than 8 should use RLE.
if bit_width=2, then anything longer than 4 should use RLE.
But we should consider decoding speed as well. unpack bit-packed data is slow when bit-pack sequence is short. So if we want faster decoding, we should make RLE less likely, i.e., make n larger.
Reacted by Jörn Horstmann, Jigao Luo and Andrew Lamb
Thanks for having me, and I really appreciate this insight from @XiangpengHao .
But we should consider decoding speed as well. unpack bit-packed data is slow when bit-pack sequence is short. So if we want faster decoding, we should make RLE less likely, i.e., make n larger.
This insight point basically highlights the core dilemma we face with CPU-parquet-reader: the most highly compressed Parquet files are often not the fastest to read (to decode and decompress). We’ve even seen cases where this tradeoff makes uncompressed Parquet -- despite its larger size -- faster for end-to-end reading.
I am always wishing for writing Parquet files optimized for read performance. However, identifying such low-level pitfalls is challenging, so thank you for shedding light on this!
But we should consider decoding speed as well. unpack bit-packed data is slow when bit-pack sequence is short. So if we want faster decoding, we should make RLE less likely, i.e., make n larger.
BTW I wonder if this calculation will change if we integrate native support for reading RLE encoded Arrays (e.g. an arrow RunEndArray 🤔 )
Slight addition to that calculation, if there is already a bitpacked run that can be continued, then adding 8 bits consumes only a single byte as bitpacked, but 2 bytes as rle. Adding 16 bits would consume 2 bytes independent of encoding. So for a bit width of 1, I think increasing the minimum rle length to 16 could be beneficial. Unfortunately I was not yet able to put that logic into code without breaking any tests 😅
so this magic number of 8 seems to be a convention and not part of the spec. means we can definitely tune it, but needs a interface to expose this configuration
Coming late to this, but the reason for 8 does come from the spec. Specifically
bit-packed-run := <bit-packed-header> <bit-packed-values>
bit-packed-header := varint-encode(<bit-pack-scaled-run-len> << 1 | 1)
// we always bit-pack a multiple of 8 values at a time, so we only store the number of values / 8
bit-pack-scaled-run-len := (bit-packed-run-len) / 8
This means you cannot have a bit-packed run of fewer than 8 elements.
Edit:
NVM...still not enough caffeine in me...the quoted comment relates to RLE runs, not bit-packed 🤦
Activity
jhorstmann commentedon Jun 24, 2025
This idea is related to the parquet tuning topic, so @alamb, @XiangpengHao and @JigaoLuo might be interested
alamb commentedon Jun 24, 2025
This sounds pretty amazing to me @jhorstmann
I don't know of any changes to that RLE/bit packing logic -- and in fact git blame shows it came in via the initial implementation from @sunchao 7+ years ago: https://github.com/apache/arrow-rs/blame/df702cfc714dd25060f6a493570221df18b2c598/parquet/src/encodings/rle.rs#L258
Adjusting the heuristics / optimizing it sounds like a great idea
XiangpengHao commentedon Jun 24, 2025
This is super cool @jhorstmann
As a parallel work, I'm optimizing decoding bit-packed data, and implemented the algorithm in this paper: https://www.microsoft.com/en-us/research/publication/selection-pushdown-in-column-stores-using-bit-manipulation-instructions/
here: https://github.com/XiangpengHao/bmi-select
I'll think about whether/how to integrate it into our decoder
XiangpengHao commentedon Jun 24, 2025
According to this paper by @XinyuZeng :
so this magic number of 8 seems to be a convention and not part of the spec. means we can definitely tune it, but needs a interface to expose this configuration
alamb commentedon Jun 24, 2025
When would it be bad to use the maximum RLE value rather than bit packing?
Like for example, if we could encode 1000 values into a single RLE tuple, wouldn't that always be better than trying to bit pack the 1000 values 🤔
XiangpengHao commentedon Jun 25, 2025
by default it will use the maximum RLE value.
The choice here is, for example, the encoder see only 7 consecutive same value, should it use RLE or bit packing?
The back of envelope calculation goes:
If bit_width=1, then any sequence longer than 8 should use RLE.
if bit_width=2, then anything longer than 4 should use RLE.
But we should consider decoding speed as well. unpack bit-packed data is slow when bit-pack sequence is short. So if we want faster decoding, we should make RLE less likely, i.e., make n larger.
JigaoLuo commentedon Jun 25, 2025
Thanks for having me, and I really appreciate this insight from @XiangpengHao .
This insight point basically highlights the core dilemma we face with CPU-parquet-reader: the most highly compressed Parquet files are often not the fastest to read (to decode and decompress). We’ve even seen cases where this tradeoff makes uncompressed Parquet -- despite its larger size -- faster for end-to-end reading.
I am always wishing for writing Parquet files optimized for read performance. However, identifying such low-level pitfalls is challenging, so thank you for shedding light on this!
alamb commentedon Jun 25, 2025
BTW I wonder if this calculation will change if we integrate native support for reading RLE encoded Arrays (e.g. an arrow RunEndArray 🤔 )
jhorstmann commentedon Jun 25, 2025
Slight addition to that calculation, if there is already a bitpacked run that can be continued, then adding 8 bits consumes only a single byte as bitpacked, but 2 bytes as rle. Adding 16 bits would consume 2 bytes independent of encoding. So for a bit width of 1, I think increasing the minimum rle length to 16 could be beneficial. Unfortunately I was not yet able to put that logic into code without breaking any tests 😅
etseidl commentedon Apr 2, 2026
Coming late to this, but the reason for 8 does come from the spec. Specifically
This means you cannot have a bit-packed run of fewer than 8 elements.
Edit:
NVM...still not enough caffeine in me...the quoted comment relates to RLE runs, not bit-packed 🤦