Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve JSON_parse_string() performance
`rb_str_resize()` might reallocate the heap area in String object and it might cause to downgrade the performance. When use `rb_str_new()` instead of `rb_str_buf_new()`, it does not allocate extra heap area internally. After then, it will not need to resize the object. This patch will be 36.6 % faster for parsing string value. ## Before ``` $ ruby bench.rb Warming up -------------------------------------- json 14.000 i/100ms Calculating ------------------------------------- json 144.882 (± 1.4%) i/s - 728.000 in 5.025682s ** Memory usage 1105348 ``` ## After ``` $ ruby bench.rb Warming up -------------------------------------- json 19.000 i/100ms Calculating ------------------------------------- json 197.928 (± 4.5%) i/s - 988.000 in 5.005914s ** Memory usage 1138428 ``` ## Test code ``` require 'json' require 'objspace' 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 puts "** Memory usage" p ObjectSpace.memsize_of_all String ```
- Loading branch information