diff options
author | Jean Boussier <[email protected]> | 2024-10-29 10:23:25 +0100 |
---|---|---|
committer | Hiroshi SHIBATA <[email protected]> | 2024-10-30 10:13:48 +0900 |
commit | 5d176436ce4683df57decab6e686c319bc4c53cd () | |
tree | 587f7945dc945b5a29ee92b9b92e820b43f61814 /ext/json/generator | |
parent | 926b4e2f4052df7cbf4ef0c24c26e0e44cb5bb2b (diff) |
[ruby/json] Allocate the FBuffer struct on the stack
Ref: https://.com/ruby/json/issues/655 The actual buffer is still on the heap, but this saves a pair of malloc/free. This helps a lot on micro-benchmarks Before: ``` ruby 3.3.4 (2024-07-09 revision https://.com/ruby/json/commit/be1089c8ec) +YJIT [arm64-darwin23] Warming up -------------------------------------- Oj 531.598k i/100ms JSON reuse 417.666k i/100ms Calculating ------------------------------------- Oj 5.735M (± 1.3%) i/s (174.35 ns/i) - 28.706M in 5.005900s JSON reuse 4.604M (± 1.4%) i/s (217.18 ns/i) - 23.389M in 5.080779s Comparison: Oj: 5735475.6 i/s JSON reuse: 4604380.3 i/s - 1.25x slower ``` After: ``` 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 ``` Bench: ```ruby require 'benchmark/ips' require 'oj' require 'json' json_encoder = JSON::State.new(JSON.dump_default_options) test_data = [1, "string", { a: 1, b: 2 }, [3, 4, 5]] Oj.default_options = Oj.default_options.merge(mode: :compat) Benchmark.ips do |x| x.config(time: 5, warmup: 2) x.report("Oj") do Oj.dump(test_data) end x.report("JSON reuse") do json_encoder.generate(test_data) end x.compare!(order: :baseline) end ``` https://.com/ruby/json/commit/72110f7992
-rw-r--r-- | ext/json/generator/generator.c | 17 | ||||
-rw-r--r-- | ext/json/generator/generator.h | 9 |
2 files changed, 9 insertions, 17 deletions
@@ -911,15 +911,6 @@ static void generate_json(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *s } } -static FBuffer *cState_prepare_buffer(VALUE self) -{ - FBuffer *buffer; - GET_STATE(self); - buffer = fbuffer_alloc(state->buffer_initial_length); - - return buffer; -} - struct generate_json_data { FBuffer *buffer; VALUE vstate; @@ -948,18 +939,20 @@ static VALUE generate_json_rescue(VALUE d, VALUE exc) static VALUE cState_partial_generate(VALUE self, VALUE obj) { - FBuffer *buffer = cState_prepare_buffer(self); GET_STATE(self); struct generate_json_data data = { - .buffer = buffer, .vstate = self, .state = state, .obj = obj }; rb_rescue(generate_json_try, (VALUE)&data, generate_json_rescue, (VALUE)&data); - return fbuffer_to_s(buffer); } /* @@ -55,16 +55,16 @@ typedef struct JSON_Generator_StateStruct { GET_STATE_TO(self, state) #define GENERATE_JSON(type) \ - FBuffer *buffer; \ VALUE Vstate; \ JSON_Generator_State *state; \ \ rb_scan_args(argc, argv, "01", &Vstate); \ Vstate = cState_from_state_s(cState, Vstate); \ TypedData_Get_Struct(Vstate, JSON_Generator_State, &JSON_Generator_State_type, state); \ - buffer = cState_prepare_buffer(Vstate); \ - generate_json_##type(buffer, Vstate, state, self); \ - return fbuffer_to_s(buffer) static VALUE mHash_to_json(int argc, VALUE *argv, VALUE self); static VALUE mArray_to_json(int argc, VALUE *argv, VALUE self); @@ -122,7 +122,6 @@ static VALUE cState_script_safe(VALUE self); static VALUE cState_script_safe_set(VALUE self, VALUE depth); static VALUE cState_strict(VALUE self); static VALUE cState_strict_set(VALUE self, VALUE strict); -static FBuffer *cState_prepare_buffer(VALUE self); static const rb_data_type_t JSON_Generator_State_type; |