[前][次][番号順一覧][スレッド一覧]

ruby-changes:63388

From: Jean <ko1@a...>
Date: Tue, 20 Oct 2020 21:52:28 +0900 (JST)
Subject: [ruby-changes:63388] 520e0916af (master): Implement a freeze: parser option

https://git.ruby-lang.org/ruby.git/commit/?id=520e0916af

From 520e0916af0fe53a5ca57269a2bae50cc60e4241 Mon Sep 17 00:00:00 2001
From: Jean Boussier <jean.boussier@g...>
Date: Wed, 9 Sep 2020 15:24:22 +0200
Subject: 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.

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 @@ https://github.com/ruby/ruby/blob/trunk/ext/json/extconf.rb#L1
 require 'mkmf'
+
 create_makefile('json')
diff --git a/ext/json/parser/extconf.rb b/ext/json/parser/extconf.rb
index f7360d4..f832b56 100644
--- a/ext/json/parser/extconf.rb
+++ b/ext/json/parser/extconf.rb
@@ -3,4 +3,29 @@ require 'mkmf' https://github.com/ruby/ruby/blob/trunk/ext/json/parser/extconf.rb#L3
 
 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 6f0e4ac..aaef53a 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, https://github.com/ruby/ruby/blob/trunk/ext/json/parser/parser.c#L97
           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: https://github.com/ruby/ruby/blob/trunk/ext/json/parser/parser.c#L869
 
 #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: https://github.com/ruby/ruby/blob/trunk/ext/json/parser/parser.c#L881
 }
 
 
-#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}; https://github.com/ruby/ruby/blob/trunk/ext/json/parser/parser.c#L889
 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 https://github.com/ruby/ruby/blob/trunk/ext/json/parser/parser.c#L897
     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: https://github.com/ruby/ruby/blob/trunk/ext/json/parser/parser.c#L939
 		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: https://github.com/ruby/ruby/blob/trunk/ext/json/parser/parser.c#L965
 	_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: https://github.com/ruby/ruby/blob/trunk/ext/json/parser/parser.c#L980
 }
 
 
-#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}; https://github.com/ruby/ruby/blob/trunk/ext/json/parser/parser.c#L988
 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 https://github.com/ruby/ruby/blob/trunk/ext/json/parser/parser.c#L1009
     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: https://github.com/ruby/ruby/blob/trunk/ext/json/parser/parser.c#L1075
 		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: https://github.com/ruby/ruby/blob/trunk/ext/json/parser/parser.c#L1143
 	_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: https://github.com/ruby/ruby/blob/trunk/ext/json/parser/parser.c#L1169
 
 
 
-#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}; https://github.com/ruby/ruby/blob/trunk/ext/json/parser/parser.c#L1177
 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 https://github.com/ruby/ruby/blob/trunk/ext/json/parser/parser.c#L1191
     *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: https://github.com/ruby/ruby/blob/trunk/ext/json/parser/parser.c#L1237
 		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: https://github.com/ruby/ruby/blob/trunk/ext/json/parser/parser.c#L1257
 	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: https://github.com/ruby/ruby/blob/trunk/ext/json/parser/parser.c#L1357
 		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: https://github.com/ruby/ruby/blob/trunk/ext/json/parser/parser.c#L1420
 	_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) https://github.com/ruby/ruby/blob/trunk/ext/json/parser/parser.c#L1509
 }
 
 
-#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}; https://github.com/ruby/ruby/blob/trunk/ext/json/parser/parser.c#L1517
 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 https://github.com/ruby/ruby/blob/trunk/ext/json/parser/parser.c#L1539
 
     *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: https://github.com/ruby/ruby/blob/trunk/ext/json/parser/parser.c#L1572
 		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: https://github.com/ruby/ruby/blob/trunk/ext/json/parser/parser.c#L1583
             {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: https://github.com/ruby/ruby/blob/trunk/ext/json/parser/parser.c#L1666
 	_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: https://github.com/ruby/ruby/blob/trunk/ext/json/parser/parser.c#L1682
     if (json->symbolize_names && json->parsing_name) {
       *result = rb_str_intern(*result);
     } else if (RB_TYPE_P(*result, T_STRING)) {
+# if STR_UMINUS_ (... truncated)

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]