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

Use rb_enc_get() to retrieve encoding type #685

Merged
merged 1 commit into from Aug 8, 2021

Commits on Aug 8, 2021

  1. Use rb_enc_get() to retrieve encoding type

    To reduce overhead, this patch use `rb_enc_get()` instead.
    
    −               | before   | after    | result
    --               | --       | --       | --
    Oj.dump          | 1.319M   | 1.336M   | -
    Oj.dump (compat) | 948.393k | 1.125M   | 1.19x
    Oj.dump (rails)  | 815.509k | 943.996k | 1.16x
    
    ### Environment
    - MacBook Air (M1, 2020)
    - macOS 12.0 beta 4
    - Apple M1
    - Ruby 3.0.2
    
    ### Before
    ```
    Warming up --------------------------------------
                 Oj.dump   131.400k i/100ms
        Oj.dump (compat)    94.239k i/100ms
         Oj.dump (rails)    79.788k i/100ms
    Calculating -------------------------------------
                 Oj.dump      1.319M (± 0.7%) i/s -      6.701M in   5.080504s
        Oj.dump (compat)    948.393k (± 0.9%) i/s -      4.806M in   5.068098s
         Oj.dump (rails)    815.509k (± 0.8%) i/s -      4.149M in   5.087955s
    ```
    
    ### After
    ```
    Warming up --------------------------------------
                 Oj.dump   133.596k i/100ms
        Oj.dump (compat)   113.563k i/100ms
         Oj.dump (rails)    95.233k i/100ms
    Calculating -------------------------------------
                 Oj.dump      1.336M (± 1.0%) i/s -      6.680M in   5.000748s
        Oj.dump (compat)      1.125M (± 0.9%) i/s -      5.678M in   5.046279s
         Oj.dump (rails)    943.996k (± 0.8%) i/s -      4.762M in   5.044464s
    ```
    
    ### 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|
      data = Oj.load(json)
      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 Aug 8, 2021
    Copy the full SHA
    28731bb View commit details
    Browse the repository at this point in the history