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

Make JSON.generate 1.75x as fast #562

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Commits on Dec 27, 2023

  1. Stop prebuilding array_delim

    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.
    mame committed Dec 27, 2023
    Configuration menu
    Copy the full SHA
    e1cf787 View commit details
    Browse the repository at this point in the history
  2. Stop prebuilding object_delim

    This speeds up `JSON.generate` by about 4% in a benchmark
    mame committed Dec 27, 2023
    Configuration menu
    Copy the full SHA
    08f79bb View commit details
    Browse the repository at this point in the history
  3. Stop prebuilding object_delim2

    Also, remove static functions that are no longer used.
    
    This speeds up `JSON.generate` by about 5% in a benchmark.
    mame committed Dec 27, 2023
    Configuration menu
    Copy the full SHA
    964f42e View commit details
    Browse the repository at this point in the history
  4. Apply RB_UNLIKELY for less frequently used options

    This speeds up `JSON.generate` by about 4% in a benchmark.
    mame committed Dec 27, 2023
    Configuration menu
    Copy the full SHA
    8a800d3 View commit details
    Browse the repository at this point in the history
  5. Use RB_ENCODING_GET instead of rb_enc_get to improve performance

    This speeds up `JSON.generate` by about 12% in a benchmark.
    mame committed Dec 27, 2023
    Configuration menu
    Copy the full SHA
    a19e80b View commit details
    Browse the repository at this point in the history
  6. 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.
    mame committed Dec 27, 2023
    Configuration menu
    Copy the full SHA
    a81ec47 View commit details
    Browse the repository at this point in the history
  7. 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.
    mame committed Dec 27, 2023
    Configuration menu
    Copy the full SHA
    fc98d7f View commit details
    Browse the repository at this point in the history
  8. 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.
    mame committed Dec 27, 2023
    Configuration menu
    Copy the full SHA
    2914e35 View commit details
    Browse the repository at this point in the history
  9. Use RARRAY_AREF instead of rb_ary_entry to improve performance

    It 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.
    mame committed Dec 27, 2023
    Configuration menu
    Copy the full SHA
    946cefe View commit details
    Browse the repository at this point in the history
  10. Configuration menu
    Copy the full SHA
    da58b62 View commit details
    Browse the repository at this point in the history