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

Optimize option value detection #481

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

Commits on Oct 16, 2021

  1. Optimize option value detection

    This patch introduces functions same as rb_hash_has_key()
    to reduce rb_funcall() calling because it has a overhead.
    
    −               | before   | after    | result
    --               | --       | --       | --
    JSON.parse       | 302.109k | 343.547k | 1.14x
    JSON.generate    | 569.705k | 568.668k | −
    
    ### Environment
    - MacBook Air (M1, 2020)
    - macOS 12.0 beta
    - Apple M1
    - Ruby 3.0.2
    
    ### Before
    ```
    Warming up --------------------------------------
              JSON.parse    30.347k i/100ms
           JSON.generate    56.987k i/100ms
    Calculating -------------------------------------
              JSON.parse    302.109k (± 0.2%) i/s -      1.517M in   5.022546s
           JSON.generate    569.705k (± 0.2%) i/s -      2.849M in   5.001462s
    ```
    
    ### After
    ```
    Warming up --------------------------------------
              JSON.parse    35.109k i/100ms
           JSON.generate    57.611k i/100ms
    Calculating -------------------------------------
              JSON.parse    343.547k (± 0.3%) i/s -      1.720M in   5.007637s
           JSON.generate    568.668k (± 0.3%) i/s -      2.881M in   5.065486s
    ```
    
    ### Test code
    ```ruby
    require 'benchmark/ips'
    require 'json'
    
    json =<<-EOF
    {
      "$id": "https://example.com/person.schema.json",
      "$schema": "https://json-schema.org/draft/2020-12/schema",
      "title": "Person",
      "type": "object",
      "properties": {
        "firstName": {
          "type": "string",
          "description": "The person's first name."
        },
        "lastName": {
          "type": "string",
          "description": "The person's last name."
        },
        "age": {
          "description": "Age in years which must be equal to or greater than zero.",
          "type": "integer",
          "minimum": 0
        }
      }
    }
    EOF
    
    Benchmark.ips do |x|
      x.report('JSON.parse') { JSON.parse(json, symbolize_names: true) }
    
      data = JSON.parse(json, symbolize_names: true)
      x.report('JSON.generate') { JSON.generate(json, indent: "\t") }
    end
    ```
    Watson1978 committed Oct 16, 2021
    Configuration menu
    Copy the full SHA
    8daf46e View commit details
    Browse the repository at this point in the history