-
Notifications
You must be signed in to change notification settings - Fork 326
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
Make JSON.generate
1.75x as fast
#562
base: master
Are you sure you want to change the base?
Commits on Dec 27, 2023
-
The purpose of this change is to exploit `fbuffer_append_char` that is faster than `fbuffer_append`. `array_delim` was a buffer that concatenated a single comma with `array_nl`. However, in the typical use case (`JSON.generate(data)`), `array_nl` is empty. This means that `array_delim` was a single-character buffer in many cases. `fbuffer_append(buffer, array_delim)` used `memcpy` to copy one byte, which was not so efficient. Rather, this change uses `fbuffer_append_char(buffer, ',')` and then `fbuffer_append(buffer, array_nl)` only when `array_nl` is not NULL. This speeds up `JSON.generate` by about 9% in a benchmark.
Configuration menu - View commit details
-
Copy full SHA for e1cf787 - Browse repository at this point
Copy the full SHA e1cf787View commit details -
This speeds up `JSON.generate` by about 4% in a benchmark
Configuration menu - View commit details
-
Copy full SHA for 08f79bb - Browse repository at this point
Copy the full SHA 08f79bbView commit details -
Stop prebuilding object_delim2
Also, remove static functions that are no longer used. This speeds up `JSON.generate` by about 5% in a benchmark.
Configuration menu - View commit details
-
Copy full SHA for 964f42e - Browse repository at this point
Copy the full SHA 964f42eView commit details -
Apply RB_UNLIKELY for less frequently used options
This speeds up `JSON.generate` by about 4% in a benchmark.
Configuration menu - View commit details
-
Copy full SHA for 8a800d3 - Browse repository at this point
Copy the full SHA 8a800d3View commit details -
Use
RB_ENCODING_GET
instead ofrb_enc_get
to improve performanceThis speeds up `JSON.generate` by about 12% in a benchmark.
Configuration menu - View commit details
-
Copy full SHA for a19e80b - Browse repository at this point
Copy the full SHA a19e80bView commit details -
Pre-check that escaping is not required for string dump
... instead of `rb_enc_str_asciionly_p`. If escaping is not needed, we can use `fbuffer_append` directly, which is much faster. This speeds up `JSON.generate` by about 16% in a benchmark.
Configuration menu - View commit details
-
Copy full SHA for a81ec47 - Browse repository at this point
Copy the full SHA a81ec47View commit details -
Use efficient object-type dispatching
Dispatching based on Ruby's VALUE structure is more efficient than simply cascaded "if ... else if ..." checks. This speeds up `JSON.generate` by about 5% in a benchmark.
Configuration menu - View commit details
-
Copy full SHA for fc98d7f - Browse repository at this point
Copy the full SHA fc98d7fView commit details -
Directly use
generate_json_string
for object keys... instead of `generate_json`. Since the object key is already confirmed to be a string, using a generic dispatch function brings an unnecessary overhead. This speeds up `JSON.generate` by about 3% in a benchmark.
Configuration menu - View commit details
-
Copy full SHA for 2914e35 - Browse repository at this point
Copy the full SHA 2914e35View commit details -
Use
RARRAY_AREF
instead ofrb_ary_entry
to improve performanceIt is safe to use `RARRAY_AREF` here because no Ruby code is executed between `RARRAY_LEN` and `RARRAY_AREF`. This speeds up `JSON.generate` by about 4% in a benchmark.
Configuration menu - View commit details
-
Copy full SHA for 946cefe - Browse repository at this point
Copy the full SHA 946cefeView commit details -
Configuration menu - View commit details
-
Copy full SHA for da58b62 - Browse repository at this point
Copy the full SHA da58b62View commit details