summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWatson <watson1978@gmail.com>2018-02-27 22:40:58 +0900
committerHiroshi SHIBATA <hsbt@ruby-lang.org>2020-07-01 18:47:51 +0900
commitcb3e62511c7c7a7d568342d82b641e491ce589e1 (patch)
treea9eec8af1f9780c2574171508ee1e2cd19256813
parent7d8ce96de6b977cebf6d9698235a8de8f8d808e1 (diff)
[flori/json] Use frozen string for hash key
When use non-frozen string for hash key with `rb_hash_aset()`, it will duplicate and freeze it internally. To avoid duplicate and freeze, this patch will give a frozen string in `rb_hash_aset()`. ``` Warming up -------------------------------------- json 14.000 i/100ms Calculating ------------------------------------- json 148.844 (± 1.3%) i/s - 756.000 in 5.079969s ``` ``` Warming up -------------------------------------- json 16.000 i/100ms Calculating ------------------------------------- json 165.608 (± 1.8%) i/s - 832.000 in 5.025367s ``` ``` require 'json' require 'securerandom' require 'benchmark/ips' obj = [] 1000.times do |i| obj << { "id": i, "uuid": SecureRandom.uuid, "created_at": Time.now } end json = obj.to_json Benchmark.ips do |x| x.report "json" do |iter| count = 0 while count < iter JSON.parse(json) count += 1 end end end ``` https://github.com/flori/json/commit/18292c0c1d
-rw-r--r--ext/json/parser/parser.c135
-rw-r--r--ext/json/parser/parser.rl1
2 files changed, 69 insertions, 67 deletions
diff --git a/ext/json/parser/parser.c b/ext/json/parser/parser.c
index e4a305663c..6f0e4ace84 100644
--- a/ext/json/parser/parser.c
+++ b/ext/json/parser/parser.c
@@ -112,7 +112,7 @@ enum {JSON_object_error = 0};
enum {JSON_object_en_main = 1};
-#line 167 "parser.rl"
+#line 168 "parser.rl"
static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting)
@@ -133,7 +133,7 @@ static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *resu
cs = JSON_object_start;
}
-#line 182 "parser.rl"
+#line 183 "parser.rl"
#line 139 "parser.c"
{
@@ -163,7 +163,7 @@ case 2:
goto st2;
goto st0;
tr2:
-#line 149 "parser.rl"
+#line 150 "parser.rl"
{
char *np;
json->parsing_name = 1;
@@ -251,6 +251,7 @@ tr11:
p--; {p++; cs = 9; goto _out;}
} else {
if (NIL_P(json->object_class)) {
+ OBJ_FREEZE(last_name);
rb_hash_aset(*result, last_name, v);
} else {
rb_funcall(*result, i_aset, 2, last_name, v);
@@ -263,7 +264,7 @@ st9:
if ( ++p == pe )
goto _test_eof9;
case 9:
-#line 267 "parser.c"
+#line 268 "parser.c"
switch( (*p) ) {
case 13: goto st9;
case 32: goto st9;
@@ -352,14 +353,14 @@ case 18:
goto st9;
goto st18;
tr4:
-#line 157 "parser.rl"
+#line 158 "parser.rl"
{ p--; {p++; cs = 27; goto _out;} }
goto st27;
st27:
if ( ++p == pe )
goto _test_eof27;
case 27:
-#line 363 "parser.c"
+#line 364 "parser.c"
goto st0;
st19:
if ( ++p == pe )
@@ -457,7 +458,7 @@ case 26:
_out: {}
}
-#line 183 "parser.rl"
+#line 184 "parser.rl"
if (cs >= JSON_object_first_final) {
if (json->create_additions) {
@@ -482,7 +483,7 @@ case 26:
-#line 486 "parser.c"
+#line 487 "parser.c"
enum {JSON_value_start = 1};
enum {JSON_value_first_final = 29};
enum {JSON_value_error = 0};
@@ -490,7 +491,7 @@ enum {JSON_value_error = 0};
enum {JSON_value_en_main = 1};
-#line 283 "parser.rl"
+#line 284 "parser.rl"
static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting)
@@ -498,14 +499,14 @@ static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *resul
int cs = EVIL;
-#line 502 "parser.c"
+#line 503 "parser.c"
{
cs = JSON_value_start;
}
-#line 290 "parser.rl"
+#line 291 "parser.rl"
-#line 509 "parser.c"
+#line 510 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -539,14 +540,14 @@ st0:
cs = 0;
goto _out;
tr2:
-#line 235 "parser.rl"
+#line 236 "parser.rl"
{
char *np = JSON_parse_string(json, p, pe, result);
if (np == NULL) { p--; {p++; cs = 29; goto _out;} } else {p = (( np))-1;}
}
goto st29;
tr3:
-#line 240 "parser.rl"
+#line 241 "parser.rl"
{
char *np;
if(pe > p + 8 && !strncmp(MinusInfinity, p, 9)) {
@@ -566,7 +567,7 @@ tr3:
}
goto st29;
tr7:
-#line 258 "parser.rl"
+#line 259 "parser.rl"
{
char *np;
np = JSON_parse_array(json, p, pe, result, current_nesting + 1);
@@ -574,7 +575,7 @@ tr7:
}
goto st29;
tr11:
-#line 264 "parser.rl"
+#line 265 "parser.rl"
{
char *np;
np = JSON_parse_object(json, p, pe, result, current_nesting + 1);
@@ -582,7 +583,7 @@ tr11:
}
goto st29;
tr25:
-#line 228 "parser.rl"
+#line 229 "parser.rl"
{
if (json->allow_nan) {
*result = CInfinity;
@@ -592,7 +593,7 @@ tr25:
}
goto st29;
tr27:
-#line 221 "parser.rl"
+#line 222 "parser.rl"
{
if (json->allow_nan) {
*result = CNaN;
@@ -602,19 +603,19 @@ tr27:
}
goto st29;
tr31:
-#line 215 "parser.rl"
+#line 216 "parser.rl"
{
*result = Qfalse;
}
goto st29;
tr34:
-#line 212 "parser.rl"
+#line 213 "parser.rl"
{
*result = Qnil;
}
goto st29;
tr37:
-#line 218 "parser.rl"
+#line 219 "parser.rl"
{
*result = Qtrue;
}
@@ -623,9 +624,9 @@ st29:
if ( ++p == pe )
goto _test_eof29;
case 29:
-#line 270 "parser.rl"
+#line 271 "parser.rl"
{ p--; {p++; cs = 29; goto _out;} }
-#line 629 "parser.c"
+#line 630 "parser.c"
switch( (*p) ) {
case 13: goto st29;
case 32: goto st29;
@@ -866,7 +867,7 @@ case 28:
_out: {}
}
-#line 291 "parser.rl"
+#line 292 "parser.rl"
if (cs >= JSON_value_first_final) {
return p;
@@ -876,7 +877,7 @@ case 28:
}
-#line 880 "parser.c"
+#line 881 "parser.c"
enum {JSON_integer_start = 1};
enum {JSON_integer_first_final = 3};
enum {JSON_integer_error = 0};
@@ -884,7 +885,7 @@ enum {JSON_integer_error = 0};
enum {JSON_integer_en_main = 1};
-#line 307 "parser.rl"
+#line 308 "parser.rl"
static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *result)
@@ -892,15 +893,15 @@ static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *res
int cs = EVIL;
-#line 896 "parser.c"
+#line 897 "parser.c"
{
cs = JSON_integer_start;
}
-#line 314 "parser.rl"
+#line 315 "parser.rl"
json->memo = p;
-#line 904 "parser.c"
+#line 905 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -934,14 +935,14 @@ case 3:
goto st0;
goto tr4;
tr4:
-#line 304 "parser.rl"
+#line 305 "parser.rl"
{ p--; {p++; cs = 4; goto _out;} }
goto st4;
st4:
if ( ++p == pe )
goto _test_eof4;
case 4:
-#line 945 "parser.c"
+#line 946 "parser.c"
goto st0;
st5:
if ( ++p == pe )
@@ -960,7 +961,7 @@ case 5:
_out: {}
}
-#line 316 "parser.rl"
+#line 317 "parser.rl"
if (cs >= JSON_integer_first_final) {
long len = p - json->memo;
@@ -975,7 +976,7 @@ case 5:
}
-#line 979 "parser.c"
+#line 980 "parser.c"
enum {JSON_float_start = 1};
enum {JSON_float_first_final = 8};
enum {JSON_float_error = 0};
@@ -983,7 +984,7 @@ enum {JSON_float_error = 0};
enum {JSON_float_en_main = 1};
-#line 341 "parser.rl"
+#line 342 "parser.rl"
static int is_bigdecimal_class(VALUE obj)
@@ -1004,15 +1005,15 @@ static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *resul
int cs = EVIL;
-#line 1008 "parser.c"
+#line 1009 "parser.c"
{
cs = JSON_float_start;
}
-#line 361 "parser.rl"
+#line 362 "parser.rl"
json->memo = p;
-#line 1016 "parser.c"
+#line 1017 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -1070,14 +1071,14 @@ case 8:
goto st0;
goto tr9;
tr9:
-#line 335 "parser.rl"
+#line 336 "parser.rl"
{ p--; {p++; cs = 9; goto _out;} }
goto st9;
st9:
if ( ++p == pe )
goto _test_eof9;
case 9:
-#line 1081 "parser.c"
+#line 1082 "parser.c"
goto st0;
st5:
if ( ++p == pe )
@@ -1138,7 +1139,7 @@ case 7:
_out: {}
}
-#line 363 "parser.rl"
+#line 364 "parser.rl"
if (cs >= JSON_float_first_final) {
long len = p - json->memo;
@@ -1164,7 +1165,7 @@ case 7:
-#line 1168 "parser.c"
+#line 1169 "parser.c"
enum {JSON_array_start = 1};
enum {JSON_array_first_final = 17};
enum {JSON_array_error = 0};
@@ -1172,7 +1173,7 @@ enum {JSON_array_error = 0};
enum {JSON_array_en_main = 1};
-#line 416 "parser.rl"
+#line 417 "parser.rl"
static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting)
@@ -1186,14 +1187,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 1190 "parser.c"
+#line 1191 "parser.c"
{
cs = JSON_array_start;
}
-#line 429 "parser.rl"
+#line 430 "parser.rl"
-#line 1197 "parser.c"
+#line 1198 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -1232,7 +1233,7 @@ case 2:
goto st2;
goto st0;
tr2:
-#line 393 "parser.rl"
+#line 394 "parser.rl"
{
VALUE v = Qnil;
char *np = JSON_parse_value(json, p, pe, &v, current_nesting);
@@ -1252,7 +1253,7 @@ st3:
if ( ++p == pe )
goto _test_eof3;
case 3:
-#line 1256 "parser.c"
+#line 1257 "parser.c"
switch( (*p) ) {
case 13: goto st3;
case 32: goto st3;
@@ -1352,14 +1353,14 @@ case 12:
goto st3;
goto st12;
tr4:
-#line 408 "parser.rl"
+#line 409 "parser.rl"
{ p--; {p++; cs = 17; goto _out;} }
goto st17;
st17:
if ( ++p == pe )
goto _test_eof17;
case 17:
-#line 1363 "parser.c"
+#line 1364 "parser.c"
goto st0;
st13:
if ( ++p == pe )
@@ -1415,7 +1416,7 @@ case 16:
_out: {}
}
-#line 430 "parser.rl"
+#line 431 "parser.rl"
if(cs >= JSON_array_first_final) {
return p + 1;
@@ -1504,7 +1505,7 @@ static VALUE json_string_unescape(VALUE result, char *string, char *stringEnd)
}
-#line 1508 "parser.c"
+#line 1509 "parser.c"
enum {JSON_string_start = 1};
enum {JSON_string_first_final = 8};
enum {JSON_string_error = 0};
@@ -1512,7 +1513,7 @@ enum {JSON_string_error = 0};
enum {JSON_string_en_main = 1};
-#line 537 "parser.rl"
+#line 538 "parser.rl"
static int
@@ -1534,15 +1535,15 @@ static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *resu
*result = rb_str_buf_new(0);
-#line 1538 "parser.c"
+#line 1539 "parser.c"
{
cs = JSON_string_start;
}
-#line 558 "parser.rl"
+#line 559 "parser.rl"
json->memo = p;
-#line 1546 "parser.c"
+#line 1547 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -1567,7 +1568,7 @@ case 2:
goto st0;
goto st2;
tr2:
-#line 523 "parser.rl"
+#line 524 "parser.rl"
{
*result = json_string_unescape(*result, json->memo + 1, p);
if (NIL_P(*result)) {
@@ -1578,14 +1579,14 @@ tr2:
{p = (( p + 1))-1;}
}
}
-#line 534 "parser.rl"
+#line 535 "parser.rl"
{ p--; {p++; cs = 8; goto _out;} }
goto st8;
st8:
if ( ++p == pe )
goto _test_eof8;
case 8:
-#line 1589 "parser.c"
+#line 1590 "parser.c"
goto st0;
st3:
if ( ++p == pe )
@@ -1661,7 +1662,7 @@ case 7:
_out: {}
}
-#line 560 "parser.rl"
+#line 561 "parser.rl"
if (json->create_additions && RTEST(match_string = json->match_string)) {
VALUE klass;
@@ -1848,7 +1849,7 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
}
-#line 1852 "parser.c"
+#line 1853 "parser.c"
enum {JSON_start = 1};
enum {JSON_first_final = 10};
enum {JSON_error = 0};
@@ -1856,7 +1857,7 @@ enum {JSON_error = 0};
enum {JSON_en_main = 1};
-#line 760 "parser.rl"
+#line 761 "parser.rl"
/*
@@ -1873,16 +1874,16 @@ static VALUE cParser_parse(VALUE self)
GET_PARSER;
-#line 1877 "parser.c"
+#line 1878 "parser.c"
{
cs = JSON_start;
}
-#line 776 "parser.rl"
+#line 777 "parser.rl"
p = json->source;
pe = p + json->len;
-#line 1886 "parser.c"
+#line 1887 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -1916,7 +1917,7 @@ st0:
cs = 0;
goto _out;
tr2:
-#line 752 "parser.rl"
+#line 753 "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;}
@@ -1926,7 +1927,7 @@ st10:
if ( ++p == pe )
goto _test_eof10;
case 10:
-#line 1930 "parser.c"
+#line 1931 "parser.c"
switch( (*p) ) {
case 13: goto st10;
case 32: goto st10;
@@ -2015,7 +2016,7 @@ case 9:
_out: {}
}
-#line 779 "parser.rl"
+#line 780 "parser.rl"
if (cs >= JSON_first_final && p == pe) {
return result;
diff --git a/ext/json/parser/parser.rl b/ext/json/parser/parser.rl
index 6b38bb283a..00a35bded4 100644
--- a/ext/json/parser/parser.rl
+++ b/ext/json/parser/parser.rl
@@ -138,6 +138,7 @@ static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions,
fhold; fbreak;
} else {
if (NIL_P(json->object_class)) {
+ OBJ_FREEZE(last_name);
rb_hash_aset(*result, last_name, v);
} else {
rb_funcall(*result, i_aset, 2, last_name, v);