ohler55 / oj

Optimized JSON
http://www.ohler.com/oj
MIT License
3.12k stars 250 forks source link

Refactor in where obtain hash keys by using common function #887

Closed Watson1978 closed 1 year ago

Watson1978 commented 1 year ago

Refactor in where obtain hash keys by using common function. However, it might have the margin of error for the overhead of calling oj_calc_hash_key().

before after result
Oj.dump 449.521k 447.738k 0.996x

Environment

Before

Warming up --------------------------------------
             Oj.load    44.738k i/100ms
Calculating -------------------------------------
             Oj.load    449.521k (± 0.9%) i/s -      6.755M in  15.029388s

After

Warming up --------------------------------------
             Oj.load    44.117k i/100ms
Calculating -------------------------------------
             Oj.load    447.738k (± 0.3%) i/s -      6.750M in  15.075672s

Test code

require 'bundler/inline'
gemfile do
  source 'https://rubygems.org'
  gem 'benchmark-ips'
  gem 'oj'
end

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.warmup = 5
  x.time = 15

  x.report('Oj.load') { Oj.load(json, mode: :compat) }
end