Skip to content

refactor character_length impl by unifying null handling logic #16877

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

waynexia
Copy link
Member

Which issue does this PR close?

Rationale for this change

Simplify the implementation by unifying the non-null and nullable branches. T::default_value() is just 0, which is the same as get .len() from an empty slice returned by string_array.value(). And in both way, the NULL in result array is marked by null buffer, not .push_null().

And I also find a performance improvement by doing it this way (and I verified this is not from value_unchecked):

character_length_StringArray_ascii_str_len_8
                        time:   [1.4357 µs 1.4402 µs 1.4456 µs]
                        change: [-73.834% -73.708% -73.579%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 21 outliers among 100 measurements (21.00%)
  1 (1.00%) high mild
  20 (20.00%) high severe

character_length_StringArray_utf8_str_len_8
                        time:   [32.558 µs 32.586 µs 32.615 µs]
                        change: [-9.6460% -9.4639% -9.3101%] (p = 0.00 < 0.05)
                        Performance has improved.

character_length_StringViewArray_ascii_str_len_8
                        time:   [16.820 µs 16.846 µs 16.878 µs]
                        change: [-23.762% -23.629% -23.496%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 7 outliers among 100 measurements (7.00%)
  3 (3.00%) high mild
  4 (4.00%) high severe

character_length_StringViewArray_utf8_str_len_8
                        time:   [32.588 µs 32.677 µs 32.787 µs]
                        change: [-8.1913% -8.0556% -7.8956%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 4 outliers among 100 measurements (4.00%)
  1 (1.00%) high mild
  3 (3.00%) high severe

character_length_StringArray_ascii_str_len_32
                        time:   [3.9844 µs 3.9866 µs 3.9888 µs]
                        change: [-50.422% -50.382% -50.343%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 2 outliers among 100 measurements (2.00%)
  2 (2.00%) high mild

character_length_StringArray_utf8_str_len_32
                        time:   [40.577 µs 40.611 µs 40.648 µs]
                        change: [+10.569% +10.705% +10.832%] (p = 0.00 < 0.05)
                        Performance has regressed.
Found 2 outliers among 100 measurements (2.00%)
  1 (1.00%) high mild
  1 (1.00%) high severe

character_length_StringViewArray_ascii_str_len_32
                        time:   [11.666 µs 11.687 µs 11.712 µs]
                        change: [-26.007% -25.861% -25.677%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 5 outliers among 100 measurements (5.00%)
  2 (2.00%) high mild
  3 (3.00%) high severe

character_length_StringViewArray_utf8_str_len_32
                        time:   [42.347 µs 42.371 µs 42.401 µs]
                        change: [+10.199% +10.295% +10.390%] (p = 0.00 < 0.05)
                        Performance has regressed.
Found 8 outliers among 100 measurements (8.00%)
  4 (4.00%) high mild
  4 (4.00%) high severe

character_length_StringArray_ascii_str_len_128
                        time:   [14.356 µs 14.382 µs 14.413 µs]
                        change: [-22.226% -22.152% -22.062%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 5 outliers among 100 measurements (5.00%)
  2 (2.00%) high mild
  3 (3.00%) high severe

character_length_StringArray_utf8_str_len_128
                        time:   [54.869 µs 54.975 µs 55.098 µs]
                        change: [-2.7784% -2.6015% -2.4263%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 5 outliers among 100 measurements (5.00%)
  1 (1.00%) low mild
  3 (3.00%) high mild
  1 (1.00%) high severe

character_length_StringViewArray_ascii_str_len_128
                        time:   [20.701 µs 20.760 µs 20.822 µs]
                        change: [-17.226% -17.001% -16.771%] (p = 0.00 < 0.05)
                        Performance has improved.

character_length_StringViewArray_utf8_str_len_128
                        time:   [56.900 µs 56.946 µs 56.998 µs]
                        change: [-3.1899% -2.9799% -2.7714%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 2 outliers among 100 measurements (2.00%)
  1 (1.00%) high mild
  1 (1.00%) high severe

character_length_StringArray_ascii_str_len_4096
                        time:   [499.09 µs 501.67 µs 504.62 µs]
                        change: [-2.5911% -1.9208% -1.3158%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 2 outliers among 100 measurements (2.00%)
  2 (2.00%) high mild

Benchmarking character_length_StringArray_utf8_str_len_4096: Warming up for 3.0000 s
Warning: Unable to complete 100 samples in 5.0s. You may wish to increase target time to 7.2s, enable flat sampling, or reduce sample count to 50.
character_length_StringArray_utf8_str_len_4096
                        time:   [1.4106 ms 1.4188 ms 1.4284 ms]
                        change: [-0.4802% +0.1161% +0.7536%] (p = 0.72 > 0.05)
                        No change in performance detected.
Found 5 outliers among 100 measurements (5.00%)
  3 (3.00%) high mild
  2 (2.00%) high severe

character_length_StringViewArray_ascii_str_len_4096
                        time:   [440.63 µs 443.01 µs 445.84 µs]
                        change: [-0.0900% +0.5865% +1.1823%] (p = 0.07 > 0.05)
                        No change in performance detected.
Found 5 outliers among 100 measurements (5.00%)
  3 (3.00%) high mild
  2 (2.00%) high severe

Benchmarking character_length_StringViewArray_utf8_str_len_4096: Warming up for 3.0000 s
Warning: Unable to complete 100 samples in 5.0s. You may wish to increase target time to 7.5s, enable flat sampling, or reduce sample count to 50.
character_length_StringViewArray_utf8_str_len_4096
                        time:   [1.4722 ms 1.4775 ms 1.4836 ms]
                        change: [-2.0827% -1.3739% -0.7239%] (p = 0.00 < 0.05)
                        Change within noise threshold.
Found 5 outliers among 100 measurements (5.00%)
  5 (5.00%) high mild

But there are two outliers character_length_StringArray_utf8_str_len_32 and character_length_StringViewArray_utf8_str_len_32 that become 10% slower, for some reason I don't know 😞.

What changes are included in this PR?

simplify character_length's implementation

Are these changes tested?

with existing test case

Are there any user-facing changes?

waynexia added 2 commits July 24, 2025 06:09
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
@github-actions github-actions bot added the functions Changes to functions implementation label Jul 23, 2025
Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

Thanks @waynexia -- I think we should try and figure out why some of the bechmarks got slower (maybe it is because checking for nulls is faster than computing zero lengths strs 🤔 )

} else {
let values: Vec<_> = (0..array.len())
.map(|i| {
let value = array.value(i);
// Safety: we are iterating with array.len() so the index is always valid
Copy link
Contributor

Choose a reason for hiding this comment

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

Another idea here is if you know the values are always ascii, you can avoid making a &str at all -- and instead simply compute the character lengths based on the offsets array (for StringArray and LargeStringArray) or the views for `StringViewArray)

@@ -136,56 +136,31 @@ where
// string is ASCII only is relatively cheap.
// If strings are ASCII only, count bytes instead.
let is_array_ascii_only = array.is_ascii();
let array = if array.null_count() == 0 {
let nulls = array.nulls().cloned();
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the idea to remove the no-nulls optimization because it doesn't make things faster?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think avoiding the null check (array.is_null(i)) makes things faster and it seems we don't need 0 as we always cloned array.nulls().cloned()

waynexia added 2 commits July 27, 2025 14:53
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
@waynexia
Copy link
Member Author

I do find revert the null checking logic on non-ascii branch will eliminate regression on those two cases, but it would slow down other cases in the same time. Still not sure why the get value operation is slower only for 32-length UTF-8...

Maybe we can make it faster by branching using offsets (like check if start equals to end), but StringArrayType doesn't provide such APIs (the same as #16877 (comment)).

However I find checking if the value is empty (zero length) in advance will make it a bit faster, the regression changes from 10% to 5% while keeping other cases unchanged:

character_length_StringArray_ascii_str_len_8
                        time:   [1.4314 µs 1.4336 µs 1.4369 µs]
                        change: [-73.924% -73.891% -73.859%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 3 outliers among 100 measurements (3.00%)
  1 (1.00%) high mild
  2 (2.00%) high severe

character_length_StringArray_utf8_str_len_8
                        time:   [31.946 µs 31.979 µs 32.018 µs]
                        change: [-11.242% -11.106% -10.965%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 6 outliers among 100 measurements (6.00%)
  3 (3.00%) high mild
  3 (3.00%) high severe

character_length_StringViewArray_ascii_str_len_8
                        time:   [15.377 µs 15.390 µs 15.404 µs]
                        change: [-30.680% -30.603% -30.527%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 3 outliers among 100 measurements (3.00%)
  2 (2.00%) high mild
  1 (1.00%) high severe

character_length_StringViewArray_utf8_str_len_8
                        time:   [32.590 µs 32.675 µs 32.767 µs]
                        change: [-8.6176% -8.4165% -8.1910%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 8 outliers among 100 measurements (8.00%)
  4 (4.00%) high mild
  4 (4.00%) high severe

character_length_StringArray_ascii_str_len_32
                        time:   [3.9932 µs 3.9959 µs 3.9982 µs]
                        change: [-50.365% -50.323% -50.281%] (p = 0.00 < 0.05)
                        Performance has improved.

character_length_StringArray_utf8_str_len_32
                        time:   [38.780 µs 38.815 µs 38.847 µs]
                        change: [+5.2960% +5.3832% +5.4714%] (p = 0.00 < 0.05)
                        Performance has regressed.

character_length_StringViewArray_ascii_str_len_32
                        time:   [11.412 µs 11.427 µs 11.443 µs]
                        change: [-27.836% -27.742% -27.636%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 4 outliers among 100 measurements (4.00%)
  3 (3.00%) high mild
  1 (1.00%) high severe

character_length_StringViewArray_utf8_str_len_32
                        time:   [41.017 µs 41.067 µs 41.114 µs]
                        change: [+6.4789% +6.6189% +6.7539%] (p = 0.00 < 0.05)
                        Performance has regressed.
Found 1 outliers among 100 measurements (1.00%)
  1 (1.00%) high mild

character_length_StringArray_ascii_str_len_128
                        time:   [14.538 µs 14.575 µs 14.612 µs]
                        change: [-23.194% -22.995% -22.793%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 1 outliers among 100 measurements (1.00%)
  1 (1.00%) high mild

character_length_StringArray_utf8_str_len_128
                        time:   [53.908 µs 53.982 µs 54.065 µs]
                        change: [-5.0270% -4.8019% -4.5793%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 7 outliers among 100 measurements (7.00%)
  1 (1.00%) low mild
  1 (1.00%) high mild
  5 (5.00%) high severe

character_length_StringViewArray_ascii_str_len_128
                        time:   [22.863 µs 22.970 µs 23.072 µs]
                        change: [-13.846% -13.226% -12.614%] (p = 0.00 < 0.05)
                        Performance has improved.

character_length_StringViewArray_utf8_str_len_128
                        time:   [56.301 µs 56.404 µs 56.515 µs]
                        change: [-4.7315% -4.5431% -4.3550%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 5 outliers among 100 measurements (5.00%)
  5 (5.00%) low mild

character_length_StringArray_ascii_str_len_4096
                        time:   [500.96 µs 502.81 µs 504.84 µs]
                        change: [-3.3507% -2.7881% -2.2107%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 7 outliers among 100 measurements (7.00%)
  4 (4.00%) high mild
  3 (3.00%) high severe

Benchmarking character_length_StringArray_utf8_str_len_4096: Warming up for 3.0000 s
Warning: Unable to complete 100 samples in 5.0s. You may wish to increase target time to 7.2s, enable flat sampling, or reduce sample count to 50.
character_length_StringArray_utf8_str_len_4096
                        time:   [1.4200 ms 1.4257 ms 1.4321 ms]
                        change: [-4.4737% -3.1885% -1.9979%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 8 outliers among 100 measurements (8.00%)
  7 (7.00%) high mild
  1 (1.00%) high severe

character_length_StringViewArray_ascii_str_len_4096
                        time:   [448.59 µs 450.82 µs 453.06 µs]
                        change: [-1.7833% -1.1598% -0.5034%] (p = 0.00 < 0.05)
                        Change within noise threshold.
Found 4 outliers among 100 measurements (4.00%)
  4 (4.00%) high mild

Benchmarking character_length_StringViewArray_utf8_str_len_4096: Warming up for 3.0000 s
Warning: Unable to complete 100 samples in 5.0s. You may wish to increase target time to 7.6s, enable flat sampling, or reduce sample count to 50.
character_length_StringViewArray_utf8_str_len_4096
                        time:   [1.4845 ms 1.4914 ms 1.4994 ms]
                        change: [-2.4781% -1.9180% -1.2655%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 10 outliers among 100 measurements (10.00%)
  7 (7.00%) high mild
  3 (3.00%) high severe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
functions Changes to functions implementation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants