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 parsing option args #683

Merged
merged 1 commit into from Aug 7, 2021

Commits on Aug 7, 2021

  1. Optimize parging option args

    It looked up one by one to make sure that the supported options were included in passed options.
    This approach of looking up the passed hash each time has an overhead.
    
    This patch will extract key and value from the passed options using `rb_hash_foreach()` and check if they are supported or not.
    
    −               | before   | after    | result
    --               | --       | --       | --
    Oj.load          | 381.671k | 419.917k | 1.10x
    Oj.dump          | 507.221k | 578.877k | 1.14x
    
    ### Environment
    - MacBook Air (M1, 2020)
    - macOS 12.0 beta 3
    - Apple M1
    - Ruby 3.0.2
    
    ### Before
    ```
    Warming up --------------------------------------
                 Oj.load    38.287k i/100ms
                 Oj.dump    51.076k i/100ms
    Calculating -------------------------------------
                 Oj.load    381.671k (± 0.6%) i/s -      1.914M in   5.015868s
                 Oj.dump    507.221k (± 0.5%) i/s -      2.554M in   5.035014s
    ```
    
    ### After
    ```
    Warming up --------------------------------------
                 Oj.load    42.352k i/100ms
                 Oj.dump    58.022k i/100ms
    Calculating -------------------------------------
                 Oj.load    419.917k (± 0.5%) i/s -      2.118M in   5.043048s
                 Oj.dump    578.877k (± 0.4%) i/s -      2.901M in   5.011700s
    ```
    
    ### Test code
    ```ruby
    require 'benchmark/ips'
    require 'oj'
    
    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('Oj.load') { Oj.load(json, symbol_keys: true) }
    
      data = Oj.load(json, symbol_keys: true)
      x.report('Oj.dump') { Oj.dump(json, mode: :compat) }
    end
    ```
    Watson1978 committed Aug 7, 2021
    Copy the full SHA
    662ede1 View commit details
    Browse the repository at this point in the history