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

Improve performance of Oj.dump with compat/rails mode #675

Merged
merged 1 commit into from Aug 2, 2021

Commits on Jul 26, 2021

  1. Improve performance of Oj.dump with compat/rails mode

    This patch introduces `oj_hash_has_key()` (same as rb_hash_has_key)
    to reduce `rb_funcall()` calling because it has a overhead.
    
    This patch will improve `Oj.dump` performance as following.
    
    −               | before   | after    | result
    --               | --       | --       | --
    Oj.dump          | 1.949M   | 1.966M   | −
    Oj.dump (compat) | 850.154k | 1.198M   | 1.41x
    Oj.dump (rails)  | 657.383k | 840.051k | 1.28x
    
    ### Environment
    - MacBook Air (M1, 2020)
    - macOS 12.0 beta 3
    - Apple M1
    - Ruby 3.0.2
    
    ### Before
    ```
    Warming up --------------------------------------
                 Oj.dump   198.379k i/100ms
        Oj.dump (compat)    86.466k i/100ms
         Oj.dump (rails)    66.760k i/100ms
    Calculating -------------------------------------
                 Oj.dump      1.949M (± 0.3%) i/s -      9.919M in   5.088487s
        Oj.dump (compat)    850.154k (± 0.3%) i/s -      4.323M in   5.085367s
         Oj.dump (rails)    657.383k (± 0.4%) i/s -      3.338M in   5.077802s
    ```
    
    ### After
    ```
    Warming up --------------------------------------
                 Oj.dump   198.297k i/100ms
        Oj.dump (compat)   120.402k i/100ms
         Oj.dump (rails)    84.204k i/100ms
    Calculating -------------------------------------
                 Oj.dump      1.966M (± 0.3%) i/s -      9.915M in   5.044305s
        Oj.dump (compat)      1.198M (± 0.2%) i/s -      6.020M in   5.026524s
         Oj.dump (rails)    840.051k (± 0.1%) i/s -      4.210M in   5.011848s
    ```
    
    ### Test code
    ```ruby
    require 'benchmark/ips'
    require 'oj'
    
    data = {
      'short_string': 'a' * 50,
      'long_string': 'b' * 255,
      'utf8_string': 'あいうえお' * 10
    }
    
    Benchmark.ips do |x|
      x.report('Oj.dump') { Oj.dump(data) }
      x.report('Oj.dump (compat)') { Oj.dump(data, mode: :compat) }
      x.report('Oj.dump (rails)') { Oj.dump(data, mode: :rails) }
    end
    ```
    Watson1978 committed Jul 26, 2021
    Copy the full SHA
    1b8a52f View commit details
    Browse the repository at this point in the history