[flori/json] Convert Hash object using rb_hash_foreach()
To convert Hash convert, this part was using following pseudo code
obj.keys.each do |key|
value = obj[key]
...
end
and rb_funcall() was called for obj.keys.
It might be slightly heavy to call the Ruby method.
This patch will iterate to convert Hash object about key/value using rb_hash_foreach() Ruby API instead of rb_funcall().
$ ruby bench_json_generate.rb
Warming up --------------------------------------
json 55.000 i/100ms
Calculating -------------------------------------
json 558.501 (± 1.1%) i/s - 2.805k in 5.022986s
$ ruby bench_json_generate.rb
Warming up --------------------------------------
json 65.000 i/100ms
Calculating -------------------------------------
json 659.576 (± 1.5%) i/s - 3.315k in 5.027127s
require 'json'
require 'benchmark/ips'
obj = []
1000.times do |i|
obj << {
"id" => i,
:age => 42,
}
end
Benchmark.ips do |x|
x.report "json" do |iter|
count = 0
while count < iter
JSON.generate(obj)
count += 1
end
end
end
[flori/json] Convert Hash object using rb_hash_foreach()
To convert Hash convert, this part was using following pseudo code
and
rb_funcall()
was called forobj.keys
.It might be slightly heavy to call the Ruby method.
This patch will iterate to convert Hash object about key/value using
rb_hash_foreach()
Ruby API instead ofrb_funcall()
.https://github.com/flori/json/commit/a73323dc5e