1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* method.h: introduce new method type VM_METHOD_TYPE_CFUNC_FAST.

This method is similar to VM_METHOD_TYPE_CFUNC methods, but
  called cfunc without building new frame (does not push new control
  frame). If error is occured in cfunc, the backtrace only shows
  caller frame and upper.
  This kind of methods can be added by rb_define_method_fast().
  This feature is similar to specialized instructions (opt_plus, etc),
  but more flexible (but a bit slower).
* class.c (rb_define_method_fast): added.
  Maybe it will be renamed soon.
* vm_insnhelper.c (vm_call_method): support method type
  VM_METHOD_TYPE_CFUNC_FAST.
* proc.c (rb_method_entry_arity): catch up new method type.
* vm_method.c (rb_add_method_cfunc_fast): added.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37198 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
ko1 2012-10-15 21:24:08 +00:00
parent 747487cda7
commit 90a6dce041
6 changed files with 82 additions and 8 deletions

View file

@ -1,3 +1,24 @@
Tue Oct 16 06:15:44 2012 Koichi Sasada <ko1@atdot.net>
* method.h: introduce new method type VM_METHOD_TYPE_CFUNC_FAST.
This method is similar to VM_METHOD_TYPE_CFUNC methods, but
called cfunc without building new frame (does not push new control
frame). If error is occured in cfunc, the backtrace only shows
caller frame and upper.
This kind of methods can be added by rb_define_method_fast().
This feature is similar to specialized instructions (opt_plus, etc),
but more flexible (but a bit slower).
* class.c (rb_define_method_fast): added.
Maybe it will be renamed soon.
* vm_insnhelper.c (vm_call_method): support method type
VM_METHOD_TYPE_CFUNC_FAST.
* proc.c (rb_method_entry_arity): catch up new method type.
* vm_method.c (rb_add_method_cfunc_fast): added.
Tue Oct 16 02:32:29 2012 Koichi Sasada <ko1@atdot.net>
* vm_insnhelper.h (CI_SET_FASTPATH): add new parameter `enabled'.

View file

@ -1253,6 +1253,12 @@ rb_define_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc
rb_add_method_cfunc(klass, rb_intern(name), func, argc, NOEX_PUBLIC);
}
void
rb_define_method_fast(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
{
rb_add_method_cfunc_fast(klass, rb_intern(name), func, argc, NOEX_PUBLIC);
}
void
rb_define_protected_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
{

View file

@ -41,7 +41,8 @@ typedef enum {
VM_METHOD_TYPE_UNDEF,
VM_METHOD_TYPE_NOTIMPLEMENTED,
VM_METHOD_TYPE_OPTIMIZED, /* Kernel#send, Proc#call, etc */
VM_METHOD_TYPE_MISSING /* wrapper for method_missing(id) */
VM_METHOD_TYPE_MISSING, /* wrapper for method_missing(id) */
VM_METHOD_TYPE_CFUNC_FAST
} rb_method_type_t;
typedef struct rb_method_cfunc_struct {
@ -66,7 +67,7 @@ typedef struct rb_method_definition_struct {
VALUE proc; /* should be mark */
enum method_optimized_type {
OPTIMIZED_METHOD_TYPE_SEND,
OPTIMIZED_METHOD_TYPE_CALL
OPTIMIZED_METHOD_TYPE_CALL,
} optimize_type;
} body;
int alias_count;
@ -88,6 +89,7 @@ struct unlinked_method_entry_list_entry {
#define UNDEFINED_METHOD_ENTRY_P(me) (!(me) || !(me)->def || (me)->def->type == VM_METHOD_TYPE_UNDEF)
void rb_add_method_cfunc(VALUE klass, ID mid, VALUE (*func)(ANYARGS), int argc, rb_method_flag_t noex);
void rb_add_method_cfunc_fast(VALUE klass, ID mid, VALUE (*func)(ANYARGS), int argc, rb_method_flag_t noex);
rb_method_entry_t *rb_add_method(VALUE klass, ID mid, rb_method_type_t type, void *option, rb_method_flag_t noex);
rb_method_entry_t *rb_method_entry(VALUE klass, ID id, VALUE *define_class_ptr);

1
proc.c
View file

@ -1656,6 +1656,7 @@ rb_method_entry_arity(const rb_method_entry_t *me)
const rb_method_definition_t *def = me->def;
if (!def) return 0;
switch (def->type) {
case VM_METHOD_TYPE_CFUNC_FAST:
case VM_METHOD_TYPE_CFUNC:
if (def->body.cfunc.argc < 0)
return -1;

View file

@ -1461,6 +1461,21 @@ vm_call_opt_call(rb_thread_t *th, rb_control_frame_t *cfp, rb_call_info_t *ci)
return rb_vm_invoke_proc(th, proc, argc, argv, ci->blockptr);
}
static VALUE
vm_call_cfunc_fast_unary(rb_thread_t *th, rb_control_frame_t *cfp, rb_call_info_t *ci)
{
cfp->sp -= 1;
return (*ci->me->def->body.cfunc.func)(ci->recv);
}
static VALUE
vm_call_cfunc_fast_binary(rb_thread_t *th, rb_control_frame_t *cfp, rb_call_info_t *ci)
{
VALUE obj = *cfp->sp;
cfp->sp -= 2;
return (*ci->me->def->body.cfunc.func)(ci->recv, obj);
}
static VALUE
vm_call_missing(rb_thread_t *th, rb_control_frame_t *cfp, rb_call_info_t *ci)
{
@ -1544,24 +1559,38 @@ vm_call_method(rb_thread_t *th, rb_control_frame_t *cfp, rb_call_info_t *ci)
}
case VM_METHOD_TYPE_OPTIMIZED:{
switch (ci->me->def->body.optimize_type) {
case OPTIMIZED_METHOD_TYPE_SEND: {
case OPTIMIZED_METHOD_TYPE_SEND:
CI_SET_FASTPATH(ci, vm_call_opt_send, enable_fastpath);
val = vm_call_opt_send(th, cfp, ci);
break;
}
case OPTIMIZED_METHOD_TYPE_CALL: {
case OPTIMIZED_METHOD_TYPE_CALL:
CI_SET_FASTPATH(ci, vm_call_opt_call, enable_fastpath);
val = vm_call_opt_call(th, cfp, ci);
break;
}
default:
rb_bug("eval_invoke_method: unsupported optimized method type (%d)",
rb_bug("vm_call_method: unsupported optimized method type (%d)",
ci->me->def->body.optimize_type);
}
break;
}
case VM_METHOD_TYPE_CFUNC_FAST:
switch (ci->me->def->body.cfunc.argc) {
case 0:
rb_check_arity(ci->argc, 0, 0);
CI_SET_FASTPATH(ci, vm_call_cfunc_fast_unary, enable_fastpath);
val = vm_call_cfunc_fast_unary(th, cfp, ci);
break;
case 1:
rb_check_arity(ci->argc, 0, 1);
CI_SET_FASTPATH(ci, vm_call_cfunc_fast_binary, enable_fastpath);
val = vm_call_cfunc_fast_binary(th, cfp, ci);
break;
default:
rb_bug("vm_call_method: unsupported cfunc_fast argc (%d)", ci->me->def->body.cfunc.argc);
}
break;
default:{
rb_bug("eval_invoke_method: unsupported method type (%d)", ci->me->def->type);
rb_bug("vm_call_method: unsupported method type (%d)", ci->me->def->type);
break;
}
}

View file

@ -95,6 +95,20 @@ rb_add_method_cfunc(VALUE klass, ID mid, VALUE (*func)(ANYARGS), int argc, rb_me
}
}
void
rb_add_method_cfunc_fast(VALUE klass, ID mid, VALUE (*func)(ANYARGS), int argc, rb_method_flag_t noex)
{
if (func != rb_f_notimplement) {
rb_method_cfunc_t opt;
opt.func = func;
opt.argc = argc;
rb_add_method(klass, mid, VM_METHOD_TYPE_CFUNC_FAST, &opt, noex);
}
else {
rb_define_notimplement_method_id(klass, mid, noex);
}
}
void
rb_unlink_method_entry(rb_method_entry_t *me)
{
@ -304,6 +318,7 @@ rb_add_method(VALUE klass, ID mid, rb_method_type_t type, void *opts, rb_method_
def->body.iseq = (rb_iseq_t *)opts;
break;
case VM_METHOD_TYPE_CFUNC:
case VM_METHOD_TYPE_CFUNC_FAST:
def->body.cfunc = *(rb_method_cfunc_t *)opts;
break;
case VM_METHOD_TYPE_ATTRSET: