json icon indicating copy to clipboard operation
json copied to clipboard

Optimize JSON::Pure::Generator by 2x-4x for simple options cases

Open eregon opened this issue 1 year ago • 1 comments

* 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

eregon avatar May 09 '24 12:05 eregon

@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).

eregon avatar Jun 03 '24 10:06 eregon

Rebased and CI should be green

eregon avatar Oct 08 '24 20:10 eregon