-
Notifications
You must be signed in to change notification settings - Fork 355
Use SWAR for parsing integers on little endian machines. #878
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
a9c9a22 to
5b76dc4
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I think we can simply do (comp & 0xFFFFFFFF) == 0x33333333 to check for 4 consecutive digits.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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;
}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>
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>
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 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