mirror of
https://github.com/ruby/ruby.git
synced 2025-09-15 08:33:58 +02:00
![]() Fix: https://github.com/ruby/json/issues/655 For very small documents, the biggest performance gap with alternatives is that the API impose that we allocate the `State` object. In a real world app this doesn't make much of a difference, but when running in a micro-benchmark this doubles the allocations, causing twice the amount of GC runs, making us look bad. However, unless we have to call a `to_json` method, the `State` object isn't visible, so with some refactoring, we can elude that allocation entirely. Instead we allocate the State internal struct on the stack, and if we need to call a `to_json` method, we allocate the `State` and spill the struct on the heap. As a result, `JSON.generate` is now as fast as re-using a `State` instance, as long as only primitives are generated. Before: ``` == Encoding small mixed (34 bytes) ruby 3.3.4 (2024-07-09 revision |
||
---|---|---|
.. | ||
-test- | ||
cgi/escape | ||
continuation | ||
coverage | ||
date | ||
digest | ||
erb/escape | ||
etc | ||
fcntl | ||
fiddle | ||
io | ||
json | ||
monitor | ||
objspace | ||
openssl | ||
pathname | ||
psych | ||
pty | ||
rbconfig/sizeof | ||
ripper | ||
rubyvm | ||
socket | ||
stringio | ||
strscan | ||
win32 | ||
win32ole | ||
zlib | ||
.document | ||
extmk.rb | ||
Setup | ||
Setup.atheos | ||
Setup.nt |