From 0b54bde47622409ef2e55bd49fd4d7df89ef0cd8 Mon Sep 17 00:00:00 2001 From: tenderlove Date: Mon, 12 Sep 2016 15:40:09 +0000 Subject: Copy the serial number from the super class to the singleton class This helps hit inline method caches more frequently. Before this commit: ``` [aaron@TC ruby (trunk)]$ time ./ruby -v benchmark/bm_vm2_poly_singleton.rb ruby 2.4.0dev (2016-09-12 trunk 56141) [x86_64-darwin15] real 0m3.679s user 0m3.632s sys 0m0.022s ``` After this commit: ``` [aaron@TC ruby (trunk)]$ time ./ruby -v benchmark/bm_vm2_poly_singleton.rb ruby 2.4.0dev (2016-09-12 trunk 56141) [x86_64-darwin15] last_commit=Copy the serial number from the super class to the singleton class real 0m2.246s user 0m2.203s sys 0m0.020s ``` [Feature #12364] [ruby-core:75425] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56144 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 6 ++++++ benchmark/bm_vm2_poly_singleton.rb | 14 ++++++++++++++ class.c | 2 ++ 3 files changed, 22 insertions(+) create mode 100644 benchmark/bm_vm2_poly_singleton.rb diff --git a/ChangeLog b/ChangeLog index 83800b1b8a..7eabe4460e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Tue Sep 13 00:39:47 2016 Aaron Patterson + + * class.c (singleton_class_of): Copy superclass serial number to + singleton class. This improves singleton class IMC hit rates. + [Feature #12364] [ruby-core:75425] + Mon Sep 12 13:46:23 2016 Anton Davydov * lib/uri/mailto.rb: Removed needless `return` and use `.`` instead of `::` diff --git a/benchmark/bm_vm2_poly_singleton.rb b/benchmark/bm_vm2_poly_singleton.rb new file mode 100644 index 0000000000..0dba4320c4 --- /dev/null +++ b/benchmark/bm_vm2_poly_singleton.rb @@ -0,0 +1,14 @@ +class C1 + def m; 1; end +end + +o1 = C1.new +o2 = C1.new +o2.singleton_class + +i = 0 +while i<6_000_000 # benchmark loop 2 + o = (i % 2 == 0) ? o1 : o2 + o.m; o.m; o.m; o.m; o.m; o.m; o.m; o.m + i += 1 +end diff --git a/class.c b/class.c index 6c7dfa5c17..ce2ede5c2b 100644 --- a/class.c +++ b/class.c @@ -1591,7 +1591,9 @@ singleton_class_of(VALUE obj) klass = RBASIC(obj)->klass; if (!(FL_TEST(klass, FL_SINGLETON) && rb_ivar_get(klass, id_attached) == obj)) { + rb_serial_t serial = RCLASS_SERIAL(klass); klass = rb_make_metaclass(obj, klass); + RCLASS_SERIAL(klass) = serial; } if (OBJ_TAINTED(obj)) { -- cgit v1.2.3