[ruby/json] Ext::Parser avoid costly check on decimal_class when it is nil

Closes: https://github.com/ruby/json/pull/512

https://github.com/ruby/json/commit/d882a45d82

Co-Authored-By: lukeg <luke.gru@gmail.com>
This commit is contained in:
Jean Boussier 2024-10-18 16:03:15 +02:00 committed by Hiroshi SHIBATA
parent 18cc663aef
commit 07fc21cfad
2 changed files with 223 additions and 213 deletions

View File

@ -1136,6 +1136,7 @@ case 7:
if (cs >= JSON_float_first_final) {
VALUE mod = Qnil;
ID method_id = 0;
if (!NIL_P(json->decimal_class)) {
if (rb_respond_to(json->decimal_class, i_try_convert)) {
mod = json->decimal_class;
method_id = i_try_convert;
@ -1161,6 +1162,7 @@ case 7:
method_id = SYM2ID(rb_str_intern(name));
}
}
}
long len = p - json->memo;
fbuffer_clear(json->fbuffer);
@ -1182,7 +1184,7 @@ case 7:
#line 1186 "parser.c"
#line 1188 "parser.c"
enum {JSON_array_start = 1};
enum {JSON_array_first_final = 17};
enum {JSON_array_error = 0};
@ -1190,7 +1192,7 @@ enum {JSON_array_error = 0};
enum {JSON_array_en_main = 1};
#line 434 "parser.rl"
#line 436 "parser.rl"
static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting)
@ -1204,14 +1206,14 @@ static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *resul
*result = NIL_P(array_class) ? rb_ary_new() : rb_class_new_instance(0, 0, array_class);
#line 1208 "parser.c"
#line 1210 "parser.c"
{
cs = JSON_array_start;
}
#line 447 "parser.rl"
#line 449 "parser.rl"
#line 1215 "parser.c"
#line 1217 "parser.c"
{
if ( p == pe )
goto _test_eof;
@ -1250,7 +1252,7 @@ case 2:
goto st2;
goto st0;
tr2:
#line 411 "parser.rl"
#line 413 "parser.rl"
{
VALUE v = Qnil;
char *np = JSON_parse_value(json, p, pe, &v, current_nesting);
@ -1270,7 +1272,7 @@ st3:
if ( ++p == pe )
goto _test_eof3;
case 3:
#line 1274 "parser.c"
#line 1276 "parser.c"
switch( (*p) ) {
case 13: goto st3;
case 32: goto st3;
@ -1370,14 +1372,14 @@ case 12:
goto st3;
goto st12;
tr4:
#line 426 "parser.rl"
#line 428 "parser.rl"
{ p--; {p++; cs = 17; goto _out;} }
goto st17;
st17:
if ( ++p == pe )
goto _test_eof17;
case 17:
#line 1381 "parser.c"
#line 1383 "parser.c"
goto st0;
st13:
if ( ++p == pe )
@ -1433,7 +1435,7 @@ case 16:
_out: {}
}
#line 448 "parser.rl"
#line 450 "parser.rl"
if(cs >= JSON_array_first_final) {
return p + 1;
@ -1594,7 +1596,7 @@ static VALUE json_string_unescape(char *string, char *stringEnd, int intern, int
}
#line 1598 "parser.c"
#line 1600 "parser.c"
enum {JSON_string_start = 1};
enum {JSON_string_first_final = 8};
enum {JSON_string_error = 0};
@ -1602,7 +1604,7 @@ enum {JSON_string_error = 0};
enum {JSON_string_en_main = 1};
#line 626 "parser.rl"
#line 628 "parser.rl"
static int
@ -1623,15 +1625,15 @@ static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *resu
VALUE match_string;
#line 1627 "parser.c"
#line 1629 "parser.c"
{
cs = JSON_string_start;
}
#line 646 "parser.rl"
#line 648 "parser.rl"
json->memo = p;
#line 1635 "parser.c"
#line 1637 "parser.c"
{
if ( p == pe )
goto _test_eof;
@ -1656,7 +1658,7 @@ case 2:
goto st0;
goto st2;
tr2:
#line 613 "parser.rl"
#line 615 "parser.rl"
{
*result = json_string_unescape(json->memo + 1, p, json->parsing_name || json-> freeze, json->parsing_name && json->symbolize_names);
if (NIL_P(*result)) {
@ -1666,14 +1668,14 @@ tr2:
{p = (( p + 1))-1;}
}
}
#line 623 "parser.rl"
#line 625 "parser.rl"
{ p--; {p++; cs = 8; goto _out;} }
goto st8;
st8:
if ( ++p == pe )
goto _test_eof8;
case 8:
#line 1677 "parser.c"
#line 1679 "parser.c"
goto st0;
st3:
if ( ++p == pe )
@ -1749,7 +1751,7 @@ case 7:
_out: {}
}
#line 648 "parser.rl"
#line 650 "parser.rl"
if (json->create_additions && RTEST(match_string = json->match_string)) {
VALUE klass;
@ -1842,7 +1844,10 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
opts = Qnil;
if (argc == 2) {
opts = argv[1];
Check_Type(opts, T_HASH);
Check_Type(argv[1], T_HASH);
if (RHASH_SIZE(argv[1]) > 0) {
opts = argv[1];
}
}
if (!NIL_P(opts)) {
@ -1936,7 +1941,7 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
}
#line 1940 "parser.c"
#line 1945 "parser.c"
enum {JSON_start = 1};
enum {JSON_first_final = 10};
enum {JSON_error = 0};
@ -1944,7 +1949,7 @@ enum {JSON_error = 0};
enum {JSON_en_main = 1};
#line 848 "parser.rl"
#line 853 "parser.rl"
/*
@ -1962,16 +1967,16 @@ static VALUE cParser_parse(VALUE self)
GET_PARSER;
#line 1966 "parser.c"
#line 1971 "parser.c"
{
cs = JSON_start;
}
#line 865 "parser.rl"
#line 870 "parser.rl"
p = json->source;
pe = p + json->len;
#line 1975 "parser.c"
#line 1980 "parser.c"
{
if ( p == pe )
goto _test_eof;
@ -2005,7 +2010,7 @@ st0:
cs = 0;
goto _out;
tr2:
#line 840 "parser.rl"
#line 845 "parser.rl"
{
char *np = JSON_parse_value(json, p, pe, &result, 0);
if (np == NULL) { p--; {p++; cs = 10; goto _out;} } else {p = (( np))-1;}
@ -2015,7 +2020,7 @@ st10:
if ( ++p == pe )
goto _test_eof10;
case 10:
#line 2019 "parser.c"
#line 2024 "parser.c"
switch( (*p) ) {
case 13: goto st10;
case 32: goto st10;
@ -2104,7 +2109,7 @@ case 9:
_out: {}
}
#line 868 "parser.rl"
#line 873 "parser.rl"
if (cs >= JSON_first_final && p == pe) {
return result;

View File

@ -357,6 +357,7 @@ static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *resul
if (cs >= JSON_float_first_final) {
VALUE mod = Qnil;
ID method_id = 0;
if (!NIL_P(json->decimal_class)) {
if (rb_respond_to(json->decimal_class, i_try_convert)) {
mod = json->decimal_class;
method_id = i_try_convert;
@ -382,6 +383,7 @@ static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *resul
method_id = SYM2ID(rb_str_intern(name));
}
}
}
long len = p - json->memo;
fbuffer_clear(json->fbuffer);
@ -737,7 +739,10 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
opts = Qnil;
if (argc == 2) {
opts = argv[1];
Check_Type(opts, T_HASH);
Check_Type(argv[1], T_HASH);
if (RHASH_SIZE(argv[1]) > 0) {
opts = argv[1];
}
}
if (!NIL_P(opts)) {