-
Notifications
You must be signed in to change notification settings - Fork 1.2k
perf(parquet): Vectorize dict-index bounds check in RleDecoder::get_batch_with_dict (up to -7.9%) #9746
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
Merged
Merged
perf(parquet): Vectorize dict-index bounds check in RleDecoder::get_batch_with_dict (up to -7.9%) #9746
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ab6e019
perf(parquet): vectorise dict-index bounds check in `RleDecoder::get_…
Dandandan 0ca4c50
perf(parquet): widen dict bounds check to 16-index chunks and cold panic
Dandandan abc64f7
fix(parquet): return error instead of panic on OOB dict index in `Rle…
Dandandan 6ea52b5
Fmt
Dandandan 963f2a2
style(parquet): drop extra blank line after clone()
Dandandan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -484,7 +484,16 @@ impl RleDecoder { | |
| if self.rle_left > 0 { | ||
| let num_values = cmp::min(max_values - values_read, self.rle_left as usize); | ||
| let dict_idx = self.current_value.unwrap() as usize; | ||
| let dict_value = dict[dict_idx].clone(); | ||
| let dict_value = dict | ||
| .get(dict_idx) | ||
| .ok_or_else(|| { | ||
| general_err!( | ||
| "dictionary index out of bounds: the len is {} but the index is {}", | ||
| dict.len(), | ||
| dict_idx | ||
| ) | ||
| })? | ||
| .clone(); | ||
|
|
||
| buffer[values_read..values_read + num_values].fill(dict_value); | ||
|
|
||
|
|
@@ -514,16 +523,30 @@ impl RleDecoder { | |
| break; | ||
| } | ||
| { | ||
| #[cold] | ||
| #[inline(never)] | ||
| fn oob(max_idx: u32, dict_len: usize) -> ParquetError { | ||
| general_err!( | ||
| "dictionary index out of bounds: the len is {} but the index is {}", | ||
| dict_len, | ||
| max_idx | ||
| ) | ||
| } | ||
| const CHUNK: usize = 16; | ||
| let out = &mut buffer[values_read..values_read + num_values]; | ||
| let idx = &index_buf[..num_values]; | ||
| let mut out_chunks = out.chunks_exact_mut(8); | ||
| let idx_chunks = idx.chunks_exact(8); | ||
| let dict_len = dict.len(); | ||
| let mut out_chunks = out.chunks_exact_mut(CHUNK); | ||
| let idx_chunks = idx.chunks_exact(CHUNK); | ||
| for (out_chunk, idx_chunk) in out_chunks.by_ref().zip(idx_chunks) { | ||
| let dict_len = dict.len(); | ||
| assert!( | ||
| idx_chunk.iter().all(|&i| (i as usize) < dict_len), | ||
| "dictionary index out of bounds" | ||
| ); | ||
| // u32 max-reduction instead of `.all(|&i| ..)`: `.all` | ||
|
Dandandan marked this conversation as resolved.
|
||
| // short-circuits and blocks autovectorisation. Negative | ||
| // i32 cast to u32 becomes a large value so the bounds | ||
| // check still rejects it. | ||
| let max_idx = idx_chunk.iter().fold(0u32, |acc, &i| acc.max(i as u32)); | ||
| if (max_idx as usize) >= dict_len { | ||
| return Err(oob(max_idx, dict_len)); | ||
| } | ||
| for (b, i) in out_chunk.iter_mut().zip(idx_chunk.iter()) { | ||
| // SAFETY: all indices checked above to be in bounds | ||
| b.clone_from(unsafe { dict.get_unchecked(*i as usize) }); | ||
|
|
@@ -532,9 +555,14 @@ impl RleDecoder { | |
| for (b, i) in out_chunks | ||
| .into_remainder() | ||
| .iter_mut() | ||
| .zip(idx.chunks_exact(8).remainder().iter()) | ||
| .zip(idx.chunks_exact(CHUNK).remainder().iter()) | ||
| { | ||
| b.clone_from(&dict[*i as usize]); | ||
| let dict_idx = *i as usize; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it is fascinaing that the rust compiler doens't already do this |
||
| if dict_idx >= dict_len { | ||
| return Err(oob(*i as u32, dict_len)); | ||
| } | ||
| // SAFETY: bounds checked above | ||
| b.clone_from(unsafe { dict.get_unchecked(dict_idx) }); | ||
| } | ||
| } | ||
| self.bit_packed_left -= num_values as u32; | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a nice change to return errors rather than panic on input