summaryrefslogtreecommitdiff
path: root/vm_method.c
diff options
context:
space:
mode:
authorKoichi Sasada <ko1@atdot.net>2021-11-13 02:12:20 +0900
committerKoichi Sasada <ko1@atdot.net>2021-11-15 15:58:56 +0900
commitb1b73936c15fd490159a9b30ab50b8d5dfea1264 (patch)
tree23c8d640d3251ca0b3865087ae12b27941397d48 /vm_method.c
parentf9638c3b1716df4a94ea6ae0854cf55d66072ee4 (diff)
downloadruby-b1b73936c15fd490159a9b30ab50b8d5dfea1264.tar.gz
`Primitive.mandatory_only?` for fast path
Compare with the C methods, A built-in methods written in Ruby is slower if only mandatory parameters are given because it needs to check the argumens and fill default values for optional and keyword parameters (C methods can check the number of parameters with `argc`, so there are no overhead). Passing mandatory arguments are common (optional arguments are exceptional, in many cases) so it is important to provide the fast path for such common cases. `Primitive.mandatory_only?` is a special builtin function used with `if` expression like that: ```ruby def self.at(time, subsec = false, unit = :microsecond, in: nil) if Primitive.mandatory_only? Primitive.time_s_at1(time) else Primitive.time_s_at(time, subsec, unit, Primitive.arg!(:in)) end end ``` and it makes two ISeq, ``` def self.at(time, subsec = false, unit = :microsecond, in: nil) Primitive.time_s_at(time, subsec, unit, Primitive.arg!(:in)) end def self.at(time) Primitive.time_s_at1(time) end ``` and (2) is pointed by (1). Note that `Primitive.mandatory_only?` should be used only in a condition of an `if` statement and the `if` statement should be equal to the methdo body (you can not put any expression before and after the `if` statement). A method entry with `mandatory_only?` (`Time.at` on the above case) is marked as `iseq_overload`. When the method will be dispatch only with mandatory arguments (`Time.at(0)` for example), make another method entry with ISeq (2) as mandatory only method entry and it will be cached in an inline method cache. The idea is similar discussed in https://bugs.ruby-lang.org/issues/16254 but it only checks mandatory parameters or more, because many cases only mandatory parameters are given. If we find other cases (optional or keyword parameters are used frequently and it hurts performance), we can extend the feature.
Diffstat (limited to 'vm_method.c')
-rw-r--r--vm_method.c44
1 files changed, 41 insertions, 3 deletions
diff --git a/vm_method.c b/vm_method.c
index 75f38d386d..b41b0ff908 100644
--- a/vm_method.c
+++ b/vm_method.c
@@ -208,6 +208,10 @@ clear_method_cache_by_id_in_class(VALUE klass, ID mid)
vm_cme_invalidate((rb_callable_method_entry_t *)cme);
RB_DEBUG_COUNTER_INC(cc_invalidate_tree_cme);
+
+ if (cme->def->iseq_overload) {
+ vm_cme_invalidate((rb_callable_method_entry_t *)cme->def->body.iseq.mandatory_only_cme);
+ }
}
// invalidate complement tbl
@@ -451,10 +455,13 @@ rb_method_definition_set(const rb_method_entry_t *me, rb_method_definition_t *de
case VM_METHOD_TYPE_ISEQ:
{
rb_method_iseq_t *iseq_body = (rb_method_iseq_t *)opts;
+ const rb_iseq_t *iseq = iseq_body->iseqptr;
rb_cref_t *method_cref, *cref = iseq_body->cref;
/* setup iseq first (before invoking GC) */
- RB_OBJ_WRITE(me, &def->body.iseq.iseqptr, iseq_body->iseqptr);
+ RB_OBJ_WRITE(me, &def->body.iseq.iseqptr, iseq);
+
+ if (iseq->body->mandatory_only_iseq) def->iseq_overload = 1;
if (0) vm_cref_dump("rb_method_definition_create", cref);
@@ -531,7 +538,8 @@ method_definition_reset(const rb_method_entry_t *me)
case VM_METHOD_TYPE_ISEQ:
RB_OBJ_WRITTEN(me, Qundef, def->body.iseq.iseqptr);
RB_OBJ_WRITTEN(me, Qundef, def->body.iseq.cref);
- break;
+ RB_OBJ_WRITTEN(me, Qundef, def->body.iseq.mandatory_only_cme);
+ break;
case VM_METHOD_TYPE_ATTRSET:
case VM_METHOD_TYPE_IVAR:
RB_OBJ_WRITTEN(me, Qundef, def->body.attr.location);
@@ -893,6 +901,35 @@ rb_method_entry_make(VALUE klass, ID mid, VALUE defined_class, rb_method_visibil
return me;
}
+static const rb_callable_method_entry_t *
+overloaded_cme(const rb_callable_method_entry_t *cme)
+{
+ VM_ASSERT(cme->def->iseq_overload);
+ VM_ASSERT(cme->def->type == VM_METHOD_TYPE_ISEQ);
+ VM_ASSERT(cme->def->body.iseq.iseqptr != NULL);
+
+ const rb_callable_method_entry_t *monly_cme = cme->def->body.iseq.mandatory_only_cme;
+
+ if (monly_cme && !METHOD_ENTRY_INVALIDATED(monly_cme)) {
+ // ok
+ }
+ else {
+ rb_method_definition_t *def = rb_method_definition_create(VM_METHOD_TYPE_ISEQ, cme->def->original_id);
+ def->body.iseq.cref = cme->def->body.iseq.cref;
+ def->body.iseq.iseqptr = cme->def->body.iseq.iseqptr->body->mandatory_only_iseq;
+
+ rb_method_entry_t *me = rb_method_entry_alloc(cme->called_id,
+ cme->owner,
+ cme->defined_class,
+ def);
+ METHOD_ENTRY_VISI_SET(me, METHOD_ENTRY_VISI(cme));
+ RB_OBJ_WRITE(cme, &cme->def->body.iseq.mandatory_only_cme, me);
+ monly_cme = (rb_callable_method_entry_t *)me;
+ }
+
+ return monly_cme;
+}
+
#define CALL_METHOD_HOOK(klass, hook, mid) do { \
const VALUE arg = ID2SYM(mid); \
VALUE recv_class = (klass); \
@@ -932,6 +969,7 @@ rb_add_method_iseq(VALUE klass, ID mid, const rb_iseq_t *iseq, rb_cref_t *cref,
iseq_body.iseqptr = iseq;
iseq_body.cref = cref;
+
rb_add_method(klass, mid, VM_METHOD_TYPE_ISEQ, &iseq_body, visi);
}
@@ -1876,7 +1914,7 @@ rb_method_definition_eq(const rb_method_definition_t *d1, const rb_method_defini
switch (d1->type) {
case VM_METHOD_TYPE_ISEQ:
- return d1->body.iseq.iseqptr == d2->body.iseq.iseqptr;
+ return d1->body.iseq.iseqptr == d2->body.iseq.iseqptr;
case VM_METHOD_TYPE_CFUNC:
return
d1->body.cfunc.func == d2->body.cfunc.func &&