From 520e0916af0fe53a5ca57269a2bae50cc60e4241 Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Wed, 9 Sep 2020 15:24:22 +0200 Subject: [PATCH] Implement a freeze: parser option If set to true all parsed objects will be immediately frozen, and strings will be deduplicated if the Ruby implementation allows it. --- ext/json/extconf.rb | 1 + ext/json/parser/extconf.rb | 25 ++++++++ ext/json/parser/parser.c | 115 +++++++++++++++++++++------------- ext/json/parser/parser.h | 1 + ext/json/parser/parser.rl | 29 ++++++++- test/json/json_parser_test.rb | 21 +++++++ 6 files changed, 147 insertions(+), 45 deletions(-) diff --git a/ext/json/extconf.rb b/ext/json/extconf.rb index 7595d58a98..8a99b6a5c8 100644 --- a/ext/json/extconf.rb +++ b/ext/json/extconf.rb @@ -1,2 +1,3 @@ require 'mkmf' + create_makefile('json') diff --git a/ext/json/parser/extconf.rb b/ext/json/parser/extconf.rb index f7360d46b2..f832b56a61 100644 --- a/ext/json/parser/extconf.rb +++ b/ext/json/parser/extconf.rb @@ -3,4 +3,29 @@ require 'mkmf' have_func("rb_enc_raise", "ruby.h") +# checking if String#-@ (str_uminus) dedupes... ' +begin + a = -(%w(t e s t).join) + b = -(%w(t e s t).join) + if a.equal?(b) + $CFLAGS << ' -DSTR_UMINUS_DEDUPE=1 ' + else + $CFLAGS << ' -DSTR_UMINUS_DEDUPE=0 ' + end +rescue NoMethodError + $CFLAGS << ' -DSTR_UMINUS_DEDUPE=0 ' +end + +# checking if String#-@ (str_uminus) directly interns frozen strings... ' +begin + s = rand.to_s.freeze + if (-s).equal?(s) && (-s.dup).equal?(s) + $CFLAGS << ' -DSTR_UMINUS_DEDUPE_FROZEN=1 ' + else + $CFLAGS << ' -DSTR_UMINUS_DEDUPE_FROZEN=0 ' + end +rescue NoMethodError + $CFLAGS << ' -DSTR_UMINUS_DEDUPE_FROZEN=0 ' +end + create_makefile 'json/ext/parser' diff --git a/ext/json/parser/parser.c b/ext/json/parser/parser.c index 6f0e4ace84..aaef53aace 100644 --- a/ext/json/parser/parser.c +++ b/ext/json/parser/parser.c @@ -97,7 +97,7 @@ static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions, i_chr, i_max_nesting, i_allow_nan, i_symbolize_names, i_object_class, i_array_class, i_decimal_class, i_key_p, i_deep_const_get, i_match, i_match_string, i_aset, i_aref, - i_leftshift, i_new, i_BigDecimal; + i_leftshift, i_new, i_BigDecimal, i_freeze, i_uminus; #line 126 "parser.rl" @@ -869,6 +869,10 @@ case 28: #line 292 "parser.rl" + if (json->freeze) { + OBJ_FREEZE(*result); + } + if (cs >= JSON_value_first_final) { return p; } else { @@ -877,7 +881,7 @@ case 28: } -#line 881 "parser.c" +#line 885 "parser.c" enum {JSON_integer_start = 1}; enum {JSON_integer_first_final = 3}; enum {JSON_integer_error = 0}; @@ -885,7 +889,7 @@ enum {JSON_integer_error = 0}; enum {JSON_integer_en_main = 1}; -#line 308 "parser.rl" +#line 312 "parser.rl" static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *result) @@ -893,15 +897,15 @@ static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *res int cs = EVIL; -#line 897 "parser.c" +#line 901 "parser.c" { cs = JSON_integer_start; } -#line 315 "parser.rl" +#line 319 "parser.rl" json->memo = p; -#line 905 "parser.c" +#line 909 "parser.c" { if ( p == pe ) goto _test_eof; @@ -935,14 +939,14 @@ case 3: goto st0; goto tr4; tr4: -#line 305 "parser.rl" +#line 309 "parser.rl" { p--; {p++; cs = 4; goto _out;} } goto st4; st4: if ( ++p == pe ) goto _test_eof4; case 4: -#line 946 "parser.c" +#line 950 "parser.c" goto st0; st5: if ( ++p == pe ) @@ -961,7 +965,7 @@ case 5: _out: {} } -#line 317 "parser.rl" +#line 321 "parser.rl" if (cs >= JSON_integer_first_final) { long len = p - json->memo; @@ -976,7 +980,7 @@ case 5: } -#line 980 "parser.c" +#line 984 "parser.c" enum {JSON_float_start = 1}; enum {JSON_float_first_final = 8}; enum {JSON_float_error = 0}; @@ -984,7 +988,7 @@ enum {JSON_float_error = 0}; enum {JSON_float_en_main = 1}; -#line 342 "parser.rl" +#line 346 "parser.rl" static int is_bigdecimal_class(VALUE obj) @@ -1005,15 +1009,15 @@ static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *resul int cs = EVIL; -#line 1009 "parser.c" +#line 1013 "parser.c" { cs = JSON_float_start; } -#line 362 "parser.rl" +#line 366 "parser.rl" json->memo = p; -#line 1017 "parser.c" +#line 1021 "parser.c" { if ( p == pe ) goto _test_eof; @@ -1071,14 +1075,14 @@ case 8: goto st0; goto tr9; tr9: -#line 336 "parser.rl" +#line 340 "parser.rl" { p--; {p++; cs = 9; goto _out;} } goto st9; st9: if ( ++p == pe ) goto _test_eof9; case 9: -#line 1082 "parser.c" +#line 1086 "parser.c" goto st0; st5: if ( ++p == pe ) @@ -1139,7 +1143,7 @@ case 7: _out: {} } -#line 364 "parser.rl" +#line 368 "parser.rl" if (cs >= JSON_float_first_final) { long len = p - json->memo; @@ -1165,7 +1169,7 @@ case 7: -#line 1169 "parser.c" +#line 1173 "parser.c" enum {JSON_array_start = 1}; enum {JSON_array_first_final = 17}; enum {JSON_array_error = 0}; @@ -1173,7 +1177,7 @@ enum {JSON_array_error = 0}; enum {JSON_array_en_main = 1}; -#line 417 "parser.rl" +#line 421 "parser.rl" static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting) @@ -1187,14 +1191,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 1191 "parser.c" +#line 1195 "parser.c" { cs = JSON_array_start; } -#line 430 "parser.rl" +#line 434 "parser.rl" -#line 1198 "parser.c" +#line 1202 "parser.c" { if ( p == pe ) goto _test_eof; @@ -1233,7 +1237,7 @@ case 2: goto st2; goto st0; tr2: -#line 394 "parser.rl" +#line 398 "parser.rl" { VALUE v = Qnil; char *np = JSON_parse_value(json, p, pe, &v, current_nesting); @@ -1253,7 +1257,7 @@ st3: if ( ++p == pe ) goto _test_eof3; case 3: -#line 1257 "parser.c" +#line 1261 "parser.c" switch( (*p) ) { case 13: goto st3; case 32: goto st3; @@ -1353,14 +1357,14 @@ case 12: goto st3; goto st12; tr4: -#line 409 "parser.rl" +#line 413 "parser.rl" { p--; {p++; cs = 17; goto _out;} } goto st17; st17: if ( ++p == pe ) goto _test_eof17; case 17: -#line 1364 "parser.c" +#line 1368 "parser.c" goto st0; st13: if ( ++p == pe ) @@ -1416,7 +1420,7 @@ case 16: _out: {} } -#line 431 "parser.rl" +#line 435 "parser.rl" if(cs >= JSON_array_first_final) { return p + 1; @@ -1505,7 +1509,7 @@ static VALUE json_string_unescape(VALUE result, char *string, char *stringEnd) } -#line 1509 "parser.c" +#line 1513 "parser.c" enum {JSON_string_start = 1}; enum {JSON_string_first_final = 8}; enum {JSON_string_error = 0}; @@ -1513,7 +1517,7 @@ enum {JSON_string_error = 0}; enum {JSON_string_en_main = 1}; -#line 538 "parser.rl" +#line 542 "parser.rl" static int @@ -1535,15 +1539,15 @@ static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *resu *result = rb_str_buf_new(0); -#line 1539 "parser.c" +#line 1543 "parser.c" { cs = JSON_string_start; } -#line 559 "parser.rl" +#line 563 "parser.rl" json->memo = p; -#line 1547 "parser.c" +#line 1551 "parser.c" { if ( p == pe ) goto _test_eof; @@ -1568,7 +1572,7 @@ case 2: goto st0; goto st2; tr2: -#line 524 "parser.rl" +#line 528 "parser.rl" { *result = json_string_unescape(*result, json->memo + 1, p); if (NIL_P(*result)) { @@ -1579,14 +1583,14 @@ tr2: {p = (( p + 1))-1;} } } -#line 535 "parser.rl" +#line 539 "parser.rl" { p--; {p++; cs = 8; goto _out;} } goto st8; st8: if ( ++p == pe ) goto _test_eof8; case 8: -#line 1590 "parser.c" +#line 1594 "parser.c" goto st0; st3: if ( ++p == pe ) @@ -1662,7 +1666,7 @@ case 7: _out: {} } -#line 561 "parser.rl" +#line 565 "parser.rl" if (json->create_additions && RTEST(match_string = json->match_string)) { VALUE klass; @@ -1678,7 +1682,22 @@ case 7: if (json->symbolize_names && json->parsing_name) { *result = rb_str_intern(*result); } else if (RB_TYPE_P(*result, T_STRING)) { +# if STR_UMINUS_DEDUPE_FROZEN + if (json->freeze) { + // Starting from MRI 2.8 it is preferable to freeze the string + // before deduplication so that it can be interned directly + // otherwise it would be duplicated first which is wasteful. + *result = rb_funcall(rb_str_freeze(*result), i_uminus, 0); + } +# elif STR_UMINUS_DEDUPE + if (json->freeze) { + // MRI 2.5 and older do not deduplicate strings that are already + // frozen. + *result = rb_funcall(*result, i_uminus, 0); + } +# else rb_str_resize(*result, RSTRING_LEN(*result)); +# endif } if (cs >= JSON_string_first_final) { return p + 1; @@ -1786,6 +1805,12 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self) } else { json->symbolize_names = 0; } + tmp = ID2SYM(i_freeze); + if (option_given_p(opts, tmp)) { + json->freeze = RTEST(rb_hash_aref(opts, tmp)) ? 1 : 0; + } else { + json->freeze = 0; + } tmp = ID2SYM(i_create_additions); if (option_given_p(opts, tmp)) { json->create_additions = RTEST(rb_hash_aref(opts, tmp)); @@ -1849,7 +1874,7 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self) } -#line 1853 "parser.c" +#line 1878 "parser.c" enum {JSON_start = 1}; enum {JSON_first_final = 10}; enum {JSON_error = 0}; @@ -1857,7 +1882,7 @@ enum {JSON_error = 0}; enum {JSON_en_main = 1}; -#line 761 "parser.rl" +#line 786 "parser.rl" /* @@ -1874,16 +1899,16 @@ static VALUE cParser_parse(VALUE self) GET_PARSER; -#line 1878 "parser.c" +#line 1903 "parser.c" { cs = JSON_start; } -#line 777 "parser.rl" +#line 802 "parser.rl" p = json->source; pe = p + json->len; -#line 1887 "parser.c" +#line 1912 "parser.c" { if ( p == pe ) goto _test_eof; @@ -1917,7 +1942,7 @@ st0: cs = 0; goto _out; tr2: -#line 753 "parser.rl" +#line 778 "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;} @@ -1927,7 +1952,7 @@ st10: if ( ++p == pe ) goto _test_eof10; case 10: -#line 1931 "parser.c" +#line 1956 "parser.c" switch( (*p) ) { case 13: goto st10; case 32: goto st10; @@ -2016,7 +2041,7 @@ case 9: _out: {} } -#line 780 "parser.rl" +#line 805 "parser.rl" if (cs >= JSON_first_final && p == pe) { return result; @@ -2126,6 +2151,8 @@ void Init_parser(void) i_leftshift = rb_intern("<<"); i_new = rb_intern("new"); i_BigDecimal = rb_intern("BigDecimal"); + i_freeze = rb_intern("freeze"); + i_uminus = rb_intern("-@"); } /* diff --git a/ext/json/parser/parser.h b/ext/json/parser/parser.h index e6cf779024..e3eb920cc6 100644 --- a/ext/json/parser/parser.h +++ b/ext/json/parser/parser.h @@ -37,6 +37,7 @@ typedef struct JSON_ParserStruct { int allow_nan; int parsing_name; int symbolize_names; + int freeze; VALUE object_class; VALUE array_class; VALUE decimal_class; diff --git a/ext/json/parser/parser.rl b/ext/json/parser/parser.rl index 00a35bded4..4629052096 100644 --- a/ext/json/parser/parser.rl +++ b/ext/json/parser/parser.rl @@ -95,7 +95,7 @@ static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions, i_chr, i_max_nesting, i_allow_nan, i_symbolize_names, i_object_class, i_array_class, i_decimal_class, i_key_p, i_deep_const_get, i_match, i_match_string, i_aset, i_aref, - i_leftshift, i_new, i_BigDecimal; + i_leftshift, i_new, i_BigDecimal, i_freeze, i_uminus; %%{ machine JSON_common; @@ -290,6 +290,10 @@ static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *resul %% write init; %% write exec; + if (json->freeze) { + OBJ_FREEZE(*result); + } + if (cs >= JSON_value_first_final) { return p; } else { @@ -573,7 +577,22 @@ static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *resu if (json->symbolize_names && json->parsing_name) { *result = rb_str_intern(*result); } else if (RB_TYPE_P(*result, T_STRING)) { +# if STR_UMINUS_DEDUPE_FROZEN + if (json->freeze) { + // Starting from MRI 2.8 it is preferable to freeze the string + // before deduplication so that it can be interned directly + // otherwise it would be duplicated first which is wasteful. + *result = rb_funcall(rb_str_freeze(*result), i_uminus, 0); + } +# elif STR_UMINUS_DEDUPE + if (json->freeze) { + // MRI 2.5 and older do not deduplicate strings that are already + // frozen. + *result = rb_funcall(*result, i_uminus, 0); + } +# else rb_str_resize(*result, RSTRING_LEN(*result)); +# endif } if (cs >= JSON_string_first_final) { return p + 1; @@ -681,6 +700,12 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self) } else { json->symbolize_names = 0; } + tmp = ID2SYM(i_freeze); + if (option_given_p(opts, tmp)) { + json->freeze = RTEST(rb_hash_aref(opts, tmp)) ? 1 : 0; + } else { + json->freeze = 0; + } tmp = ID2SYM(i_create_additions); if (option_given_p(opts, tmp)) { json->create_additions = RTEST(rb_hash_aref(opts, tmp)); @@ -886,6 +911,8 @@ void Init_parser(void) i_leftshift = rb_intern("<<"); i_new = rb_intern("new"); i_BigDecimal = rb_intern("BigDecimal"); + i_freeze = rb_intern("freeze"); + i_uminus = rb_intern("-@"); } /* diff --git a/test/json/json_parser_test.rb b/test/json/json_parser_test.rb index 514441efce..e29f3f126f 100644 --- a/test/json/json_parser_test.rb +++ b/test/json/json_parser_test.rb @@ -218,6 +218,17 @@ class JSONParserTest < Test::Unit::TestCase end end + def test_freeze + assert_predicate parse('{}', :freeze => true), :frozen? + assert_predicate parse('[]', :freeze => true), :frozen? + assert_predicate parse('"foo"', :freeze => true), :frozen? + + if string_deduplication_available? + assert_same -'foo', parse('"foo"', :freeze => true) + assert_same -'foo', parse('{"foo": 1}', :freeze => true).keys.first + end + end + def test_parse_comments json = <