perf(strings): SIMD-vectorize lengthUnicode#7
Open
jaylisde wants to merge 1 commit into
Open
Conversation
The uncapped UTF-8 length helper used by the Presto/Spark `length()` SQL
function on non-ASCII strings was a per-byte scalar loop counting bytes
where (b & 0xC0) != 0x80. Replace with an xsimd block that does the same
test on kBatchSize bytes at a time, popcounts the resulting mask, and
falls through to the existing scalar tail. The same trick is already
used by `cappedUnicodeImpl` further down in the same file.
Microbench (release, AMD EPYC 9R14, AVX2):
benchmark scalar vectorized speedup
utfLength_100 367.55 ms 222.96 ms 1.66x
utfLength_1024 3.02 s 1.46 s 2.07x
ASCII paths are unaffected (length() returns input.size() on
isAscii=true via the callAscii overload of LengthFunction).
Test plan:
pytest-style: builds + runs cleanly under release.
cmake --build _build/release --target velox_functions_string_test \
velox_functions_test
./velox_functions_string_test # 27/27
./velox_functions_test --gtest_filter='StringFunctionsTest.*' # 53/53
Signed-off-by: Shaojie Li <jieshao@amazon.com>
jaylisde
force-pushed
the
perf/cpu-length-unicode-simd
branch
from
June 4, 2026 21:53
9f3156f to
063b782
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
lengthUnicode(uncapped UTF-8 length, backinglength()on non-ASCII input) was a per-byte scalar loop. This replaces it with the same SIMD continuation-byte count + popcount already used bycappedUnicodeImplin the same file, with a scalar tail for the remainder.Microbenchmark
Release, AMD EPYC 9R14 (AVX2):
ASCII is unaffected —
LengthFunction::callAsciireturnsinput.size()and never callslengthUnicode.Test plan
velox_functions_string_test27/27,velox_functions_test --gtest_filter='StringFunctionsTest.*'53/53. Correctness covered by the existingStringFunctionsTest.lengthcase over ASCII + multi-byte inputs.