Skip to content

Commit

Permalink
bench: Avoid allocating an extra hash when not necessary (#949)
Browse files Browse the repository at this point in the history
`JSON.parse(@JSON, symbolize_names: @Symbolize)` allocates an extra hash,
which isn't necessary when `symbolize` is `false`.

In the grand scheme of things, it's not a big deal, but on this sort
of micro-benchmarks that can easily accound for a huge part of the runtime
just because of the increased GC pressure.
  • Loading branch information
byroot authored Jan 3, 2025
1 parent 7388e5b commit 562e68a
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion test/perf_strict.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ def capture_error(tag, orig, load_key, dump_key, &blk)
puts 'Strict Parse Performance'
perf = Perf.new()
unless @failed.key?('JSON::Ext')
perf.add('JSON::Ext', 'parse') { JSON.parse(@json, symbolize_names: @symbolize) }
if @symbolize
perf.add('JSON::Ext', 'parse') { JSON.parse(@json, symbolize_names: true) }
else
perf.add('JSON::Ext', 'parse') { JSON.parse(@json) }
end
perf.before('JSON::Ext') { JSON.parser = JSON::Ext::Parser }
end
unless @failed.key?('Oj:strict')
Expand Down

0 comments on commit 562e68a

Please sign in to comment.