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

Improve validate_utf8 performance #2048

Merged
merged 6 commits into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions arrow/benches/array_data_validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,22 @@ fn create_binary_array_data(length: i32) -> ArrayData {
.unwrap()
}

fn array_slice_benchmark(c: &mut Criterion) {
fn validate_utf8_array(arr: &StringArray) {
arr.data().validate_values().unwrap();
}

fn validate_benchmark(c: &mut Criterion) {
//Binary Array
c.bench_function("validate_binary_array_data 20000", |b| {
b.iter(|| create_binary_array_data(20000))
});

//Utf8 Array
let str_arr = StringArray::from(vec!["test"; 20000]);
c.bench_function("validate_utf8_array_data 20000", |b| {
b.iter(|| validate_utf8_array(&str_arr))
});
}

criterion_group!(benches, array_slice_benchmark);
criterion_group!(benches, validate_benchmark);
criterion_main!(benches);
41 changes: 31 additions & 10 deletions arrow/src/array/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1125,16 +1125,37 @@ impl ArrayData {
T: ArrowNativeType + TryInto<usize> + num::Num + std::fmt::Display,
{
let values_buffer = &self.buffers[1].as_slice();

self.validate_each_offset::<T, _>(values_buffer.len(), |string_index, range| {
std::str::from_utf8(&values_buffer[range.clone()]).map_err(|e| {
ArrowError::InvalidArgumentError(format!(
"Invalid UTF8 sequence at string index {} ({:?}): {}",
string_index, range, e
))
})?;
Ok(())
})
if let Ok(values_str) = std::str::from_utf8(values_buffer) {
// Validate Offsets are correct
self.validate_each_offset::<T, _>(
values_buffer.len(),
|string_index, range| {
if !values_str.is_char_boundary(range.start)
|| !values_str.is_char_boundary(range.end)
Comment on lines +1133 to +1134
Copy link
Contributor

@tustvold tustvold Jul 23, 2022

Choose a reason for hiding this comment

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

I think you can remove values_str.is_char_boundary(range.end), as the end offset is the start offset of the next string slice and will therefore be checked by that. We do not need to check the final offset as if the end of the string is not a valid char boundary, the string as a whole would fail validation.

Copy link
Contributor

Choose a reason for hiding this comment

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

I realized an additional subtlety with this, if the offsets buffer is sliced, you need to validate that this slicing is at a valid boundary, which a naive implementation of the above might miss.

Theoretically you only need to validate the range of values covered by the offsets, which might be another possible optimisation 🤔

{
return Err(ArrowError::InvalidArgumentError(format!(
"incomplete utf-8 byte sequence from index {}",
string_index
)));
}
Ok(())
},
)
} else {
// find specific offset that failed utf8 validation
self.validate_each_offset::<T, _>(
values_buffer.len(),
|string_index, range| {
std::str::from_utf8(&values_buffer[range.clone()]).map_err(|e| {
ArrowError::InvalidArgumentError(format!(
"Invalid UTF8 sequence at string index {} ({:?}): {}",
string_index, range, e
))
})?;
Ok(())
},
)
}
}

/// Ensures that all offsets in `buffers[0]` into `buffers[1]` are
Expand Down