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

ruby-changes:63093

From: Koichi <ko1@a...>
Date: Fri, 25 Sep 2020 12:53:18 +0900 (JST)
Subject: [ruby-changes:63093] 5286526346 (master): frozen T_OBJECT can be shareable.

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

From 52865263467b48c0f5af6d9548972dd1f9e5bee1 Mon Sep 17 00:00:00 2001
From: Koichi Sasada <ko1@a...>
Date: Fri, 25 Sep 2020 03:19:27 +0900
Subject: frozen T_OBJECT can be shareable.

If an T_OBJECT object is frozen and all ivars are shareable,
the object should be shareable.

diff --git a/bootstraptest/test_ractor.rb b/bootstraptest/test_ractor.rb
index b6f00de..c693a9c 100644
--- a/bootstraptest/test_ractor.rb
+++ b/bootstraptest/test_ractor.rb
@@ -424,6 +424,33 @@ assert_equal "[[[1, true], [:sym, true], [:xyzzy, true], [\"frozen\", true], " \ https://github.com/ruby/ruby/blob/trunk/bootstraptest/test_ractor.rb#L424
   [sr, ur].inspect
 }
 
+# frozen Objects are shareable
+assert_equal [false, true, false].inspect, %q{
+  class C
+    def initialize freeze
+      @a = 1
+      @b = :sym
+      @c = 'frozen_str'
+      @c.freeze if freeze
+      @d = true
+    end
+  end
+
+  def check obj1
+    obj2 = Ractor.new obj1 do |obj|
+      obj
+    end.take
+
+    obj1.object_id == obj2.object_id
+  end
+
+  results = []
+  results << check(C.new(true))         # false
+  results << check(C.new(true).freeze)  # true
+  results << check(C.new(false).freeze) # false
+}
+
+
 # move example2: String
 # touching moved object causes an error
 assert_equal 'hello world', %q{
diff --git a/ractor.c b/ractor.c
index a7e588a..fbc9192 100644
--- a/ractor.c
+++ b/ractor.c
@@ -1776,6 +1776,22 @@ rb_ractor_shareable_p_hash_i(VALUE key, VALUE value, VALUE arg) https://github.com/ruby/ruby/blob/trunk/ractor.c#L1776
     return ST_CONTINUE;
 }
 
+static bool
+ractor_obj_ivars_shareable_p(VALUE obj)
+{
+    uint32_t len = ROBJECT_NUMIV(obj);
+    VALUE *ptr = ROBJECT_IVPTR(obj);
+
+    for (uint32_t i=0; i<len; i++) {
+        VALUE val = ptr[i];
+        if (val != Qundef && !rb_ractor_shareable_p(ptr[i])) {
+            return false;
+        }
+    }
+
+    return true;
+}
+
 MJIT_FUNC_EXPORTED bool
 rb_ractor_shareable_p_continue(VALUE obj)
 {
@@ -1826,6 +1842,13 @@ rb_ractor_shareable_p_continue(VALUE obj) https://github.com/ruby/ruby/blob/trunk/ractor.c#L1842
                 return false;
             }
         }
+      case T_OBJECT:
+        if (RB_OBJ_FROZEN_RAW(obj) && ractor_obj_ivars_shareable_p(obj)) {
+            goto shareable;
+        }
+        else {
+            return false;
+        }
       default:
         return false;
     }
-- 
cgit v0.10.2


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

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