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 JSON.parse() performance #345
Closed
Closed
+69
−67
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Watson1978
force-pushed
the
parse_string
branch
from
February 25, 2018 13:41
bf0e97c
to
c451dd8
Compare
Watson1978
changed the title
Improve JSON_parse_string() performance
Improve JSON.parse() performance
Feb 27, 2018
When use non-frozen string for hash key with `rb_hash_aset()`, it will duplicate and freeze it internally. To avoid duplicate and freeze, this patch will give a frozen string in `rb_hash_aset()`. ## Before ``` Warming up -------------------------------------- json 14.000 i/100ms Calculating ------------------------------------- json 148.844 (± 1.3%) i/s - 756.000 in 5.079969s ``` ### After ``` Warming up -------------------------------------- json 16.000 i/100ms Calculating ------------------------------------- json 165.608 (± 1.8%) i/s - 832.000 in 5.025367s ``` ### Test code ``` require 'json' require 'securerandom' require 'benchmark/ips' obj = [] 1000.times do |i| obj << { "id": i, "uuid": SecureRandom.uuid, "created_at": Time.now } end json = obj.to_json Benchmark.ips do |x| x.report "json" do |iter| count = 0 while count < iter JSON.parse(json) count += 1 end end end ```
@flori Anything preventing this from getting merged? It gives a nice 15% performance increase. |
@Watson1978 Can you rebase this from the current master? |
This has been merged, thanks @Watson1978 for the PR 👍 |
Thanks! |
Watson1978
added a commit
to Watson1978/oj
that referenced
this pull request
Aug 5, 2021
When use non-frozen string as hash key with rb_hash_aset(), it will duplicate and freeze the string internally. ```c static int hash_aset_str(st_data_t *key, st_data_t *val, struct update_arg *arg, int existing) { if (!existing && !RB_OBJ_FROZEN(*key)) { *key = rb_hash_key_str(*key); } return hash_aset(key, val, arg, existing); } ``` Refer: https://github.com/ruby/ruby/blob/bda56a03a625793cb3fd110458c3f7323d73705e/hash.c#L2890-L2897 To avoid duplicate and freeze, this patch will give a frozen string in rb_hash_aset(). FYI) If you use string as hash key, hash object always might have frozen string as key. ``` irb(main):001:0> hash = { "foo" => 42, bar: 55 } => {"foo"=>42, :bar=>55} irb(main):002:0> hash.keys[0].frozen? => true irb(main):003:0> hash.keys[1].frozen? => true ``` This patch has same approch with flori/json#345 − | before | after | result -- | -- | -- | -- Oj.load | 335.122k | 422.081k | 1.26x ### Environment - MacBook Air (M1, 2020) - macOS 12.0 beta 3 - Apple M1 - Ruby 3.0.2 ### Before ``` Warming up -------------------------------------- Oj.load 33.829k i/100ms Calculating ------------------------------------- Oj.load 335.122k (± 0.9%) i/s - 1.691M in 5.047682s ``` ### After ``` Warming up -------------------------------------- Oj.load 42.573k i/100ms Calculating ------------------------------------- Oj.load 422.081k (± 0.5%) i/s - 2.129M in 5.043373s ``` ### 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) } end ```
ohler55
pushed a commit
to ohler55/oj
that referenced
this pull request
Aug 5, 2021
When use non-frozen string as hash key with rb_hash_aset(), it will duplicate and freeze the string internally. ```c static int hash_aset_str(st_data_t *key, st_data_t *val, struct update_arg *arg, int existing) { if (!existing && !RB_OBJ_FROZEN(*key)) { *key = rb_hash_key_str(*key); } return hash_aset(key, val, arg, existing); } ``` Refer: https://github.com/ruby/ruby/blob/bda56a03a625793cb3fd110458c3f7323d73705e/hash.c#L2890-L2897 To avoid duplicate and freeze, this patch will give a frozen string in rb_hash_aset(). FYI) If you use string as hash key, hash object always might have frozen string as key. ``` irb(main):001:0> hash = { "foo" => 42, bar: 55 } => {"foo"=>42, :bar=>55} irb(main):002:0> hash.keys[0].frozen? => true irb(main):003:0> hash.keys[1].frozen? => true ``` This patch has same approch with flori/json#345 − | before | after | result -- | -- | -- | -- Oj.load | 335.122k | 422.081k | 1.26x ### Environment - MacBook Air (M1, 2020) - macOS 12.0 beta 3 - Apple M1 - Ruby 3.0.2 ### Before ``` Warming up -------------------------------------- Oj.load 33.829k i/100ms Calculating ------------------------------------- Oj.load 335.122k (± 0.9%) i/s - 1.691M in 5.047682s ``` ### After ``` Warming up -------------------------------------- Oj.load 42.573k i/100ms Calculating ------------------------------------- Oj.load 422.081k (± 0.5%) i/s - 2.129M in 5.043373s ``` ### 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) } end ```
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When use non-frozen string as hash key with
rb_hash_aset()
, it will duplicate and freeze the string internally.To avoid duplicate and freeze, this patch will give a frozen string in
rb_hash_aset()
.FYI)
If you use string as hash key, hash object always might have frozen string as key.
This patches will be 15 % faster.
Environment
Before
After
Test code