diff options
author | John Hawthorn <john@hawthorn.email> | 2022-02-18 23:05:23 -0800 |
---|---|---|
committer | John Hawthorn <john@hawthorn.email> | 2022-03-10 09:34:08 -0800 |
commit | 1b15756d24c11ed6bfddb5ae53402a071a20ea97 (patch) | |
tree | 82102755afc808a6b77dbdc270d541184b8705c6 /object.c | |
parent | 67faea970857c292ae35dadd103287d4f4449a58 (diff) |
Fast object is iclass checks
Calling rb_obj_is_kind_of with an ICLASS returns the same result as
calling it with the ICLASS's original Module.
Most of the time we encounter an ICLASS here checking the validity of a
protected method or super call, which we expect to return true (or raise
a slow exception anyways). We can take advantage of this by performing a
fast class inheritance check on the ICLASS's "includer" in hopes that it
returns true.
If the includer class check returns false we still have to fallback to
the full inheritance chain scan for the module's inclusion, but this
should be less common.
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/5619
Diffstat (limited to 'object.c')
-rw-r--r-- | object.c | 32 |
1 files changed, 23 insertions, 9 deletions
@@ -817,18 +817,32 @@ rb_obj_is_kind_of(VALUE obj, VALUE c) // class without checking type and can return immediately. if (cl == c) return Qtrue; - // Fast path: Both are T_CLASS - if (LIKELY(RB_TYPE_P(c, T_CLASS))) { - return class_search_class_ancestor(cl, c); - } - // Note: YJIT needs this function to never allocate and never raise when // `c` is a class or a module. - c = class_or_module_required(c); - c = RCLASS_ORIGIN(c); - // Slow path: check each ancestor in the linked list and its method table - return RBOOL(class_search_ancestor(cl, c)); + if (LIKELY(RB_TYPE_P(c, T_CLASS))) { + // Fast path: Both are T_CLASS + return class_search_class_ancestor(cl, c); + } else if (RB_TYPE_P(c, T_ICLASS)) { + // First check if we inherit the includer + // If we do we can return true immediately + VALUE includer = RCLASS_INCLUDER(c); + RUBY_ASSERT(RB_TYPE_P(includer, T_CLASS)); + if (cl == includer) return Qtrue; + + if(class_search_class_ancestor(cl, includer)) + return Qtrue; + + // We don't include the ICLASS directly, but must check if we inherit + // the module via another include + return RBOOL(class_search_ancestor(cl, RCLASS_ORIGIN(c))); + } else if (RB_TYPE_P(c, T_MODULE)) { + // Slow path: check each ancestor in the linked list and its method table + return RBOOL(class_search_ancestor(cl, RCLASS_ORIGIN(c))); + } else { + rb_raise(rb_eTypeError, "class or module required"); + UNREACHABLE_RETURN(Qfalse); + } } |