-
Notifications
You must be signed in to change notification settings - Fork 357
Speed up String encoding on CPUs without SIMD hardware support #812
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -162,8 +162,25 @@ static const unsigned char escape_table_basic[256] = { | |||||||||
|
|
||||||||||
| static unsigned char (*search_escape_basic_impl)(search_state *); | ||||||||||
|
|
||||||||||
| inline bool has_json_escapable_byte(uint64_t x) { | ||||||||||
| uint64_t is_ascii = 0x8080808080808080ULL & ~x; | ||||||||||
| uint64_t xor2 = x ^ 0x0202020202020202ULL; | ||||||||||
| uint64_t lt32_or_eq34 = xor2 - 0x2121212121212121ULL; | ||||||||||
| uint64_t sub92 = x ^ 0x5C5C5C5C5C5C5C5CULL; | ||||||||||
| uint64_t eq92 = (sub92 - 0x0101010101010101ULL); | ||||||||||
| return ((lt32_or_eq34 | eq92) & is_ascii) != 0; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| static inline unsigned char search_escape_basic(search_state *search) | ||||||||||
| { | ||||||||||
| while (search->ptr <= search->end - 8) { | ||||||||||
| uint64_t* pi = (uint64_t*)(search->ptr); | ||||||||||
| if(has_json_escapable_byte(*pi)) { | ||||||||||
|
Comment on lines
+177
to
+178
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not a huge deal, but rather than a function that return a boolean and fallback to the slow path, we could use the same structure as SIMD codepaths (return a mask that gives us the position of characters to escape).
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That being said, I'm not sure if this PR is really worth it, as we kinda assume the overwhelming majority of users will have SIMD available. So it's questionable whether complexifying the fallback implementation bring any benefit.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This PR comes from my curiosity how far I could take the C code without specialized hardware, and then wanted to share the results. But I share the hesitancy to merge. Thanks for the review, closing this now. |
||||||||||
| break; | ||||||||||
| } | ||||||||||
| search->ptr += 8; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| while (search->ptr < search->end) { | ||||||||||
| if (RB_UNLIKELY(escape_table_basic[(const unsigned char)*search->ptr])) { | ||||||||||
| search_flush(search); | ||||||||||
|
|
||||||||||
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.