-
Notifications
You must be signed in to change notification settings - Fork 1.9k
perf: optimise right for byte access and StringView #20069
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
base: main
Are you sure you want to change the base?
Conversation
|
cc @Jefffrey |
|
|
||
| /// Calculate the byte length of the substring of last `n` chars from string `string` | ||
| /// (or all but first `|n|` chars if n is negative) | ||
| fn right_byte_length(string: &str, n: i64) -> usize { |
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.
I haven't looked too closely, but I feel we can deduplicate right + left implementation code as the main difference is this byte length function? In that it flips which side it looks from?
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.
Unfortunately, it's quite different.
-
left_byte_lengthandright_byte_lengthare almost symmetric with flipping the sign of thenargument, except for the0case. -
The side of lookup in the byte array - from the left or from the middle.
-
The string view is built differently (length adjustment vs. offset).
So, having the generic implementation wouldn't be that helpful - plenty of ifs all around.
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.
I do think we could deduplicate somewhat, and perhaps having this function return a Range instead of just a usize might make it more feasible; but can explore this in a followup 👍
| }, | ||
| (Some(string), Some(n)) => { | ||
| let byte_length = right_byte_length(string, n); | ||
| // println!( |
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.
Commented code accidentally added here
| args: args.clone(), | ||
| arg_fields: arg_fields.clone(), | ||
| number_rows: size, | ||
| return_field: Field::new("f", DataType::Utf8, true) |
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.
Shouldn't this use Utf8View when is_string_view == true ?
| Ordering::Equal => string.len(), | ||
| Ordering::Greater => string | ||
| .char_indices() | ||
| .nth_back(n.unsigned_abs() as usize - 1) |
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.
nit: This may truncate on 32-bit machines
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.
For string arrays, we support 64-bit offsets on 32-bit platforms, but the string will be limited in 32-bit size. I added saturation, so left would return the whole string anyway.
For string views, Arrow provides 32-bit views only. Since we cannot construct a large string view as input, it won't be a problem.
|
What do you think of reworking both left and right benches to a single file like for trim.rs? |
Jefffrey
left a comment
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.
The use of make_view() here made me realize we could use it for left as well 🤔
For example replace this section:
datafusion/datafusion/functions/src/unicode/left.rs
Lines 196 to 200 in 3797995
| // Input string comes from StringViewArray, so it should fit in 32-bit length | |
| let new_length: u32 = left_byte_length(string, n) as u32; | |
| let byte_view = ByteView::from(view); | |
| // Construct a new view | |
| shrink_string_view_array_view(string, new_length, byte_view) |
With this:
let new_length = left_byte_length(string, n);
let bytes = &string.as_bytes()[..new_length];
let byte_view = ByteView::from(view);
make_view(bytes, byte_view.buffer_index, byte_view.offset)Though we can explore this in a followup
What do you think of reworking both left and right benches to a single file like for trim.rs?
I think that would be a good idea.
| if result_bytes.len() > 12 { | ||
| let byte_view = ByteView::from(view); | ||
| // Reuse buffer, but adjust offset and length | ||
| make_view( | ||
| result_bytes, | ||
| byte_view.buffer_index, | ||
| byte_view.offset + new_offset as u32, | ||
| ) | ||
| } else { | ||
| // inline value does not need block id or offset | ||
| make_view(result_bytes, 0, 0) | ||
| } |
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.
| if result_bytes.len() > 12 { | |
| let byte_view = ByteView::from(view); | |
| // Reuse buffer, but adjust offset and length | |
| make_view( | |
| result_bytes, | |
| byte_view.buffer_index, | |
| byte_view.offset + new_offset as u32, | |
| ) | |
| } else { | |
| // inline value does not need block id or offset | |
| make_view(result_bytes, 0, 0) | |
| } | |
| let byte_view = ByteView::from(view); | |
| make_view( | |
| result_bytes, | |
| byte_view.buffer_index, | |
| byte_view.offset + new_offset as u32, | |
| ) |
We could probably avoid this outside if check since make_view already checks this for us
|
|
||
| /// Calculate the byte length of the substring of last `n` chars from string `string` | ||
| /// (or all but first `|n|` chars if n is negative) | ||
| fn right_byte_length(string: &str, n: i64) -> usize { |
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.
I do think we could deduplicate somewhat, and perhaps having this function return a Range instead of just a usize might make it more feasible; but can explore this in a followup 👍
Which issue does this PR close?
rightfor byte access and StringView #20068.Rationale for this change
Similar to issue #19749 and the optimisation of
leftin #19980, it's worth doing the same forrightWhat changes are included in this PR?
Improve efficiency of the function by making fewer memory allocations and going directly to bytes, based on char boundaries
Provide a specialisation for StringView with buffer zero-copy
Use
arrow_array::buffer::make_viewfor low-level view manipulation (we still need to know about a magic constant 12 for a buffer layout)Benchmark - up to 90% performance improvement
Are these changes tested?
Existing unit tests for
rightAdded more unit tests
Added bench similar to
right.rsExisting SLTs pass
Are there any user-facing changes?
No