summaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
authorJean Boussier <jean.boussier@gmail.com>2020-09-09 15:24:22 +0200
committerJean Boussier <jean.boussier@gmail.com>2020-09-15 14:49:35 +0200
commit9bf8aa21b36de3bd90bed85e6c85412426b03d9d (patch)
treef636f2f8736107a57687d38dfe7ac6f80c47ac92 /ext
parentc5083b238ed2016b968c402990f9de084f415e0d (diff)
downloadjson-9bf8aa21b36de3bd90bed85e6c85412426b03d9d.tar.gz
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.
Diffstat (limited to 'ext')
-rw-r--r--ext/json/ext/parser/extconf.rb25
-rw-r--r--ext/json/ext/parser/parser.c115
-rw-r--r--ext/json/ext/parser/parser.h1
-rw-r--r--ext/json/ext/parser/parser.rl29
-rw-r--r--ext/json/extconf.rb1
5 files changed, 126 insertions, 45 deletions
diff --git a/ext/json/ext/parser/extconf.rb b/ext/json/ext/parser/extconf.rb
index f7360d4..f832b56 100644
--- a/ext/json/ext/parser/extconf.rb
+++ b/ext/json/ext/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/ext/parser/parser.c b/ext/json/ext/parser/parser.c
index 6f0e4ac..aaef53a 100644
--- a/ext/json/ext/parser/parser.c
+++ b/ext/json/ext/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/ext/parser/parser.h b/ext/json/ext/parser/parser.h
index e6cf779..e3eb920 100644
--- a/ext/json/ext/parser/parser.h
+++ b/ext/json/ext/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/ext/parser/parser.rl b/ext/json/ext/parser/parser.rl
index 00a35bd..4629052 100644
--- a/ext/json/ext/parser/parser.rl
+++ b/ext/json/ext/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/ext/json/extconf.rb b/ext/json/extconf.rb
index 7595d58..8a99b6a 100644
--- a/ext/json/extconf.rb
+++ b/ext/json/extconf.rb
@@ -1,2 +1,3 @@
require 'mkmf'
+
create_makefile('json')