JSON.dump looks terrible on micro-benchmarks because the way it
handles arguments is quite allocation heavy compared to the actual
JSON generation work.
Profiling the small hash benchmarked show 14% of time spent in Array#compact
and 34% time spent in JSON::Ext::GeneratorState.new. Only 41% in the
actual generate function.
By micro-optimizing JSON.dump, it can look much better:
Optimize JSON.dump argument parsing
JSON.dumplooks terrible on micro-benchmarks because the way ithandles arguments is quite allocation heavy compared to the actual
JSON generation work.
Profiling the
small hashbenchmarked show 14% of time spent inArray#compactand
34%time spent inJSON::Ext::GeneratorState.new. Only41%in theactual
generatefunction.By micro-optimizing
JSON.dump, it can look much better:Before:
After:
Now profiles of the
small hashbenchmark show 44% ingenerateand45%inGeneratorStateallocation.