Speed up String encoding on CPUs without SIMD hardware support#812
Speed up String encoding on CPUs without SIMD hardware support#812radiospiel wants to merge 1 commit into
Conversation
This change tests for the presence of characters to be escaped on CPUs without special SIMD instructions. We are testing 8 chars in one round, following a code excerpt from https://lemire.me/blog/2025/04/13/detect-control-characters-quotes-and-backslashes-efficiently-using-swar/ For strings that don't require escapes I have seen speedups of around 15% with this change.
|
|
||
| static unsigned char (*search_escape_basic_impl)(search_state *); | ||
|
|
||
| inline bool has_json_escapable_byte(uint64_t x) { |
There was a problem hiding this comment.
| inline bool has_json_escapable_byte(uint64_t x) { | |
| static inline bool has_json_escapable_byte(uint64_t x) { |
| uint64_t* pi = (uint64_t*)(search->ptr); | ||
| if(has_json_escapable_byte(*pi)) { |
There was a problem hiding this comment.
| uint64_t* pi = (uint64_t*)(search->ptr); | |
| if(has_json_escapable_byte(*pi)) { | |
| uint64_t *pi = (uint64_t *)search->ptr; | |
| if (has_json_escapable_byte(*pi)) { |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
This change tests for the presence of characters to be escaped on CPUs without special SIMD instructions. We are testing 8 chars in one round, following a code excerpt from https://lemire.me/blog/2025/04/13/detect-control-characters-quotes-and-backslashes-efficiently-using-swar/
For strings that don't require escapes I have seen speedups of around 15% with this change.