summaryrefslogtreecommitdiff
path: root/ext/json/generator
diff options
context:
space:
mode:
authorJean Boussier <[email protected]>2024-10-29 11:00:16 +0100
committerHiroshi SHIBATA <[email protected]>2024-11-01 13:04:24 +0900
commit59eebeca02325861dd16452c9b85f4920bccd84f ()
treedcc4bd7fde4a87782599354843ea788870285296 /ext/json/generator
parentd329896fb590de96832d522b475404cae1279767 (diff)
[ruby/json] Allocate the initial generator buffer on the stack
Ref: https://.com/ruby/json/issues/655 Followup: https://.com/ruby/json/issues/657 Assuming the generator might be used for fairly small documents we can start with a reasonable buffer size of the stack, and if we outgrow it, we can spill on the heap. In a way this is optimizing for micro-benchmarks, but there are valid use case for fiarly small JSON document in actual real world scenarios, so trashing the GC less in such case make sense. Before: ``` ruby 3.3.4 (2024-07-09 revision https://.com/ruby/json/commit/be1089c8ec) +YJIT [arm64-darwin23] Warming up -------------------------------------- Oj 518.700k i/100ms JSON reuse 483.370k i/100ms Calculating ------------------------------------- Oj 5.722M (± 1.8%) i/s (174.76 ns/i) - 29.047M in 5.077823s JSON reuse 5.278M (± 1.5%) i/s (189.46 ns/i) - 26.585M in 5.038172s Comparison: Oj: 5722283.8 i/s JSON reuse: 5278061.7 i/s - 1.08x slower ``` After: ``` ruby 3.3.4 (2024-07-09 revision https://.com/ruby/json/commit/be1089c8ec) +YJIT [arm64-darwin23] Warming up -------------------------------------- Oj 517.837k i/100ms JSON reuse 548.871k i/100ms Calculating ------------------------------------- Oj 5.693M (± 1.6%) i/s (175.65 ns/i) - 28.481M in 5.004056s JSON reuse 5.855M (± 1.2%) i/s (170.80 ns/i) - 29.639M in 5.063004s Comparison: Oj: 5692985.6 i/s JSON reuse: 5854857.9 i/s - 1.03x faster ``` https://.com/ruby/json/commit/fe607f4806
-rw-r--r--ext/json/generator/generator.c3
-rw-r--r--ext/json/generator/generator.h1
2 files changed, 3 insertions, 1 deletions
@@ -954,8 +954,9 @@ static VALUE cState_partial_generate(VALUE self, VALUE obj, void (*func)(FBuffer
{
GET_STATE(self);
FBuffer buffer = {0};
- fbuffer_init(&buffer, state->buffer_initial_length);
struct generate_json_data data = {
.buffer = &buffer,
@@ -54,6 +54,7 @@ typedef struct JSON_Generator_StateStruct {
JSON_Generator_State *state; \
GET_STATE_TO(self, state)
static VALUE mHash_to_json(int argc, VALUE *argv, VALUE self);
static VALUE mArray_to_json(int argc, VALUE *argv, VALUE self);
#ifdef RUBY_INTEGER_UNIFICATION