Skip to content

Commit e660b61

Browse files
committed
parser.rl: initialize the buffer with 512B on the stack
This very significantly reduce the overhead on smaller benchmarks ``` == Parsing small hash (65 bytes) ruby 3.3.4 (2024-07-09 revision be1089c8ec) [arm64-darwin23] Warming up -------------------------------------- json 304.417k i/100ms oj 219.431k i/100ms oj strict 254.532k i/100ms Oj::Parser 431.309k i/100ms rapidjson 281.703k i/100ms Calculating ------------------------------------- json 3.046M (± 0.1%) i/s (328.25 ns/i) - 15.525M in 5.096243s oj 2.225M (± 0.2%) i/s (449.50 ns/i) - 11.191M in 5.030429s oj strict 2.553M (± 0.5%) i/s (391.75 ns/i) - 12.981M in 5.085538s Oj::Parser 4.280M (± 0.8%) i/s (233.64 ns/i) - 21.565M in 5.038834s rapidjson 2.826M (± 0.3%) i/s (353.83 ns/i) - 14.367M in 5.083480s Comparison: json: 3046420.8 i/s Oj::Parser: 4280132.7 i/s - 1.40x faster rapidjson: 2826209.4 i/s - 1.08x slower oj strict: 2552619.7 i/s - 1.19x slower oj: 2224670.7 i/s - 1.37x slower ```
1 parent 278f105 commit e660b61

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

ext/json/ext/parser/parser.c

+16-10
Original file line numberDiff line numberDiff line change
@@ -1919,17 +1919,20 @@ static VALUE cParser_parse(VALUE self)
19191919
VALUE result = Qnil;
19201920
GET_PARSER;
19211921

1922+
char stack_buffer[FBUFFER_STACK_SIZE];
1923+
fbuffer_stack_init(&json->fbuffer, FBUFFER_INITIAL_LENGTH_DEFAULT, stack_buffer, FBUFFER_STACK_SIZE);
19221924

1923-
#line 1924 "parser.c"
1925+
1926+
#line 1927 "parser.c"
19241927
{
19251928
cs = JSON_start;
19261929
}
19271930

1928-
#line 823 "parser.rl"
1931+
#line 826 "parser.rl"
19291932
p = json->source;
19301933
pe = p + json->len;
19311934

1932-
#line 1933 "parser.c"
1935+
#line 1936 "parser.c"
19331936
{
19341937
if ( p == pe )
19351938
goto _test_eof;
@@ -1973,7 +1976,7 @@ cs = 0;
19731976
if ( ++p == pe )
19741977
goto _test_eof10;
19751978
case 10:
1976-
#line 1977 "parser.c"
1979+
#line 1980 "parser.c"
19771980
switch( (*p) ) {
19781981
case 13: goto st10;
19791982
case 32: goto st10;
@@ -2062,7 +2065,7 @@ case 9:
20622065
_out: {}
20632066
}
20642067

2065-
#line 826 "parser.rl"
2068+
#line 829 "parser.rl"
20662069

20672070
if (cs >= JSON_first_final && p == pe) {
20682071
return result;
@@ -2082,17 +2085,20 @@ static VALUE cParser_m_parse(VALUE klass, VALUE source, VALUE opts)
20822085
JSON_Parser *json = &parser;
20832086
parser_init(json, source, opts);
20842087

2088+
char stack_buffer[FBUFFER_STACK_SIZE];
2089+
fbuffer_stack_init(&json->fbuffer, FBUFFER_INITIAL_LENGTH_DEFAULT, stack_buffer, FBUFFER_STACK_SIZE);
2090+
20852091

2086-
#line 2087 "parser.c"
2092+
#line 2093 "parser.c"
20872093
{
20882094
cs = JSON_start;
20892095
}
20902096

2091-
#line 846 "parser.rl"
2097+
#line 852 "parser.rl"
20922098
p = json->source;
20932099
pe = p + json->len;
20942100

2095-
#line 2096 "parser.c"
2101+
#line 2102 "parser.c"
20962102
{
20972103
if ( p == pe )
20982104
goto _test_eof;
@@ -2136,7 +2142,7 @@ cs = 0;
21362142
if ( ++p == pe )
21372143
goto _test_eof10;
21382144
case 10:
2139-
#line 2140 "parser.c"
2145+
#line 2146 "parser.c"
21402146
switch( (*p) ) {
21412147
case 13: goto st10;
21422148
case 32: goto st10;
@@ -2225,7 +2231,7 @@ case 9:
22252231
_out: {}
22262232
}
22272233

2228-
#line 849 "parser.rl"
2234+
#line 855 "parser.rl"
22292235

22302236
if (cs >= JSON_first_final && p == pe) {
22312237
return result;

ext/json/ext/parser/parser.rl

+6
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,9 @@ static VALUE cParser_parse(VALUE self)
819819
VALUE result = Qnil;
820820
GET_PARSER;
821821

822+
char stack_buffer[FBUFFER_STACK_SIZE];
823+
fbuffer_stack_init(&json->fbuffer, FBUFFER_INITIAL_LENGTH_DEFAULT, stack_buffer, FBUFFER_STACK_SIZE);
824+
822825
%% write init;
823826
p = json->source;
824827
pe = p + json->len;
@@ -842,6 +845,9 @@ static VALUE cParser_m_parse(VALUE klass, VALUE source, VALUE opts)
842845
JSON_Parser *json = &parser;
843846
parser_init(json, source, opts);
844847

848+
char stack_buffer[FBUFFER_STACK_SIZE];
849+
fbuffer_stack_init(&json->fbuffer, FBUFFER_INITIAL_LENGTH_DEFAULT, stack_buffer, FBUFFER_STACK_SIZE);
850+
845851
%% write init;
846852
p = json->source;
847853
pe = p + json->len;

0 commit comments

Comments
 (0)