json
json copied to clipboard
Optimize JSON::Pure::Generator by 2x-4x for simple options cases
* ruby --yjit benchmarks/bench.rb dump pure ruby 3.3.1 (2024-04-23 revision c56cd86388) +YJIT [x86_64-linux]
Before: JSON.dump(obj) 604.604 (± 0.3%) i/s (1.65 ms/i) - 3.060k in 5.061200s
After: JSON.dump(obj) 2.531k (± 0.4%) i/s (395.14 μs/i) - 12.801k in 5.058326s
* ruby benchmarks/bench.rb dump pure
truffleruby 24.1.0-dev-a8ebb51b, like ruby 3.2.2, Oracle GraalVM JVM [x86_64-linux]
Before: JSON.dump(obj) 3.728k (± 9.4%) i/s (268.26 μs/i) - 18.559k in 5.068915s
After: JSON.dump(obj) 7.835k (± 8.5%) i/s (127.63 μs/i) - 39.004k in 5.031116s
Using the benchmark from https://github.com/flori/json/pull/580
BTW, the C extension, with YJIT gives:
JSON::Ext::Generator
ruby 3.3.1 (2024-04-23 revision c56cd86388) +YJIT [x86_64-linux]
JSON.dump(obj) 7.402k (± 1.0%) i/s (135.10 μs/i) - 37.485k in 5.064832s
@hsbt Could you review this as well? :pray:
cc @byroot I mentioned this to you at RubyKaigi. Might also be interesting to @maximecb (this is a pure-Ruby JSON parser, this PR is optimizing the pure-Ruby JSON generator).
Rebased and CI should be green