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
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions ext/json/ext/generator/generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

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

break;
}
search->ptr += 8;
}

while (search->ptr < search->end) {
if (RB_UNLIKELY(escape_table_basic[(const unsigned char)*search->ptr])) {
search_flush(search);
Expand Down
Loading