[SPARK-59062][CORE] Word-at-a-time single-byte search for UTF8String.contains - #58354
Open
david-mollitor-db wants to merge 1 commit into
Open
[SPARK-59062][CORE] Word-at-a-time single-byte search for UTF8String.contains#58354david-mollitor-db wants to merge 1 commit into
david-mollitor-db wants to merge 1 commit into
Conversation
david-mollitor-db
force-pushed
the
SPARK-contains-swar
branch
from
August 27, 2026 19:35
0df8292 to
f16e6eb
Compare
…contains `UTF8String.contains` scans byte-by-byte. For a single-byte needle -- the common case produced by `LIKE '%x%'` (rewritten to `Contains`) with an ASCII character -- add a word-at-a-time (SWAR / memchr-style) scan that checks 8 bytes per iteration. Add `ByteArrayMethods.containsByte(base, offset, length, target)`, which XORs each 8-byte word with the broadcast target byte and uses the exact "word contains a zero byte" test `(w - 0x0101010101010101L) & ~w & 0x8080808080808080L`. It reports only existence (not position), so it is endianness independent; alignment handling mirrors `arrayEquals`. `UTF8String.contains` takes a `numBytes == 1` fast path that delegates to it, which also drops the redundant per-position `matchAt` call.
david-mollitor-db
force-pushed
the
SPARK-contains-swar
branch
from
August 27, 2026 19:56
f16e6eb to
e4c0b14
Compare
david-mollitor-db
marked this pull request as ready for review
August 27, 2026 19:57
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.
What changes were proposed in this pull request?
UTF8String.containsscans for the needle byte-by-byte. For a single-byte needle --the common case produced by
LIKE '%x%'(which the optimizer rewrites toContains) withan ASCII character -- this PR adds a word-at-a-time (SWAR / memchr-style) scan.
ByteArrayMethods.containsByte(Object base, long offset, long length, byte target):broadcasts the target byte to all 8 lanes of a word, then for each 8-byte word XORs and
applies the classic exact "a word contains a zero byte" test
(w - 0x0101010101010101L) & ~w & 0x8080808080808080L. It reports only existence, notposition, so it is endianness independent. Alignment handling mirrors
arrayEquals.UTF8String.containstakes anumBytes == 1fast path delegating to it, which also dropsthe redundant per-position
matchAtcall.Why are the changes needed?
Containssingle-character predicates are common ('%,%','%@%','% %','%/%'). Thecurrent byte-at-a-time scan reads through
Platform.getByte(Unsafe), which the JIT cannotauto-vectorize, so it processes one byte per iteration.
Does this PR introduce any user-facing change?
No.
How was this patch tested?
UTF8StringSuite.containsgains single-byte cases (first byte, past the first word, lastbyte, absent, sub-word length); the property-based
UTF8StringPropertyCheckSuitealso passes.Local microbenchmark (needle absent, i.e. full scan), nanoseconds per call:
Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Opus 4.8