Skip to content

perf(strings): SIMD-vectorize lengthUnicode#7

Open
jaylisde wants to merge 1 commit into
mainfrom
perf/cpu-length-unicode-simd
Open

perf(strings): SIMD-vectorize lengthUnicode#7
jaylisde wants to merge 1 commit into
mainfrom
perf/cpu-length-unicode-simd

Conversation

@jaylisde

@jaylisde jaylisde commented Jun 4, 2026

Copy link
Copy Markdown
Owner

Summary

lengthUnicode (uncapped UTF-8 length, backing length() on non-ASCII input) was a per-byte scalar loop. This replaces it with the same SIMD continuation-byte count + popcount already used by cappedUnicodeImpl in the same file, with a scalar tail for the remainder.

Microbenchmark

Release, AMD EPYC 9R14 (AVX2):

benchmark scalar vectorized speedup
utfLength_100 367.55 ms 222.96 ms 1.66×
utfLength_1024 3.02 s 1.46 s 2.07×

ASCII is unaffected — LengthFunction::callAscii returns input.size() and never calls lengthUnicode.

Test plan

velox_functions_string_test 27/27, velox_functions_test --gtest_filter='StringFunctionsTest.*' 53/53. Correctness covered by the existing StringFunctionsTest.length case over ASCII + multi-byte inputs.

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
jaylisde force-pushed the perf/cpu-length-unicode-simd branch from 9f3156f to 063b782 Compare June 4, 2026 21:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant