Use SWAR for parsing integers on little endian machines.#878
Closed
samyron wants to merge 2 commits into
Closed
Conversation
a9c9a22 to
5b76dc4
Compare
byroot
reviewed
Oct 31, 2025
byroot
left a comment
Member
There was a problem hiding this comment.
I like this a lot. But I think I'll refactor it a bit to reduce duplication, and potentially improve code generation.
| static inline int has_eight_consecutive_digits(const char *p) { | ||
| uint64_t val; | ||
| memcpy(&val, p, sizeof(uint64_t)); | ||
| return (((val & 0xF0F0F0F0F0F0F0F0) | (((val + 0x0606060606060606) & 0xF0F0F0F0F0F0F0F0) >> 4)) == 0x3333333333333333); |
Member
There was a problem hiding this comment.
I'm thinking we could use that trick combined with clz (or similar) to know how many consecutive digits we have.
I suspect 8 consecutive digits aren't that common, but if we also had a 4 digits (uint32_t) version and a fast dispatch, that could help on more benchmarks.
Member
There was a problem hiding this comment.
Actually, I think we can simply do (comp & 0xFFFFFFFF) == 0x33333333 to check for 4 consecutive digits.
Member
There was a problem hiding this comment.
4bytes version:
static inline uint32_t parse_four_digits_unrolled(const char *p) {
uint64_t large_val;
memcpy(&large_val, p, sizeof(uint64_t));
uint32_t val = (uint32_t)large_val;
const uint32_t mask = 0x000000FF;
const uint32_t mul1 = 100;
val -= 0x30303030;
val = (val * 10) + (val >> 8); // val = (val * 2561) >> 8;
val = ((val & mask) * mul1) + (((val >> 16) & mask));
return (uint32_t)val;
}
Merged
byroot
added a commit
to byroot/json
that referenced
this pull request
Nov 1, 2025
Closes: ruby#878 ``` == Parsing float parsing (2251051 bytes) ruby 3.4.6 (2025-09-16 revision dbd83256b1) +YJIT +PRISM [arm64-darwin24] Warming up -------------------------------------- after 23.000 i/100ms Calculating ------------------------------------- after 214.382 (± 0.5%) i/s (4.66 ms/i) - 1.081k in 5.042555s Comparison: before: 189.5 i/s after: 214.4 i/s - 1.13x faster ``` Co-Authored-By: Scott Myron <samyron@gmail.com>
byroot
added a commit
to byroot/json
that referenced
this pull request
Nov 1, 2025
Closes: ruby#878 ``` == Parsing float parsing (2251051 bytes) ruby 3.4.6 (2025-09-16 revision dbd83256b1) +YJIT +PRISM [arm64-darwin24] Warming up -------------------------------------- after 23.000 i/100ms Calculating ------------------------------------- after 214.382 (± 0.5%) i/s (4.66 ms/i) - 1.081k in 5.042555s Comparison: before: 189.5 i/s after: 214.4 i/s - 1.13x faster ``` Co-Authored-By: Scott Myron <samyron@gmail.com>
matzbot
pushed a commit
to ruby/ruby
that referenced
this pull request
Nov 1, 2025
Closes: ruby/json#878 ``` == Parsing float parsing (2251051 bytes) ruby 3.4.6 (2025-09-16 revision ruby/json@dbd83256b1) +YJIT +PRISM [arm64-darwin24] Warming up -------------------------------------- after 23.000 i/100ms Calculating ------------------------------------- after 214.382 (± 0.5%) i/s (4.66 ms/i) - 1.081k in 5.042555s Comparison: before: 189.5 i/s after: 214.4 i/s - 1.13x faster ``` ruby/json@6348ff0891 Co-Authored-By: Scott Myron <samyron@gmail.com>
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.
This PR uses SWAR on little endian architectures to recognize and parse eight consecutive ASCII digits. This seems to have positive performance improvements when parsing long (but not too long) integers and floats.
The
unsigned 32 bit integer parsingdata was created with the following:The
integer parsingwas created witht he following:The benchmarks below are using a Macbook Air M1.
This branch compared to master
Run 1
Run 2
This branch compared to other libraries