From f84b46d19ff091323c4ef8732d82fd88c3823ab7 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 9 May 2016 17:30:39 -0700 Subject: [PATCH] Copy the serial number from the super class to the singleton class This helps hit inline method caches more frequently. --- benchmark/bm_vm2_poly_singleton.rb | 14 ++++++++++++++ class.c | 2 ++ 2 files changed, 16 insertions(+) create mode 100644 benchmark/bm_vm2_poly_singleton.rb diff --git a/benchmark/bm_vm2_poly_singleton.rb b/benchmark/bm_vm2_poly_singleton.rb new file mode 100644 index 0000000..0dba432 --- /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 0261838..65428be 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)) { -- 2.8.1