perf: use 256-byte lookup table for stripChars ASCII membership#898
Open
He-Pin wants to merge 2 commits into
Open
perf: use 256-byte lookup table for stripChars ASCII membership#898He-Pin wants to merge 2 commits into
He-Pin wants to merge 2 commits into
Conversation
Replace the 128-bit bitmask approach (two Long values with conditional branches) with a 256-byte lookup table for ASCII character membership testing in stripChars/lstripChars/rstripChars. The lookup table eliminates the two conditional branches per char in inAsciiMask(), replacing them with a single array load. This keeps the strip loop branch-free and cache-friendly. Also added early return optimization for empty inputs.
Motivation: The old variable name `allAscii` and comment "branch-free" were inaccurate — the lookup table covers chars 0-255 (byte range), not just ASCII 0-127, and the loop still has branches. Modification: - Rename `allAscii` to `allByte` to reflect the actual 0-255 range - Fix comments to accurately describe the optimization Result: Code documentation now accurately reflects what the code does.
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.
Motivation
The
std.stripChars/std.lstripChars/std.rstripCharsfunctions were 1.45-1.74x slower than jrsonnet (Rust implementation) on Scala Native. The main bottleneck was theinAsciiMaskfunction which had two conditional branches per character for ASCII membership testing.Key Design Decision
Replace the 128-bit bitmask approach (two Long values with conditional branches) with a 256-byte lookup table for ASCII character membership testing. The lookup table eliminates the two conditional branches per char, replacing them with a single array load.
Modification
Changed
sjsonnet/src/sjsonnet/stdlib/StringModule.scala:stripAsciiMaskwithstripLookupusing 256-byte lookup tableinAsciiMaskandstripAsciiMaskmethodsBenchmark Results
Scala Native vs jrsonnet (hyperfine)
JMH (JVM)
Baseline JMH benchmarks are stable; the change benefits all platforms.
Analysis
The 256-byte lookup table approach eliminates the two conditional branches in
inAsciiMask():if (c < 64) (lo & (1L << c)) != 0L else if (c < 128) (hi & (1L << (c - 64))) != 0Ltable(c) != 0This keeps the strip loop branch-free and cache-friendly. The lookup table allocation (256 bytes) is amortized across the strip operation.
References
Result
./mill __.test)./mill __.reformat)