You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "XiangpengHao (via GitHub)" <gi...@apache.org> on 2024/07/05 15:47:36 UTC
[PR] Fast utf8 validation when loading string view from parquet [arrow-rs]
XiangpengHao opened a new pull request, #6009:
URL: https://github.com/apache/arrow-rs/pull/6009
# Which issue does this PR close?
<!--
We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
-->
Closes #5995.
# Rationale for this change
Current utf-8 validation for string view is super slow, even slower than reading StringArray from parquet, which copies & consolidates the strings to a new buffer.
It does not have to be slow, but making it fast requires a bit of art. I tried many approaches, and this PR is the most simple and easy to understand one.
Please check the comments to see if they make sense (and helps understanding what's going on).
To run the benchmark:
```bash
cargo bench --bench arrow_reader --features="arrow test_common experimental" "arrow_array_reader/String.*Array/plain"
```
We will get this:
```
arrow_array_reader/StringViewArray/plain encoded, mandatory, no NULLs
time: [287.81 µs 288.21 µs 288.66 µs]
change: [-84.308% -84.292% -84.273%] (p = 0.00 < 0.05)
Performance has improved.
arrow_array_reader/StringViewArray/plain encoded, optional, no NULLs
time: [289.25 µs 289.60 µs 289.95 µs]
change: [-84.303% -84.286% -84.268%] (p = 0.00 < 0.05)
Performance has improved.
arrow_array_reader/StringViewArray/plain encoded, optional, half NULLs
time: [207.22 µs 207.39 µs 207.54 µs]
change: [-78.958% -78.940% -78.924%] (p = 0.00 < 0.05)
Performance has improved.
```
Not only does loading StringViewArray 5x faster than the previous implementation, but also almost 2x faster than loading StringArray:
```
arrow_array_reader/StringArray/plain encoded, mandatory, no NULLs
time: [345.03 µs 345.54 µs 346.20 µs]
arrow_array_reader/StringArray/plain encoded, optional, no NULLs
time: [366.46 µs 368.51 µs 370.30 µs]
arrow_array_reader/StringArray/plain encoded, optional, half NULLs
time: [427.10 µs 427.21 µs 427.32 µs]
```
<!--
Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.
-->
# What changes are included in this PR?
The code change is quite small, just some small tweaks to adjust the timing of utf-8 validation.
<!--
There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
-->
# Are there any user-facing changes?
<!--
If there are user-facing changes then we may require documentation to be updated before approving the PR.
-->
<!---
If there are any breaking changes to public APIs, please add the `breaking change` label.
-->
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fast utf8 validation when loading string view from parquet [arrow-rs]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #6009:
URL: https://github.com/apache/arrow-rs/pull/6009#issuecomment-2211769211
> It isn't sufficient to just check that the string buffer is valid UTF-8, you must also validate that the offsets don't split a UTF-8 codepoint. Now it might be that the length checking logic already does this in effect, as a UTF-8 continuation would be > 128, but at the very least we should justify this aspect
I was thinking about constructing an invalid case
Since the lengths are little endian it would be possible for an unterminated UTF8 sequence to be followed by a length byte which in theory could complete the utf8 sequence
However, as @tustvold points out, all multi-byte utf-8 values have a `1` in their top bit (and thus `u128` representation is greater than 128) so in this case thelength byte can not be mis-interpreted as a part of the multi-byte sequence
I would be happy to make some ascii art diagrams if that would help
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fast utf8 validation when loading string view from parquet [arrow-rs]
Posted by "XiangpengHao (via GitHub)" <gi...@apache.org>.
XiangpengHao commented on code in PR #6009:
URL: https://github.com/apache/arrow-rs/pull/6009#discussion_r1667088615
##########
parquet/src/arrow/array_reader/byte_view_array.rs:
##########
@@ -332,7 +334,34 @@ impl ByteViewArrayDecoderPlain {
}
if self.validate_utf8 {
- check_valid_utf8(unsafe { buf.get_unchecked(start_offset..end_offset) })?;
+ // It seems you are trying to understand what's going on here, take a breath and be patient.
+ // Utf-8 validation is a non-trivial task, here are some background facts:
+ // (1) Validating one 100 bytes of utf-8 is much faster than validating ten 10 bytes of utf-8.
+ // Potentially because of the auto SIMD by compiler, someone please confirm this :)
+ // (2) Practical strings are short, 99% of strings are smaller than 100 bytes, as shown in paper:
+ // https://www.vldb.org/pvldb/vol17/p148-zeng.pdf, Figure 5f.
+ // (3) Parquet plain encoding makes utf-8 validation harder,
+ // because it stores the length of each string right before the string.
+ // This means naive utf-8 validation will be slow, because the validation need to skip the length bytes.
+ // I.e., the validation cannot validate the buffer in one pass, but instead, validate strings chunk by chunk.
+ //
+ // Given the above observations, the goal is to do batch validation as much as possible.
+ // The key idea is that if the length is smaller than 128 (99% of the case), then the length bytes are valid utf-8 (bc they are ASCII).
+ //
+ // The implementation keeps a water mark `utf8_validation_begin` to track the beginning of the buffer that is not validated.
+ // If the length is smaller than 128, then we continue to next string.
+ // If the length is larger than 128, then we validate the buffer before the length bytes, and move the water mark to the beginning of next string.
+ if len < 128 {
+ // fast path, move to next string.
+ // the len bytes are valid utf8.
+ } else {
+ // unfortunately, the len bytes may not be valid utf8, we need to wrap up and validate everything before it.
+ check_valid_utf8(unsafe {
+ buf.get_unchecked(utf8_validation_begin..self.offset)
+ })?;
+ // move the cursor to skip the len bytes.
+ utf8_validation_begin = start_offset;
Review Comment:
Here we delay the current string to the next validation, so it points to the starting of the current string.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fast utf8 validation when loading string view from parquet [arrow-rs]
Posted by "XiangpengHao (via GitHub)" <gi...@apache.org>.
XiangpengHao commented on code in PR #6009:
URL: https://github.com/apache/arrow-rs/pull/6009#discussion_r1666964734
##########
parquet/src/arrow/array_reader/byte_view_array.rs:
##########
@@ -332,7 +334,34 @@ impl ByteViewArrayDecoderPlain {
}
if self.validate_utf8 {
- check_valid_utf8(unsafe { buf.get_unchecked(start_offset..end_offset) })?;
+ // It seems you are trying to understand what's going on here, take a breath and be patient.
+ // Utf-8 validation is a non-trivial task, here are some background facts:
+ // (1) Validating one 100 bytes of utf-8 is much faster than validating ten 10 bytes of utf-8.
+ // Potentially because of the auto SIMD by compiler, someone please confirm this :)
+ // (2) Practical strings are short, 99% of strings are smaller than 100 bytes, as shown in paper:
+ // https://www.vldb.org/pvldb/vol17/p148-zeng.pdf, Figure 5f.
+ // (3) Parquet plain encoding makes utf-8 validation harder,
+ // because it stores the length of each string right before the string.
+ // This means naive utf-8 validation will be slow, because the validation need to skip the length bytes.
+ // I.e., the validation cannot validate the buffer in one pass, but instead, validate strings chunk by chunk.
+ //
+ // Given the above observations, the goal is to do batch validation as much as possible.
+ // The key idea is that if the length is smaller than 128 (99% of the case), then the length bytes are valid utf-8 (bc they are ASCII).
+ //
+ // The implementation keeps a water mark `utf8_validation_begin` to track the beginning of the buffer that is not validated.
+ // If the length is smaller than 128, then we continue to next string.
+ // If the length is larger than 128, then we validate the buffer before the length bytes, and move the water mark to the beginning of next string.
+ if len < 128 {
Review Comment:
I agree it's a bit awkward... I just want to place some comments under `if len < 128`.
Do you think it will look better to just have `if len >= 128`?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fast utf8 validation when loading string view from parquet [arrow-rs]
Posted by "XiangpengHao (via GitHub)" <gi...@apache.org>.
XiangpengHao commented on PR #6009:
URL: https://github.com/apache/arrow-rs/pull/6009#issuecomment-2211111424
I believe the CI failure is not from us...
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fast utf8 validation when loading string view from parquet [arrow-rs]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #6009:
URL: https://github.com/apache/arrow-rs/pull/6009#discussion_r1667365513
##########
parquet/src/arrow/array_reader/byte_view_array.rs:
##########
@@ -332,7 +334,38 @@ impl ByteViewArrayDecoderPlain {
}
if self.validate_utf8 {
- check_valid_utf8(unsafe { buf.get_unchecked(start_offset..end_offset) })?;
+ // It seems you are trying to understand what's going on here, take a breath and be patient.
+ // Utf-8 validation is a non-trivial task, here are some background facts:
+ // (1) Validating one 2048-byte string is much faster than validating 128 of 16-byte string.
+ // As shown in https://github.com/apache/arrow-rs/pull/6009#issuecomment-2211174229
+ // Potentially because the SIMD operations favor longer strings.
+ // (2) Practical strings are short, 99% of strings are smaller than 100 bytes, as shown in paper:
+ // https://www.vldb.org/pvldb/vol17/p148-zeng.pdf, Figure 5f.
+ // (3) Parquet plain encoding makes utf-8 validation harder,
+ // because it stores the length of each string right before the string.
+ // This means naive utf-8 validation will be slow, because the validation need to skip the length bytes.
+ // I.e., the validation cannot validate the buffer in one pass, but instead, validate strings chunk by chunk.
+ //
+ // Given the above observations, the goal is to do batch validation as much as possible.
+ // The key idea is that if the length is smaller than 128 (99% of the case), then the length bytes are valid utf-8, as reasoned blow:
Review Comment:
```suggestion
// The key idea is that if the length is smaller than 128 (99% of the case), then the length bytes are valid utf-8, as reasoned below:
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fast utf8 validation when loading string view from parquet [arrow-rs]
Posted by "XiangpengHao (via GitHub)" <gi...@apache.org>.
XiangpengHao commented on PR #6009:
URL: https://github.com/apache/arrow-rs/pull/6009#issuecomment-2211174229
I run a tiny benchmark to show the speed of utf-8 validation versus the string length. With the same total amount of bytes in the buffer, bigger chunk size -> faster performance. I hope this can back the hypotheses of this PR. I'll probably write a small blog for a more detailed analysis.
Code:
```rust
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
fn make_string(n: usize) -> Vec<u8> {
let mut s = Vec::with_capacity(n);
for _ in 0..n {
s.push('A' as u8);
}
s
}
fn validate_utf8(c: &mut Criterion) {
let s = make_string(2048);
for chunk in [8, 16, 32, 64, 128, 256, 512, 1024, 2048] {
c.bench_with_input(
BenchmarkId::new("validate 2048 byte buffer", chunk),
&chunk,
|b, c| {
b.iter(|| {
for c in s.chunks(*c) {
black_box(std::str::from_utf8(c).unwrap());
}
})
},
);
}
}
criterion_group!(benches, validate_utf8);
criterion_main!(benches);
```
Result:
<img width="601" alt="image" src="https://github.com/apache/arrow-rs/assets/6504314/6643a588-9355-4877-ab0d-68b04ef7343b">
[ChatGPT link](https://chatgpt.com/share/2cf6b622-7194-4703-840b-f4eea65df37e)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fast utf8 validation when loading string view from parquet [arrow-rs]
Posted by "mapleFU (via GitHub)" <gi...@apache.org>.
mapleFU commented on code in PR #6009:
URL: https://github.com/apache/arrow-rs/pull/6009#discussion_r1667080895
##########
parquet/src/arrow/array_reader/byte_view_array.rs:
##########
@@ -332,7 +334,34 @@ impl ByteViewArrayDecoderPlain {
}
if self.validate_utf8 {
- check_valid_utf8(unsafe { buf.get_unchecked(start_offset..end_offset) })?;
+ // It seems you are trying to understand what's going on here, take a breath and be patient.
+ // Utf-8 validation is a non-trivial task, here are some background facts:
+ // (1) Validating one 100 bytes of utf-8 is much faster than validating ten 10 bytes of utf-8.
+ // Potentially because of the auto SIMD by compiler, someone please confirm this :)
+ // (2) Practical strings are short, 99% of strings are smaller than 100 bytes, as shown in paper:
+ // https://www.vldb.org/pvldb/vol17/p148-zeng.pdf, Figure 5f.
+ // (3) Parquet plain encoding makes utf-8 validation harder,
+ // because it stores the length of each string right before the string.
+ // This means naive utf-8 validation will be slow, because the validation need to skip the length bytes.
+ // I.e., the validation cannot validate the buffer in one pass, but instead, validate strings chunk by chunk.
+ //
+ // Given the above observations, the goal is to do batch validation as much as possible.
+ // The key idea is that if the length is smaller than 128 (99% of the case), then the length bytes are valid utf-8 (bc they are ASCII).
+ //
+ // The implementation keeps a water mark `utf8_validation_begin` to track the beginning of the buffer that is not validated.
+ // If the length is smaller than 128, then we continue to next string.
+ // If the length is larger than 128, then we validate the buffer before the length bytes, and move the water mark to the beginning of next string.
+ if len < 128 {
+ // fast path, move to next string.
+ // the len bytes are valid utf8.
+ } else {
+ // unfortunately, the len bytes may not be valid utf8, we need to wrap up and validate everything before it.
+ check_valid_utf8(unsafe {
+ buf.get_unchecked(utf8_validation_begin..self.offset)
+ })?;
+ // move the cursor to skip the len bytes.
+ utf8_validation_begin = start_offset;
Review Comment:
Just a naive question, should this point to `end_offset`?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fast utf8 validation when loading string view from parquet [arrow-rs]
Posted by "mapleFU (via GitHub)" <gi...@apache.org>.
mapleFU commented on code in PR #6009:
URL: https://github.com/apache/arrow-rs/pull/6009#discussion_r1667244678
##########
parquet/src/arrow/array_reader/byte_view_array.rs:
##########
@@ -332,7 +334,34 @@ impl ByteViewArrayDecoderPlain {
}
if self.validate_utf8 {
- check_valid_utf8(unsafe { buf.get_unchecked(start_offset..end_offset) })?;
+ // It seems you are trying to understand what's going on here, take a breath and be patient.
+ // Utf-8 validation is a non-trivial task, here are some background facts:
+ // (1) Validating one 100 bytes of utf-8 is much faster than validating ten 10 bytes of utf-8.
+ // Potentially because of the auto SIMD by compiler, someone please confirm this :)
+ // (2) Practical strings are short, 99% of strings are smaller than 100 bytes, as shown in paper:
+ // https://www.vldb.org/pvldb/vol17/p148-zeng.pdf, Figure 5f.
+ // (3) Parquet plain encoding makes utf-8 validation harder,
+ // because it stores the length of each string right before the string.
+ // This means naive utf-8 validation will be slow, because the validation need to skip the length bytes.
+ // I.e., the validation cannot validate the buffer in one pass, but instead, validate strings chunk by chunk.
+ //
+ // Given the above observations, the goal is to do batch validation as much as possible.
+ // The key idea is that if the length is smaller than 128 (99% of the case), then the length bytes are valid utf-8 (bc they are ASCII).
+ //
+ // The implementation keeps a water mark `utf8_validation_begin` to track the beginning of the buffer that is not validated.
+ // If the length is smaller than 128, then we continue to next string.
+ // If the length is larger than 128, then we validate the buffer before the length bytes, and move the water mark to the beginning of next string.
+ if len < 128 {
+ // fast path, move to next string.
+ // the len bytes are valid utf8.
+ } else {
+ // unfortunately, the len bytes may not be valid utf8, we need to wrap up and validate everything before it.
+ check_valid_utf8(unsafe {
+ buf.get_unchecked(utf8_validation_begin..self.offset)
+ })?;
+ // move the cursor to skip the len bytes.
+ utf8_validation_begin = start_offset;
Review Comment:
Thanks I got understand this, but would this check introduce checking for 4 bytes `len`, which could be non-utf8?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fast utf8 validation when loading string view from parquet [arrow-rs]
Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #6009:
URL: https://github.com/apache/arrow-rs/pull/6009#discussion_r1666988777
##########
parquet/src/arrow/array_reader/byte_view_array.rs:
##########
@@ -332,7 +334,34 @@ impl ByteViewArrayDecoderPlain {
}
if self.validate_utf8 {
- check_valid_utf8(unsafe { buf.get_unchecked(start_offset..end_offset) })?;
+ // It seems you are trying to understand what's going on here, take a breath and be patient.
+ // Utf-8 validation is a non-trivial task, here are some background facts:
+ // (1) Validating one 100 bytes of utf-8 is much faster than validating ten 10 bytes of utf-8.
+ // Potentially because of the auto SIMD by compiler, someone please confirm this :)
Review Comment:
The rust standard library at least used to have a lot of manual SIMD trickery to make UTF-8 validation fast
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fast utf8 validation when loading string view from parquet [arrow-rs]
Posted by "mapleFU (via GitHub)" <gi...@apache.org>.
mapleFU commented on code in PR #6009:
URL: https://github.com/apache/arrow-rs/pull/6009#discussion_r1667244678
##########
parquet/src/arrow/array_reader/byte_view_array.rs:
##########
@@ -332,7 +334,34 @@ impl ByteViewArrayDecoderPlain {
}
if self.validate_utf8 {
- check_valid_utf8(unsafe { buf.get_unchecked(start_offset..end_offset) })?;
+ // It seems you are trying to understand what's going on here, take a breath and be patient.
+ // Utf-8 validation is a non-trivial task, here are some background facts:
+ // (1) Validating one 100 bytes of utf-8 is much faster than validating ten 10 bytes of utf-8.
+ // Potentially because of the auto SIMD by compiler, someone please confirm this :)
+ // (2) Practical strings are short, 99% of strings are smaller than 100 bytes, as shown in paper:
+ // https://www.vldb.org/pvldb/vol17/p148-zeng.pdf, Figure 5f.
+ // (3) Parquet plain encoding makes utf-8 validation harder,
+ // because it stores the length of each string right before the string.
+ // This means naive utf-8 validation will be slow, because the validation need to skip the length bytes.
+ // I.e., the validation cannot validate the buffer in one pass, but instead, validate strings chunk by chunk.
+ //
+ // Given the above observations, the goal is to do batch validation as much as possible.
+ // The key idea is that if the length is smaller than 128 (99% of the case), then the length bytes are valid utf-8 (bc they are ASCII).
+ //
+ // The implementation keeps a water mark `utf8_validation_begin` to track the beginning of the buffer that is not validated.
+ // If the length is smaller than 128, then we continue to next string.
+ // If the length is larger than 128, then we validate the buffer before the length bytes, and move the water mark to the beginning of next string.
+ if len < 128 {
+ // fast path, move to next string.
+ // the len bytes are valid utf8.
+ } else {
+ // unfortunately, the len bytes may not be valid utf8, we need to wrap up and validate everything before it.
+ check_valid_utf8(unsafe {
+ buf.get_unchecked(utf8_validation_begin..self.offset)
+ })?;
+ // move the cursor to skip the len bytes.
+ utf8_validation_begin = start_offset;
Review Comment:
Thanks I got understand this, but would this check introduce checking for 4 bytes `len`, which could be non-utf8?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fast utf8 validation when loading string view from parquet [arrow-rs]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #6009:
URL: https://github.com/apache/arrow-rs/pull/6009
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fast utf8 validation when loading string view from parquet [arrow-rs]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #6009:
URL: https://github.com/apache/arrow-rs/pull/6009#issuecomment-2214922344
🚀
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fast utf8 validation when loading string view from parquet [arrow-rs]
Posted by "Xuanwo (via GitHub)" <gi...@apache.org>.
Xuanwo commented on code in PR #6009:
URL: https://github.com/apache/arrow-rs/pull/6009#discussion_r1667235108
##########
parquet/src/arrow/array_reader/byte_view_array.rs:
##########
@@ -332,7 +334,34 @@ impl ByteViewArrayDecoderPlain {
}
if self.validate_utf8 {
- check_valid_utf8(unsafe { buf.get_unchecked(start_offset..end_offset) })?;
+ // It seems you are trying to understand what's going on here, take a breath and be patient.
+ // Utf-8 validation is a non-trivial task, here are some background facts:
+ // (1) Validating one 100 bytes of utf-8 is much faster than validating ten 10 bytes of utf-8.
+ // Potentially because of the auto SIMD by compiler, someone please confirm this :)
+ // (2) Practical strings are short, 99% of strings are smaller than 100 bytes, as shown in paper:
+ // https://www.vldb.org/pvldb/vol17/p148-zeng.pdf, Figure 5f.
+ // (3) Parquet plain encoding makes utf-8 validation harder,
+ // because it stores the length of each string right before the string.
+ // This means naive utf-8 validation will be slow, because the validation need to skip the length bytes.
+ // I.e., the validation cannot validate the buffer in one pass, but instead, validate strings chunk by chunk.
+ //
+ // Given the above observations, the goal is to do batch validation as much as possible.
+ // The key idea is that if the length is smaller than 128 (99% of the case), then the length bytes are valid utf-8 (bc they are ASCII).
+ //
+ // The implementation keeps a water mark `utf8_validation_begin` to track the beginning of the buffer that is not validated.
+ // If the length is smaller than 128, then we continue to next string.
+ // If the length is larger than 128, then we validate the buffer before the length bytes, and move the water mark to the beginning of next string.
+ if len < 128 {
Review Comment:
Yes, an empty `if` block makes me spend some time checking if I missed something. But anyway, it's not a big issue. It's fine to keep as-is.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fast utf8 validation when loading string view from parquet [arrow-rs]
Posted by "Xuanwo (via GitHub)" <gi...@apache.org>.
Xuanwo commented on code in PR #6009:
URL: https://github.com/apache/arrow-rs/pull/6009#discussion_r1666961773
##########
parquet/src/arrow/array_reader/byte_view_array.rs:
##########
@@ -332,7 +334,34 @@ impl ByteViewArrayDecoderPlain {
}
if self.validate_utf8 {
- check_valid_utf8(unsafe { buf.get_unchecked(start_offset..end_offset) })?;
+ // It seems you are trying to understand what's going on here, take a breath and be patient.
+ // Utf-8 validation is a non-trivial task, here are some background facts:
+ // (1) Validating one 100 bytes of utf-8 is much faster than validating ten 10 bytes of utf-8.
+ // Potentially because of the auto SIMD by compiler, someone please confirm this :)
+ // (2) Practical strings are short, 99% of strings are smaller than 100 bytes, as shown in paper:
+ // https://www.vldb.org/pvldb/vol17/p148-zeng.pdf, Figure 5f.
+ // (3) Parquet plain encoding makes utf-8 validation harder,
+ // because it stores the length of each string right before the string.
+ // This means naive utf-8 validation will be slow, because the validation need to skip the length bytes.
+ // I.e., the validation cannot validate the buffer in one pass, but instead, validate strings chunk by chunk.
+ //
+ // Given the above observations, the goal is to do batch validation as much as possible.
+ // The key idea is that if the length is smaller than 128 (99% of the case), then the length bytes are valid utf-8 (bc they are ASCII).
+ //
+ // The implementation keeps a water mark `utf8_validation_begin` to track the beginning of the buffer that is not validated.
+ // If the length is smaller than 128, then we continue to next string.
+ // If the length is larger than 128, then we validate the buffer before the length bytes, and move the water mark to the beginning of next string.
+ if len < 128 {
Review Comment:
Hi, is there a reason to write `if` case in this way?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fast utf8 validation when loading string view from parquet [arrow-rs]
Posted by "Xuanwo (via GitHub)" <gi...@apache.org>.
Xuanwo commented on PR #6009:
URL: https://github.com/apache/arrow-rs/pull/6009#issuecomment-2211114194
> I believe the CI failure is not from us...
Yep, docs build failed for https://github.com/apache/arrow-rs/issues/6008
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fast utf8 validation when loading string view from parquet [arrow-rs]
Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #6009:
URL: https://github.com/apache/arrow-rs/pull/6009#discussion_r1666988923
##########
parquet/src/arrow/array_reader/byte_view_array.rs:
##########
@@ -332,7 +334,34 @@ impl ByteViewArrayDecoderPlain {
}
if self.validate_utf8 {
- check_valid_utf8(unsafe { buf.get_unchecked(start_offset..end_offset) })?;
+ // It seems you are trying to understand what's going on here, take a breath and be patient.
+ // Utf-8 validation is a non-trivial task, here are some background facts:
+ // (1) Validating one 100 bytes of utf-8 is much faster than validating ten 10 bytes of utf-8.
+ // Potentially because of the auto SIMD by compiler, someone please confirm this :)
+ // (2) Practical strings are short, 99% of strings are smaller than 100 bytes, as shown in paper:
+ // https://www.vldb.org/pvldb/vol17/p148-zeng.pdf, Figure 5f.
+ // (3) Parquet plain encoding makes utf-8 validation harder,
+ // because it stores the length of each string right before the string.
+ // This means naive utf-8 validation will be slow, because the validation need to skip the length bytes.
+ // I.e., the validation cannot validate the buffer in one pass, but instead, validate strings chunk by chunk.
+ //
+ // Given the above observations, the goal is to do batch validation as much as possible.
+ // The key idea is that if the length is smaller than 128 (99% of the case), then the length bytes are valid utf-8 (bc they are ASCII).
Review Comment:
This is a very clever idea
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org