In this post, the author dives deep into the performance analysis of ruby/json and Oj on micro-benchmarks to identify and fix the setup cost issue. Surprisingly, even after reusing the JSON::State object, ruby/json was still slower compared to Oj. The author introduces the concepts of stack and heap allocations and explains the benefits and limitations of each. By optimizing the stack buffer allocation in the codebase, there was a significant performance improvement observed. Additionally, the author highlights the inefficiency in long to string conversion function and suggests a more optimized approach. The attempt to use Ruby String as a buffer didn’t yield the expected results and even led to degraded performance.
https://byroot.github.io/ruby/json/2024/12/29/optimizing-ruby-json-part-4.html