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 Oj.dump performance #674

Merged
merged 1 commit into from Jul 25, 2021
Merged

Commits on Jul 25, 2021

  1. Improve Oj.dump performance

    This patch uses standard C library to copy the string
    because copying one byte at a time is slow.
    
    This patch will improve `Oj.dump` performance as following.
    
    -                | before   | after    | result
    --               | --       | --       | --
    Oj.dump          | 689.236k | 1.853M   | 2.69x
    Oj.dump (compat) | 476.107k | 827.446k | 1.74x
    Oj.dump (rails)  | 464.545k | 644.494k | 1.39x
    
    ### Environment
    - MacBook Air (M1, 2020)
    - macOS 12.0 beta 3
    - Apple M1
    - Ruby 3.0.2
    
    ### Before
    ```
    Warming up --------------------------------------
                 Oj.dump    69.210k i/100ms
        Oj.dump (compat)    47.123k i/100ms
         Oj.dump (rails)    45.911k i/100ms
    Calculating -------------------------------------
                 Oj.dump    689.236k (± 0.2%) i/s -      3.460M in   5.020801s
        Oj.dump (compat)    476.107k (± 0.9%) i/s -      2.403M in   5.048128s
         Oj.dump (rails)    464.545k (± 0.9%) i/s -      2.341M in   5.040711s
    ```
    
    ### After
    ```
    Warming up --------------------------------------
                 Oj.dump   187.096k i/100ms
        Oj.dump (compat)    82.879k i/100ms
         Oj.dump (rails)    64.371k i/100ms
    Calculating -------------------------------------
                 Oj.dump      1.853M (± 0.3%) i/s -      9.355M in   5.049406s
        Oj.dump (compat)    827.446k (± 0.2%) i/s -      4.144M in   5.008145s
         Oj.dump (rails)    644.494k (± 0.2%) i/s -      3.283M in   5.093814s
    ```
    
    ### 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 25, 2021
    Copy the full SHA
    f3640e0 View commit details
    Browse the repository at this point in the history