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

ruby-changes:63815

From: Koichi <ko1@a...>
Date: Tue, 1 Dec 2020 18:16:39 +0900 (JST)
Subject: [ruby-changes:63815] 764de7566f (master): should not use rb_str_modify(), too

https://git.ruby-lang.org/ruby.git/commit/?id=764de7566f

From 764de7566ffa3fe828abf13ec93d76515ba27dd1 Mon Sep 17 00:00:00 2001
From: Koichi Sasada <ko1@a...>
Date: Tue, 1 Dec 2020 16:34:59 +0900
Subject: should not use rb_str_modify(), too

Same as 8247b8edde, should not use rb_str_modify() here.

https://bugs.ruby-lang.org/issues/17343#change-88858

diff --git a/bootstraptest/test_ractor.rb b/bootstraptest/test_ractor.rb
index 2ae6602..a960d9d 100644
--- a/bootstraptest/test_ractor.rb
+++ b/bootstraptest/test_ractor.rb
@@ -1022,13 +1022,21 @@ assert_equal 'can not make a Proc shareable because it accesses outer variables https://github.com/ruby/ruby/blob/trunk/bootstraptest/test_ractor.rb#L1022
   end
 }
 
-# Ractor deep copies frozen objects
+# Ractor deep copies frozen objects (ary)
 assert_equal '[true, false]', %q{
   Ractor.new([[]].freeze) { |ary|
     [ary.frozen?, ary.first.frozen? ]
   }.take
 }
 
+# Ractor deep copies frozen objects (str)
+assert_equal '[true, false]', %q{
+  s = String.new.instance_eval { @x = []; freeze}
+  Ractor.new(s) { |s|
+    [s.frozen?, s.instance_variable_get(:@x).frozen?]
+  }.take
+}
+
 ###
 ### Synchronization tests
 ###
diff --git a/internal/string.h b/internal/string.h
index 5c9f615..091035d 100644
--- a/internal/string.h
+++ b/internal/string.h
@@ -42,6 +42,8 @@ VALUE rb_str_initialize(VALUE str, const char *ptr, long len, rb_encoding *enc); https://github.com/ruby/ruby/blob/trunk/internal/string.h#L42
 size_t rb_str_memsize(VALUE);
 char *rb_str_to_cstr(VALUE str);
 const char *ruby_escaped_char(int c);
+void rb_str_make_independent(VALUE str);
+
 static inline bool STR_EMBED_P(VALUE str);
 static inline bool STR_SHARED_P(VALUE str);
 static inline VALUE QUOTE(VALUE v);
diff --git a/ractor.c b/ractor.c
index c895aab..fcdf5cf 100644
--- a/ractor.c
+++ b/ractor.c
@@ -2294,7 +2294,7 @@ obj_traverse_replace_i(VALUE obj, struct obj_traverse_replace_data *data) https://github.com/ruby/ruby/blob/trunk/ractor.c#L2294
       case T_MATCH:
         break;
       case T_STRING:
-        rb_str_modify(obj);
+        rb_str_make_independent(obj);
         break;
 
       case T_OBJECT:
diff --git a/string.c b/string.c
index 086e97a..2ce0fd2 100644
--- a/string.c
+++ b/string.c
@@ -214,6 +214,16 @@ str_make_independent(VALUE str) https://github.com/ruby/ruby/blob/trunk/string.c#L214
     str_make_independent_expand((str), len, 0L, termlen);
 }
 
+static inline int str_dependent_p(VALUE str);
+
+void
+rb_str_make_independent(VALUE str)
+{
+    if (str_dependent_p(str)) {
+        str_make_independent(str);
+    }
+}
+
 /* symbols for [up|down|swap]case/capitalize options */
 static VALUE sym_ascii, sym_turkic, sym_lithuanian, sym_fold;
 
@@ -324,7 +334,7 @@ fstr_update_callback(st_data_t *key, st_data_t *value, st_data_t data, int exist https://github.com/ruby/ruby/blob/trunk/string.c#L334
 		str = str_new_frozen(rb_cString, str);
 	    if (STR_SHARED_P(str)) { /* str should not be shared */
 		/* shared substring  */
-		str_make_independent(str);
+                str_make_independent(str);
 		assert(OBJ_FROZEN(str));
 	    }
 	    if (!BARE_STRING_P(str)) {
-- 
cgit v0.10.2


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

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