diff options
author | Watson <watson1978@gmail.com> | 2018-02-27 22:40:58 +0900 |
---|---|---|
committer | Hiroshi SHIBATA <hsbt@ruby-lang.org> | 2020-07-01 18:47:51 +0900 |
commit | cb3e62511c7c7a7d568342d82b641e491ce589e1 (patch) | |
tree | a9eec8af1f9780c2574171508ee1e2cd19256813 /ext/json/parser/parser.rl | |
parent | 7d8ce96de6b977cebf6d9698235a8de8f8d808e1 (diff) |
[flori/json] Use frozen string for hash key
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()`.
```
Warming up --------------------------------------
json 14.000 i/100ms
Calculating -------------------------------------
json 148.844 (± 1.3%) i/s - 756.000 in 5.079969s
```
```
Warming up --------------------------------------
json 16.000 i/100ms
Calculating -------------------------------------
json 165.608 (± 1.8%) i/s - 832.000 in 5.025367s
```
```
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
```
https://github.com/flori/json/commit/18292c0c1d
Diffstat (limited to 'ext/json/parser/parser.rl')
-rw-r--r-- | ext/json/parser/parser.rl | 1 |
1 files changed, 1 insertions, 0 deletions
diff --git a/ext/json/parser/parser.rl b/ext/json/parser/parser.rl index 6b38bb283a..00a35bded4 100644 --- a/ext/json/parser/parser.rl +++ b/ext/json/parser/parser.rl @@ -138,6 +138,7 @@ static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions, fhold; fbreak; } else { if (NIL_P(json->object_class)) { + OBJ_FREEZE(last_name); rb_hash_aset(*result, last_name, v); } else { rb_funcall(*result, i_aset, 2, last_name, v); |