From 38a74803632428f8fef128750668c9848b8952f0 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Mon, 15 Nov 2021 16:07:08 -0800 Subject: [PATCH] Lazily create singletons on instance_{exec,eval} Previously when instance_exec or instance_eval was called on an object, that object would be given a singleton class so that method definitions inside the block would be added to the object rather than its class. This commit aims to improve performance by delaying the creation of the singleton class unless/until one is needed for method definition. Most of the time instance_eval is used without any method definition. This was implemented by adding a flag to the cref indicating that it represents a singleton of the object rather than a class itself. In this case CREF_CLASS returns the object's existing class, but in cases that we are defining a method (either via definemethod or VM_SPECIAL_OBJECT_CBASE which is used for undef and alias). This also happens to fix what I believe is a bug. Previously instance_eval behaved differently with regards to constant access for true/false/nil than for all other objects. I don't think this was intentional. String::Foo = "foo" "".instance_eval("Foo") # => "foo" Integer::Foo = "foo" 123.instance_eval("Foo") # => "foo" TrueClass::Foo = "foo" true.instance_eval("Foo") # NameError: uninitialized constant Foo This also slightly changes the error message when trying to define a method through instance_eval on an object which can't have a singleton class. Before: $ ruby -e '123.instance_eval { def foo; end }' -e:1:in `block in
': no class/module to add method (TypeError) After: $ ./ruby -e '123.instance_eval { def foo; end }' -e:1:in `block in
': can't define singleton (TypeError) IMO this error is a small improvement on the original and better matches the (both old and new) message when definging a method using `def self.` $ ruby -e '123.instance_eval{ def self.foo; end }' -e:1:in `block in
': can't define singleton (TypeError) Co-authored-by: Matthew Draper --- bootstraptest/test_eval.rb | 16 +++++++++ eval_intern.h | 31 ++++++++++++++++- gc.c | 4 +-- insns.def | 1 + method.h | 2 +- test/ruby/test_eval.rb | 70 ++++++++++++++++++++++++++++++++++++++ test/ruby/test_undef.rb | 16 +++++++++ vm.c | 11 ++++-- vm_eval.c | 66 +++++++++++++++++------------------ vm_insnhelper.c | 23 +++++-------- 10 files changed, 183 insertions(+), 57 deletions(-) diff --git a/bootstraptest/test_eval.rb b/bootstraptest/test_eval.rb index 5d2593c306..ec9c093b8a 100644 --- a/bootstraptest/test_eval.rb +++ b/bootstraptest/test_eval.rb @@ -116,6 +116,22 @@ def foo Const } } +assert_equal %q{1}, %q{ + class TrueClass + Const = 1 + end + true.instance_eval %{ + Const + } +} +assert_equal %q{[:Const]}, %q{ + mod = Module.new + mod.instance_eval %{ + Const = 1 + } + raise if defined?(Module::Const) + mod.singleton_class.constants +} assert_equal %q{top}, %q{ Const = :top class C diff --git a/eval_intern.h b/eval_intern.h index 58400b5f25..e858d79c3e 100644 --- a/eval_intern.h +++ b/eval_intern.h @@ -173,11 +173,28 @@ rb_ec_tag_jump(const rb_execution_context_t *ec, enum ruby_tag_type st) #define CREF_FL_PUSHED_BY_EVAL IMEMO_FL_USER1 #define CREF_FL_OMOD_SHARED IMEMO_FL_USER2 +#define CREF_FL_SINGLETON IMEMO_FL_USER3 + +static inline int CREF_SINGLETON(const rb_cref_t *cref); static inline VALUE CREF_CLASS(const rb_cref_t *cref) { - return cref->klass; + if (CREF_SINGLETON(cref)) { + return CLASS_OF(cref->klass_or_self); + } else { + return cref->klass_or_self; + } +} + +static inline VALUE +CREF_CLASS_FOR_DEFINITION(const rb_cref_t *cref) +{ + if (CREF_SINGLETON(cref)) { + return rb_singleton_class(cref->klass_or_self); + } else { + return cref->klass_or_self; + } } static inline rb_cref_t * @@ -216,6 +233,18 @@ CREF_PUSHED_BY_EVAL_SET(rb_cref_t *cref) cref->flags |= CREF_FL_PUSHED_BY_EVAL; } +static inline int +CREF_SINGLETON(const rb_cref_t *cref) +{ + return cref->flags & CREF_FL_SINGLETON; +} + +static inline void +CREF_SINGLETON_SET(rb_cref_t *cref) +{ + cref->flags |= CREF_FL_SINGLETON; +} + static inline int CREF_OMOD_SHARED(const rb_cref_t *cref) { diff --git a/gc.c b/gc.c index 60387d2ab3..d945b71312 100644 --- a/gc.c +++ b/gc.c @@ -6869,7 +6869,7 @@ gc_mark_imemo(rb_objspace_t *objspace, VALUE obj) } return; case imemo_cref: - gc_mark(objspace, RANY(obj)->as.imemo.cref.klass); + gc_mark(objspace, RANY(obj)->as.imemo.cref.klass_or_self); gc_mark(objspace, (VALUE)RANY(obj)->as.imemo.cref.next); gc_mark(objspace, RANY(obj)->as.imemo.cref.refinements); return; @@ -9734,7 +9734,7 @@ gc_ref_update_imemo(rb_objspace_t *objspace, VALUE obj) } break; case imemo_cref: - UPDATE_IF_MOVED(objspace, RANY(obj)->as.imemo.cref.klass); + UPDATE_IF_MOVED(objspace, RANY(obj)->as.imemo.cref.klass_or_self); TYPED_UPDATE_IF_MOVED(objspace, struct rb_cref_struct *, RANY(obj)->as.imemo.cref.next); UPDATE_IF_MOVED(objspace, RANY(obj)->as.imemo.cref.refinements); break; diff --git a/insns.def b/insns.def index f759c1a5c5..30dcfa6973 100644 --- a/insns.def +++ b/insns.def @@ -350,6 +350,7 @@ putspecialobject (rb_num_t value_type) () (VALUE val) +// attr bool leaf = (value_type != VM_SPECIAL_OBJECT_CBASE); /* get cbase may allocate a singleton */ { enum vm_special_object_type type; diff --git a/method.h b/method.h index 031d2ce89f..8bff5b3b8d 100644 --- a/method.h +++ b/method.h @@ -44,7 +44,7 @@ typedef struct rb_scope_visi_struct { typedef struct rb_cref_struct { VALUE flags; VALUE refinements; - VALUE klass; + VALUE klass_or_self; struct rb_cref_struct * next; const rb_scope_visibility_t scope_visi; } rb_cref_t; diff --git a/test/ruby/test_eval.rb b/test/ruby/test_eval.rb index bf551c6845..d55977c986 100644 --- a/test/ruby/test_eval.rb +++ b/test/ruby/test_eval.rb @@ -219,6 +219,12 @@ def test_instance_eval_cvar end end + def test_instance_exec_cvar + [Object.new, [], 7, :sym, true, false, nil].each do |obj| + assert_equal(13, obj.instance_exec{@@cvar}) + end + end + def test_instance_eval_method bug2788 = '[ruby-core:28324]' [Object.new, [], nil, true, false].each do |o| @@ -253,6 +259,70 @@ def test_instance_eval_const assert_equal(2, bar) end + def test_instance_exec_block_basic + forall_TYPE do |o| + assert_equal nil, o.instance_exec { nil } + assert_equal true, o.instance_exec { true } + assert_equal false, o.instance_exec { false } + assert_equal o, o.instance_exec { self } + assert_equal 1, o.instance_exec { 1 } + assert_equal :sym, o.instance_exec { :sym } + + assert_equal 11, o.instance_exec { 11 } + assert_equal 12, o.instance_exec { @ivar } unless o.frozen? + assert_equal 13, o.instance_exec { @@cvar } + assert_equal 14, o.instance_exec { $gvar__eval } + assert_equal 15, o.instance_exec { Const } + assert_equal 16, o.instance_exec { 7 + 9 } + assert_equal 17, o.instance_exec { 17.to_i } + assert_equal "18", o.instance_exec { "18" } + assert_equal "19", o.instance_exec { "1#{9}" } + + 1.times { + assert_equal 12, o.instance_exec { @ivar } unless o.frozen? + assert_equal 13, o.instance_exec { @@cvar } + assert_equal 14, o.instance_exec { $gvar__eval } + assert_equal 15, o.instance_exec { Const } + } + end + end + + def test_instance_exec_method_definition + klass = Class.new + o = klass.new + + o.instance_exec do + def foo + :foo_result + end + end + + assert_respond_to o, :foo + refute_respond_to klass, :foo + refute_respond_to klass.new, :foo + + assert_equal :foo_result, o.foo + end + + def test_instance_exec_eval_method_definition + klass = Class.new + o = klass.new + + o.instance_exec do + eval %{ + def foo + :foo_result + end + } + end + + assert_respond_to o, :foo + refute_respond_to klass, :foo + refute_respond_to klass.new, :foo + + assert_equal :foo_result, o.foo + end + # # From ruby/test/ruby/test_eval.rb # diff --git a/test/ruby/test_undef.rb b/test/ruby/test_undef.rb index e0add7c3ab..074b92be55 100644 --- a/test/ruby/test_undef.rb +++ b/test/ruby/test_undef.rb @@ -35,4 +35,20 @@ def test_special_const_undef end end end + + def test_singleton_undef + klass = Class.new do + def foo + :ok + end + end + + klass.new.foo + + klass.new.instance_eval do + undef foo + end + + klass.new.foo + end end diff --git a/vm.c b/vm.c index 5941329c4e..bb3b08b7ee 100644 --- a/vm.c +++ b/vm.c @@ -248,6 +248,8 @@ vm_cref_new0(VALUE klass, rb_method_visibility_t visi, int module_func, rb_cref_ } } + VM_ASSERT(klass); + cref = (rb_cref_t *)rb_imemo_new(imemo_cref, klass, (VALUE)(use_prev_prev ? CREF_NEXT(prev_cref) : prev_cref), scope_visi.value, refinements); if (pushed_by_eval) CREF_PUSHED_BY_EVAL_SET(cref); @@ -277,18 +279,21 @@ ref_delete_symkey(VALUE key, VALUE value, VALUE unused) static rb_cref_t * vm_cref_dup(const rb_cref_t *cref) { - VALUE klass = CREF_CLASS(cref); const rb_scope_visibility_t *visi = CREF_SCOPE_VISI(cref); rb_cref_t *next_cref = CREF_NEXT(cref), *new_cref; int pushed_by_eval = CREF_PUSHED_BY_EVAL(cref); - new_cref = vm_cref_new(klass, visi->method_visi, visi->module_func, next_cref, pushed_by_eval); + new_cref = vm_cref_new(cref->klass_or_self, visi->method_visi, visi->module_func, next_cref, pushed_by_eval); if (!NIL_P(CREF_REFINEMENTS(cref))) { VALUE ref = rb_hash_dup(CREF_REFINEMENTS(cref)); rb_hash_foreach(ref, ref_delete_symkey, Qnil); CREF_REFINEMENTS_SET(new_cref, ref); - CREF_OMOD_SHARED_UNSET(new_cref); + CREF_OMOD_SHARED_UNSET(new_cref); + } + + if (CREF_SINGLETON(cref)) { + CREF_SINGLETON_SET(new_cref); } return new_cref; diff --git a/vm_eval.c b/vm_eval.c index 983baf7de6..5b4ff40f28 100644 --- a/vm_eval.c +++ b/vm_eval.c @@ -1855,7 +1855,7 @@ eval_string_wrap_protect(VALUE data) { const struct eval_string_wrap_arg *const arg = (struct eval_string_wrap_arg*)data; rb_cref_t *cref = rb_vm_cref_new_toplevel(); - cref->klass = arg->klass; + cref->klass_or_self = arg->klass; return eval_string_with_cref(arg->top_self, rb_str_new_cstr(arg->str), cref, rb_str_new_cstr("eval"), 1); } @@ -1917,7 +1917,7 @@ rb_eval_cmd_kw(VALUE cmd, VALUE arg, int kw_splat) /* block eval under the class/module context */ static VALUE -yield_under(VALUE under, VALUE self, int argc, const VALUE *argv, int kw_splat) +yield_under(VALUE under, int singleton, VALUE self, int argc, const VALUE *argv, int kw_splat) { rb_execution_context_t *ec = GET_EC(); rb_control_frame_t *cfp = ec->cfp; @@ -1958,7 +1958,14 @@ yield_under(VALUE under, VALUE self, int argc, const VALUE *argv, int kw_splat) VM_FORCE_WRITE_SPECIAL_CONST(&VM_CF_LEP(ec->cfp)[VM_ENV_DATA_INDEX_SPECVAL], new_block_handler); } + //rb_obj_info_dump(under); + // Make crefs log that this is a special lazy singleton? + cref = vm_cref_push(ec, under, ep, TRUE); + if (singleton) { + cref->klass_or_self = self; + CREF_SINGLETON_SET(cref); + } return vm_yield_with_cref(ec, argc, argv, kw_splat, cref, is_lambda); } @@ -1986,19 +1993,24 @@ rb_yield_refine_block(VALUE refinement, VALUE refinements) /* string eval under the class/module context */ static VALUE -eval_under(VALUE under, VALUE self, VALUE src, VALUE file, int line) +eval_under(VALUE under, int singleton, VALUE self, VALUE src, VALUE file, int line) { - rb_cref_t *cref = vm_cref_push(GET_EC(), under, NULL, SPECIAL_CONST_P(self) && !NIL_P(under)); + rb_cref_t *cref = vm_cref_push(GET_EC(), under, NULL, FALSE); + if (singleton) { + cref->klass_or_self = self; + CREF_SINGLETON_SET(cref); + } SafeStringValue(src); + return eval_string_with_cref(self, src, cref, file, line); } static VALUE -specific_eval(int argc, const VALUE *argv, VALUE klass, VALUE self, int kw_splat) +specific_eval(int argc, const VALUE *argv, VALUE klass, VALUE self, int singleton, int kw_splat) { if (rb_block_given_p()) { rb_check_arity(argc, 0, 0); - return yield_under(klass, self, 1, &self, kw_splat); + return yield_under(klass, singleton, self, 1, &self, kw_splat); } else { VALUE file = Qundef; @@ -2014,23 +2026,7 @@ specific_eval(int argc, const VALUE *argv, VALUE klass, VALUE self, int kw_splat file = argv[1]; if (!NIL_P(file)) StringValue(file); } - return eval_under(klass, self, code, file, line); - } -} - -static VALUE -singleton_class_for_eval(VALUE self) -{ - if (SPECIAL_CONST_P(self)) { - return rb_special_singleton_class(self); - } - switch (BUILTIN_TYPE(self)) { - case T_FLOAT: case T_BIGNUM: case T_SYMBOL: - return Qnil; - case T_STRING: - if (FL_TEST_RAW(self, RSTRING_FSTR)) return Qnil; - default: - return rb_singleton_class(self); + return eval_under(klass, singleton, self, code, file, line); } } @@ -2070,15 +2066,15 @@ singleton_class_for_eval(VALUE self) static VALUE rb_obj_instance_eval_internal(int argc, const VALUE *argv, VALUE self) { - VALUE klass = singleton_class_for_eval(self); - return specific_eval(argc, argv, klass, self, RB_PASS_CALLED_KEYWORDS); + VALUE klass = CLASS_OF(self); + return specific_eval(argc, argv, klass, self, TRUE, RB_PASS_CALLED_KEYWORDS); } VALUE rb_obj_instance_eval(int argc, const VALUE *argv, VALUE self) { - VALUE klass = singleton_class_for_eval(self); - return specific_eval(argc, argv, klass, self, RB_NO_KEYWORDS); + VALUE klass = CLASS_OF(self); + return specific_eval(argc, argv, klass, self, TRUE, RB_NO_KEYWORDS); } /* @@ -2102,15 +2098,15 @@ rb_obj_instance_eval(int argc, const VALUE *argv, VALUE self) static VALUE rb_obj_instance_exec_internal(int argc, const VALUE *argv, VALUE self) { - VALUE klass = singleton_class_for_eval(self); - return yield_under(klass, self, argc, argv, RB_PASS_CALLED_KEYWORDS); + VALUE klass = CLASS_OF(self); + return yield_under(klass, TRUE, self, argc, argv, RB_PASS_CALLED_KEYWORDS); } VALUE rb_obj_instance_exec(int argc, const VALUE *argv, VALUE self) { - VALUE klass = singleton_class_for_eval(self); - return yield_under(klass, self, argc, argv, RB_NO_KEYWORDS); + VALUE klass = CLASS_OF(self); + return yield_under(klass, TRUE, self, argc, argv, RB_NO_KEYWORDS); } /* @@ -2143,13 +2139,13 @@ rb_obj_instance_exec(int argc, const VALUE *argv, VALUE self) static VALUE rb_mod_module_eval_internal(int argc, const VALUE *argv, VALUE mod) { - return specific_eval(argc, argv, mod, mod, RB_PASS_CALLED_KEYWORDS); + return specific_eval(argc, argv, mod, mod, FALSE, RB_PASS_CALLED_KEYWORDS); } VALUE rb_mod_module_eval(int argc, const VALUE *argv, VALUE mod) { - return specific_eval(argc, argv, mod, mod, RB_NO_KEYWORDS); + return specific_eval(argc, argv, mod, mod, FALSE, RB_NO_KEYWORDS); } /* @@ -2177,13 +2173,13 @@ rb_mod_module_eval(int argc, const VALUE *argv, VALUE mod) static VALUE rb_mod_module_exec_internal(int argc, const VALUE *argv, VALUE mod) { - return yield_under(mod, mod, argc, argv, RB_PASS_CALLED_KEYWORDS); + return yield_under(mod, FALSE, mod, argc, argv, RB_PASS_CALLED_KEYWORDS); } VALUE rb_mod_module_exec(int argc, const VALUE *argv, VALUE mod) { - return yield_under(mod, mod, argc, argv, RB_NO_KEYWORDS); + return yield_under(mod, FALSE, mod, argc, argv, RB_NO_KEYWORDS); } /* diff --git a/vm_insnhelper.c b/vm_insnhelper.c index d1930d146c..bf80c92dc6 100644 --- a/vm_insnhelper.c +++ b/vm_insnhelper.c @@ -914,15 +914,8 @@ static inline VALUE vm_get_cbase(const VALUE *ep) { const rb_cref_t *cref = vm_get_cref(ep); - VALUE klass = Qundef; - - while (cref) { - if ((klass = CREF_CLASS(cref)) != 0) { - break; - } - cref = CREF_NEXT(cref); - } + VALUE klass = CREF_CLASS_FOR_DEFINITION(cref); return klass; } @@ -934,7 +927,7 @@ vm_get_const_base(const VALUE *ep) while (cref) { if (!CREF_PUSHED_BY_EVAL(cref) && - (klass = CREF_CLASS(cref)) != 0) { + (klass = CREF_CLASS_FOR_DEFINITION(cref)) != 0) { break; } cref = CREF_NEXT(cref); @@ -1060,7 +1053,7 @@ vm_get_cvar_base(const rb_cref_t *cref, const rb_control_frame_t *cfp, int top_l while (CREF_NEXT(cref) && (NIL_P(CREF_CLASS(cref)) || FL_TEST(CREF_CLASS(cref), FL_SINGLETON) || - CREF_PUSHED_BY_EVAL(cref))) { + CREF_PUSHED_BY_EVAL(cref) || CREF_SINGLETON(cref))) { cref = CREF_NEXT(cref); } if (top_level_raise && !CREF_NEXT(cref)) { @@ -4651,14 +4644,14 @@ vm_define_method(const rb_execution_context_t *ec, VALUE obj, ID id, VALUE iseqv rb_method_visibility_t visi; rb_cref_t *cref = vm_ec_cref(ec); - if (!is_singleton) { - klass = CREF_CLASS(cref); - visi = vm_scope_visibility_get(ec); - } - else { /* singleton */ + if (is_singleton) { klass = rb_singleton_class(obj); /* class and frozen checked in this API */ visi = METHOD_VISI_PUBLIC; } + else { + klass = CREF_CLASS_FOR_DEFINITION(cref); + visi = vm_scope_visibility_get(ec); + } if (NIL_P(klass)) { rb_raise(rb_eTypeError, "no class/module to add method"); -- 2.33.1