Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce rb_vm_call_with_refinements to DRY up a few calls #4919

Merged
merged 2 commits into from Oct 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions vm_eval.c
Expand Up @@ -57,6 +57,20 @@ rb_vm_call0(rb_execution_context_t *ec, VALUE recv, ID id, int argc, const VALUE
return vm_call0_body(ec, &calling, argv);
}

MJIT_FUNC_EXPORTED VALUE
rb_vm_call_with_refinements(rb_execution_context_t *ec, VALUE recv, ID id, int argc, const VALUE *argv, int kw_splat)
{
const rb_callable_method_entry_t *me =
rb_callable_method_entry_with_refinements(CLASS_OF(recv), id, NULL);
if (me) {
return rb_vm_call0(ec, recv, id, argc, argv, me, kw_splat);
}
else {
/* fallback to funcall (e.g. method_missing) */
return rb_funcallv(recv, id, argc, argv);
}
}

static inline VALUE
vm_call0_cc(rb_execution_context_t *ec, VALUE recv, ID id, int argc, const VALUE *argv, const struct rb_callcache *cc, int kw_splat)
{
Expand Down
31 changes: 4 additions & 27 deletions vm_insnhelper.c
Expand Up @@ -2087,6 +2087,7 @@ rb_eql_opt(VALUE obj1, VALUE obj2)
#endif // MJIT_HEADER

extern VALUE rb_vm_call0(rb_execution_context_t *ec, VALUE, ID, int, const VALUE*, const rb_callable_method_entry_t *, int kw_splat);
extern VALUE rb_vm_call_with_refinements(rb_execution_context_t *, VALUE, ID, int, const VALUE *, int);

static VALUE
check_match(rb_execution_context_t *ec, VALUE pattern, VALUE target, enum vm_check_match_type type)
Expand All @@ -2100,15 +2101,7 @@ check_match(rb_execution_context_t *ec, VALUE pattern, VALUE target, enum vm_che
}
/* fall through */
case VM_CHECKMATCH_TYPE_CASE: {
const rb_callable_method_entry_t *me =
rb_callable_method_entry_with_refinements(CLASS_OF(pattern), idEqq, NULL);
if (me) {
return rb_vm_call0(ec, pattern, idEqq, 1, &target, me, RB_NO_KEYWORDS);
}
else {
/* fallback to funcall (e.g. method_missing) */
return rb_funcallv(pattern, idEqq, 1, &target);
}
return rb_vm_call_with_refinements(ec, pattern, idEqq, 1, &target, RB_NO_KEYWORDS);
}
default:
rb_bug("check_match: unreachable");
Expand Down Expand Up @@ -4675,15 +4668,7 @@ vm_opt_newarray_max(rb_execution_context_t *ec, rb_num_t num, const VALUE *ptr)
}
}
else {
VALUE ary = rb_ary_new4(num, ptr);
const rb_callable_method_entry_t *me =
rb_callable_method_entry_with_refinements(rb_cArray, idMax, NULL);
if (me) {
return rb_vm_call0(ec, ary, idMax, 0, NULL, me, RB_NO_KEYWORDS);
}
else {
return rb_funcall(ary, idMax, 0);
}
return rb_vm_call_with_refinements(ec, rb_ary_new4(num, ptr), idMax, 0, NULL, RB_NO_KEYWORDS);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought the same thing too, and found an ASSUME would make the generated code better.

Suggested change
return rb_vm_call_with_refinements(ec, rb_ary_new4(num, ptr), idMax, 0, NULL, RB_NO_KEYWORDS);
VALUE ary = rb_ary_new4(num, ptr);
ASSUME(! SPECIAL_CONST_P(ary));
return rb_vm_call_with_refinements(ec, ary, idMax, 0, NULL, RB_NO_KEYWORDS);

Same for vm_opt_newarray_min.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, what I tried was the static inline in vm_insnhelper.c.
May not be effective to MJIT_FUNC_EXPORTED function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. I'm going to merge this as it is. If you think static inline or ASSUME would be helpful, please add them. Thank you for your review.

}
}

Expand All @@ -4708,15 +4693,7 @@ vm_opt_newarray_min(rb_execution_context_t *ec, rb_num_t num, const VALUE *ptr)
}
}
else {
VALUE ary = rb_ary_new4(num, ptr);
const rb_callable_method_entry_t *me =
rb_callable_method_entry_with_refinements(rb_cArray, idMin, NULL);
if (me) {
return rb_vm_call0(ec, ary, idMin, 0, NULL, me, RB_NO_KEYWORDS);
}
else {
return rb_funcall(ary, idMin, 0);
}
return rb_vm_call_with_refinements(ec, rb_ary_new4(num, ptr), idMin, 0, NULL, RB_NO_KEYWORDS);
}
}

Expand Down
6 changes: 3 additions & 3 deletions vm_method.c
Expand Up @@ -1236,7 +1236,7 @@ callable_method_entry_refeinements0(VALUE klass, ID id, VALUE *defined_class_ptr
}

static const rb_callable_method_entry_t *
callable_method_entry_refeinements(VALUE klass, ID id, VALUE *defined_class_ptr, bool with_refinements)
callable_method_entry_refinements(VALUE klass, ID id, VALUE *defined_class_ptr, bool with_refinements)
{
const rb_callable_method_entry_t *cme = callable_method_entry(klass, id, defined_class_ptr);
return callable_method_entry_refeinements0(klass, id, defined_class_ptr, with_refinements, cme);
Expand All @@ -1245,13 +1245,13 @@ callable_method_entry_refeinements(VALUE klass, ID id, VALUE *defined_class_ptr,
MJIT_FUNC_EXPORTED const rb_callable_method_entry_t *
rb_callable_method_entry_with_refinements(VALUE klass, ID id, VALUE *defined_class_ptr)
{
return callable_method_entry_refeinements(klass, id, defined_class_ptr, true);
return callable_method_entry_refinements(klass, id, defined_class_ptr, true);
}

static const rb_callable_method_entry_t *
callable_method_entry_without_refinements(VALUE klass, ID id, VALUE *defined_class_ptr)
{
return callable_method_entry_refeinements(klass, id, defined_class_ptr, false);
return callable_method_entry_refinements(klass, id, defined_class_ptr, false);
}

const rb_method_entry_t *
Expand Down