Skip to content
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

Question: does whitespace detection by bitwise operation make jsoniter-scala faster? #1086

Open
i10416 opened this issue Sep 25, 2023 · 1 comment

Comments

@i10416
Copy link

i10416 commented Sep 25, 2023

There is a technique to detect whitespace using bitwise operation.

val spaces = 1L<<' ' | 1L<<'\n' | 1L<<'\r' | 1L<<'\t'
// because ' ', '\n', '\r', '\t' are less than or equals to 32, 
// you can get bit flags for whitespaces shifting 1 by their int value.

if (1<<char&spaces != 0) {
  // char is whitespace here
}

The same idea is discussed in typelevel/jawn#26 (comment)

Does this technique make jsoniter-scala faster or jsoniter-scala has already adopted this technique?

@plokhotnyuk
Copy link
Owner

plokhotnyuk commented Sep 25, 2023

Currently it is checked by the following code:

b == ' ' || b == '\n' || (b | 0x4) == '\r'

Predicted condition jumps are quite cheep and can have throughput of 2 checks per cycle on contemporary CPUs.

We could try it in the JVM version of JsonReader:

b <= 32 && ((1L << b) & 0x100002600L) == 0

Clang is able to make branch-less object code from that:
image

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

No branches or pull requests

2 participants