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

ruby-changes:63555

From: Aaron <ko1@a...>
Date: Tue, 10 Nov 2020 02:44:39 +0900 (JST)
Subject: [ruby-changes:63555] eb229994e5 (master): eagerly initialize ivar table when index is small enough

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

From eb229994e5b53e201e776ea103970751d3b1725b Mon Sep 17 00:00:00 2001
From: Aaron Patterson <tenderlove@r...>
Date: Fri, 6 Nov 2020 10:11:20 -0800
Subject: eagerly initialize ivar table when index is small enough

When the inline cache is written, the iv table will contain an entry for
the instance variable.  If we get an inline cache hit, then we know the
iv table must contain a value for the index written to the inline cache.

If the index in the inline cache is larger than the list on the object,
but *smaller* than the iv index table on the class, then we can just
eagerly allocate the iv list to be the same size as the iv index table.

This avoids duplicate work of checking frozen as well as looking up the
index for the particular instance variable name.

diff --git a/benchmark/vm_ivar_init.yml b/benchmark/vm_ivar_init.yml
new file mode 100644
index 0000000..c6f1633
--- /dev/null
+++ b/benchmark/vm_ivar_init.yml
@@ -0,0 +1,14 @@ https://github.com/ruby/ruby/blob/trunk/benchmark/vm_ivar_init.yml#L1
+prelude: |
+  class C
+    def initialize
+      @a = nil
+      @b = nil
+      @c = nil
+      @d = nil
+      @e = nil
+    end
+  end
+benchmark:
+  vm_ivar_init: |
+    C.new
+loop_count: 30000000
diff --git a/internal/variable.h b/internal/variable.h
index d5d0ccc..34ad4e1 100644
--- a/internal/variable.h
+++ b/internal/variable.h
@@ -52,6 +52,7 @@ VALUE rb_gvar_get(ID); https://github.com/ruby/ruby/blob/trunk/internal/variable.h#L52
 VALUE rb_gvar_set(ID, VALUE);
 VALUE rb_gvar_defined(ID);
 void rb_const_warn_if_deprecated(const rb_const_entry_t *, VALUE, ID);
+void rb_init_iv_list(VALUE obj, uint32_t len, uint32_t newsize, st_table * index_tbl);
 MJIT_SYMBOL_EXPORT_END
 
 static inline bool
diff --git a/variable.c b/variable.c
index 7ebbcf8..669d47e 100644
--- a/variable.c
+++ b/variable.c
@@ -1402,6 +1402,28 @@ rb_obj_transient_heap_evacuate(VALUE obj, int promote) https://github.com/ruby/ruby/blob/trunk/variable.c#L1402
 }
 #endif
 
+void
+rb_init_iv_list(VALUE obj, uint32_t len, uint32_t newsize, st_table * index_tbl)
+{
+    VALUE *ptr = ROBJECT_IVPTR(obj);
+    VALUE *newptr;
+
+    if (RBASIC(obj)->flags & ROBJECT_EMBED) {
+        newptr = obj_ivar_heap_alloc(obj, newsize);
+        MEMCPY(newptr, ptr, VALUE, len);
+        RBASIC(obj)->flags &= ~ROBJECT_EMBED;
+        ROBJECT(obj)->as.heap.ivptr = newptr;
+    } else {
+        newptr = obj_ivar_heap_realloc(obj, len, newsize);
+    }
+
+    for (; len < newsize; len++) {
+        newptr[len] = Qundef;
+    }
+    ROBJECT(obj)->as.heap.numiv = newsize;
+    ROBJECT(obj)->as.heap.iv_index_tbl = index_tbl;
+}
+
 static VALUE
 obj_ivar_set(VALUE obj, ID id, VALUE val)
 {
@@ -1419,25 +1441,8 @@ obj_ivar_set(VALUE obj, ID id, VALUE val) https://github.com/ruby/ruby/blob/trunk/variable.c#L1441
 
     len = ROBJECT_NUMIV(obj);
     if (len <= ivup.index) {
-        VALUE *ptr = ROBJECT_IVPTR(obj);
-        VALUE *newptr;
         uint32_t newsize = iv_index_tbl_newsize(&ivup);
-
-        if (RBASIC(obj)->flags & ROBJECT_EMBED) {
-            newptr = obj_ivar_heap_alloc(obj, newsize);
-            MEMCPY(newptr, ptr, VALUE, len);
-            RBASIC(obj)->flags &= ~ROBJECT_EMBED;
-            ROBJECT(obj)->as.heap.ivptr = newptr;
-        }
-        else {
-            newptr = obj_ivar_heap_realloc(obj, len, newsize);
-        }
-
-        for (; len < newsize; len++) {
-            newptr[len] = Qundef;
-        }
-        ROBJECT(obj)->as.heap.numiv = newsize;
-        ROBJECT(obj)->as.heap.iv_index_tbl = ivup.u.iv_index_tbl;
+        rb_init_iv_list(obj, len, newsize, ivup.u.iv_index_tbl);
     }
     RB_OBJ_WRITE(obj, &ROBJECT_IVPTR(obj)[ivup.index], val);
 
diff --git a/vm_insnhelper.c b/vm_insnhelper.c
index 51b3c40..f8b2f70 100644
--- a/vm_insnhelper.c
+++ b/vm_insnhelper.c
@@ -1213,11 +1213,21 @@ vm_setivar(VALUE obj, ID id, VALUE val, const rb_iseq_t *iseq, IVC ic, const str https://github.com/ruby/ruby/blob/trunk/vm_insnhelper.c#L1213
 	    VALUE *ptr = ROBJECT_IVPTR(obj);
 	    index = !is_attr ? ic->entry->index : vm_cc_attr_index(cc)-1;
 
-	    if (RB_DEBUG_COUNTER_INC_UNLESS(ivar_set_ic_miss_oorange, index < ROBJECT_NUMIV(obj))) {
+	    if (index < ROBJECT_NUMIV(obj)) {
 		RB_OBJ_WRITE(obj, &ptr[index], val);
 		RB_DEBUG_COUNTER_INC(ivar_set_ic_hit);
 		return val; /* inline cache hit */
-	    }
+	    } else {
+                st_table * iv_idx_tbl = RCLASS_IV_INDEX_TBL(rb_class_real(klass));
+                if (index < iv_idx_tbl->num_entries) {
+                    rb_init_iv_list(obj, ROBJECT_NUMIV(obj), iv_idx_tbl->num_entries, iv_idx_tbl);
+                    ptr = ROBJECT_IVPTR(obj);
+                    RB_OBJ_WRITE(obj, &ptr[index], val);
+                    RB_DEBUG_COUNTER_INC(ivar_set_ic_hit);
+                    return val; /* inline cache hit */
+                }
+            }
+            RB_DEBUG_COUNTER_INC(ivar_set_ic_miss_oorange);
 	}
 	else {
 	    struct st_table *iv_index_tbl = ROBJECT_IV_INDEX_TBL(obj);
-- 
cgit v0.10.2


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

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