Skip to content

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

radiospiel
Copy link
Contributor

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.

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.
@@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
inline bool has_json_escapable_byte(uint64_t x) {
static inline bool has_json_escapable_byte(uint64_t x) {

Comment on lines +177 to +178
uint64_t* pi = (uint64_t*)(search->ptr);
if(has_json_escapable_byte(*pi)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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)) {

Copy link
Member

Choose a reason for hiding this comment

The 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).

Copy link
Member

Choose a reason for hiding this comment

The 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants