* method.h: back to share rb_method_definition_t by

rb_method_entry_t.
  r50728 changed sharing `def's to isolating `def's
  on alias and so on. However, this change conflicts
  future improvement plan. So I change back to sharing approach.
* method.h: move rb_method_definition_t::flags to
  rb_method_entry_t::attr::flags.
  rb_method_entry_t::attr is union with VALUE because this field
  should have same size of VALUE. rb_method_entry_t is T_IMEMO).
  And also add the following access macros to it's fileds.
  * METHOD_ENTRY_VISI(me)
  * METHOD_ENTRY_BASIC(me)
  * METHOD_ENTRY_SAFE(me)
* vm_method.c (rb_method_definition_addref): added instead of
  rb_method_definition_clone().
  Do not create new definition, but increment alias_count.
* class.c (clone_method): catch up this fix.
* class.c (method_entry_i): ditto.
* proc.c (mnew_internal): ditto.
* proc.c (mnew_missing): ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50792 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
ko1 2015-06-06 10:19:48 +00:00
Родитель c9044a2472
Коммит c19d373750
7 изменённых файлов: 114 добавлений и 90 удалений

Просмотреть файл

@ -1,3 +1,43 @@
Sat Jun 6 18:23:41 2015 Koichi Sasada <ko1@atdot.net>
* method.h: back to share rb_method_definition_t by
rb_method_entry_t.
r50728 changed sharing `def's to isolating `def's
on alias and so on. However, this change conflicts
future improvement plan. So I change back to sharing approach.
* method.h: move rb_method_definition_t::flags to
rb_method_entry_t::attr::flags.
rb_method_entry_t::attr is union with VALUE because this field
should have same size of VALUE. rb_method_entry_t is T_IMEMO).
And also add the following access macros to it's fileds.
* METHOD_ENTRY_VISI(me)
* METHOD_ENTRY_BASIC(me)
* METHOD_ENTRY_SAFE(me)
* vm_method.c (rb_method_definition_addref): added instead of
rb_method_definition_clone().
Do not create new definition, but increment alias_count.
* class.c (clone_method): catch up this fix.
* class.c (method_entry_i): ditto.
* proc.c (mnew_internal): ditto.
* proc.c (mnew_missing): ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
Sat Jun 6 15:59:38 2015 Koichi Sasada <ko1@atdot.net>
* class.c: ins_methods_push() needs rb_method_visibility_t type on

Просмотреть файл

@ -248,11 +248,11 @@ clone_method(VALUE old_klass, VALUE new_klass, ID mid, const rb_method_entry_t *
rb_cref_t *new_cref;
newiseqval = rb_iseq_clone(me->def->body.iseq.iseqptr->self, new_klass);
rb_vm_rewrite_cref(me->def->body.iseq.cref, old_klass, new_klass, &new_cref);
rb_add_method_iseq(new_klass, mid, newiseqval, new_cref, me->def->flags.visi);
rb_add_method_iseq(new_klass, mid, newiseqval, new_cref, METHOD_ENTRY_VISI(me));
RB_GC_GUARD(newiseqval);
}
else {
rb_method_entry_set(new_klass, mid, me, me->def->flags.visi);
rb_method_entry_set(new_klass, mid, me, METHOD_ENTRY_VISI(me));
}
}
@ -1129,7 +1129,7 @@ method_entry_i(st_data_t key, st_data_t value, st_data_t data)
type = METHOD_VISI_UNDEF; /* none */
}
else {
type = me->def->flags.visi;
type = METHOD_ENTRY_VISI(me);
}
st_add_direct(arg->list, key, (st_data_t)type);
}

Просмотреть файл

@ -47,12 +47,25 @@ typedef struct rb_cref_struct {
typedef struct rb_method_entry_struct {
VALUE flags;
VALUE reserved;
union {
struct {
rb_method_visibility_t visi: 3;
unsigned int safe: 3;
unsigned int basic: 1;
} flags;
VALUE v; /* it should be VALUE size */
} attr;
struct rb_method_definition_struct * const def;
ID called_id;
const VALUE klass; /* should be marked */
} rb_method_entry_t;
#define METHOD_ENTRY_VISI(me) (me)->attr.flags.visi
#define METHOD_ENTRY_BASIC(me) (me)->attr.flags.basic
#define METHOD_ENTRY_SAFE(me) (me)->attr.flags.safe
typedef enum {
VM_METHOD_TYPE_ISEQ,
VM_METHOD_TYPE_CFUNC,
@ -97,12 +110,8 @@ typedef struct rb_method_refined_struct {
} rb_method_refined_t;
typedef struct rb_method_definition_struct {
struct {
rb_method_visibility_t visi: 3;
unsigned int safe: 3;
unsigned int basic: 1;
} flags;
rb_method_type_t type; /* method type */
int alias_count;
union {
rb_method_iseq_t iseq;
@ -120,7 +129,6 @@ typedef struct rb_method_definition_struct {
} optimize_type;
} body;
int *alias_count_ptr;
ID original_id;
} rb_method_definition_t;
@ -157,7 +165,7 @@ VALUE rb_obj_method_location(VALUE obj, ID id);
void rb_free_method_entry(const rb_method_entry_t *me);
void rb_sweep_method_entry(void *vm);
rb_method_entry_t *rb_method_entry_create(ID called_id, VALUE klass, rb_method_definition_t *def);
rb_method_entry_t *rb_method_entry_create(ID called_id, VALUE klass, rb_method_visibility_t visi, rb_method_definition_t *def);
rb_method_entry_t *rb_method_entry_clone(const rb_method_entry_t *me);
void rb_method_entry_copy(rb_method_entry_t *dst, const rb_method_entry_t *src);

12
proc.c
Просмотреть файл

@ -1161,11 +1161,11 @@ mnew_missing(VALUE rclass, VALUE klass, VALUE obj, ID id, ID rid, VALUE mclass)
data->id = rid;
def = ZALLOC(rb_method_definition_t);
def->flags.visi = METHOD_VISI_UNDEF;
def->type = VM_METHOD_TYPE_MISSING;
def->original_id = id;
me = rb_method_entry_create(id, klass, def);
me = rb_method_entry_create(id, klass, METHOD_VISI_UNDEF, def);
RB_OBJ_WRITE(method, &data->me, me);
OBJ_INFECT(method, klass);
@ -1181,7 +1181,6 @@ mnew_internal(const rb_method_entry_t *me, VALUE defined_class, VALUE klass,
VALUE rclass = klass;
VALUE method;
ID rid = id;
rb_method_definition_t *def = 0;
rb_method_visibility_t visi = METHOD_VISI_UNDEF;
again:
@ -1192,17 +1191,16 @@ mnew_internal(const rb_method_entry_t *me, VALUE defined_class, VALUE klass,
if (!error) return Qnil;
rb_print_undef(klass, id, 0);
}
def = me->def;
if (visi == METHOD_VISI_UNDEF) {
visi = def->flags.visi;
visi = METHOD_ENTRY_VISI(me);
if (scope && (visi != METHOD_VISI_PUBLIC)) {
if (!error) return Qnil;
rb_print_inaccessible(klass, id, visi);
}
}
if (def->type == VM_METHOD_TYPE_ZSUPER) {
if (me->def->type == VM_METHOD_TYPE_ZSUPER) {
klass = RCLASS_SUPER(defined_class);
id = def->original_id;
id = me->def->original_id;
me = rb_method_entry_without_refinements(klass, id, &defined_class);
goto again;
}

Просмотреть файл

@ -387,7 +387,7 @@ check_funcall_respond_to(rb_thread_t *th, VALUE klass, VALUE recv, ID mid)
VALUE defined_class;
const rb_method_entry_t *me = rb_method_entry(klass, idRespond_to, &defined_class);
if (me && !me->def->flags.basic) {
if (me && !METHOD_ENTRY_BASIC(me)) {
const rb_block_t *passed_block = th->passed_block;
VALUE args[2], result;
int arity = rb_method_entry_arity(me);
@ -570,7 +570,7 @@ rb_method_call_status(rb_thread_t *th, const rb_method_entry_t *me, call_type sc
}
klass = me->klass;
oid = me->def->original_id;
visi = me->def->flags.visi;
visi = METHOD_ENTRY_VISI(me);
if (oid != idMethodMissing) {
/* receiver specified form for private method */
@ -592,7 +592,7 @@ rb_method_call_status(rb_thread_t *th, const rb_method_entry_t *me, call_type sc
}
}
if (me->def->flags.safe > th->safe_level) {
if (METHOD_ENTRY_SAFE(me) > th->safe_level) {
rb_raise(rb_eSecurityError, "calling insecure method: %"PRIsVALUE, rb_id2str(me->called_id));
}
}

Просмотреть файл

@ -1315,7 +1315,7 @@ vm_callee_setup_arg(rb_thread_t *th, rb_call_info_t *ci, const rb_iseq_t *iseq,
CI_SET_FASTPATH(ci,
(UNLIKELY(ci->flag & VM_CALL_TAILCALL) ? vm_call_iseq_setup_tailcall : vm_call_iseq_setup_normal),
(!IS_ARGS_SPLAT(ci) && !IS_ARGS_KEYWORD(ci) && !(ci->me->def->flags.visi == METHOD_VISI_PROTECTED)));
(!IS_ARGS_SPLAT(ci) && !IS_ARGS_KEYWORD(ci) && !(METHOD_ENTRY_VISI(ci->me) == METHOD_VISI_PROTECTED)));
}
else {
ci->aux.opt_pc = setup_parameters_complex(th, iseq, ci, argv, arg_setup_method);
@ -1908,7 +1908,7 @@ vm_call_method(rb_thread_t *th, rb_control_frame_t *cfp, rb_call_info_t *ci)
start_method_dispatch:
if (ci->me != 0) {
if (LIKELY(ci->me->def->flags.visi == METHOD_VISI_PUBLIC && ci->me->def->flags.safe == 0)) {
if (LIKELY(METHOD_ENTRY_VISI(ci->me) == METHOD_VISI_PUBLIC && METHOD_ENTRY_SAFE(ci->me) == 0)) {
VALUE klass;
normal_method_dispatch:
@ -2036,7 +2036,7 @@ vm_call_method(rb_thread_t *th, rb_control_frame_t *cfp, rb_call_info_t *ci)
}
else {
int safe;
if (!(ci->flag & VM_CALL_FCALL) && (ci->me->def->flags.visi == METHOD_VISI_PRIVATE)) {
if (!(ci->flag & VM_CALL_FCALL) && (METHOD_ENTRY_VISI(ci->me) == METHOD_VISI_PRIVATE)) {
enum method_missing_reason stat = MISSING_PRIVATE;
bp();
if (ci->flag & VM_CALL_VCALL) stat |= MISSING_VCALL;
@ -2045,7 +2045,7 @@ vm_call_method(rb_thread_t *th, rb_control_frame_t *cfp, rb_call_info_t *ci)
CI_SET_FASTPATH(ci, vm_call_method_missing, 1);
return vm_call_method_missing(th, cfp, ci);
}
else if (!(ci->flag & VM_CALL_OPT_SEND) && (ci->me->def->flags.visi == METHOD_VISI_PROTECTED)) {
else if (!(ci->flag & VM_CALL_OPT_SEND) && (METHOD_ENTRY_VISI(ci->me) == METHOD_VISI_PROTECTED)) {
enable_fastpath = 0;
if (!rb_obj_is_kind_of(cfp->self, ci->defined_class)) {
ci->aux.method_missing_reason = MISSING_PROTECTED;
@ -2055,7 +2055,7 @@ vm_call_method(rb_thread_t *th, rb_control_frame_t *cfp, rb_call_info_t *ci)
goto normal_method_dispatch;
}
}
else if ((safe = ci->me->def->flags.safe) > th->safe_level && safe > 2) {
else if ((safe = METHOD_ENTRY_SAFE(ci->me)) > th->safe_level && safe > 2) {
rb_raise(rb_eSecurityError, "calling insecure method: %"PRIsVALUE, rb_id2str(ci->mid));
}
else {
@ -2466,9 +2466,7 @@ vm_defined(rb_thread_t *th, rb_control_frame_t *reg_cfp, rb_num_t op_type, VALUE
const rb_method_entry_t *me = rb_method_entry(klass, SYM2ID(obj), 0);
if (me) {
const rb_method_definition_t *def = me->def;
switch (def->flags.visi) {
switch (METHOD_ENTRY_VISI(me)) {
case METHOD_VISI_PRIVATE:
break;
case METHOD_VISI_PROTECTED:
@ -2479,7 +2477,7 @@ vm_defined(rb_thread_t *th, rb_control_frame_t *reg_cfp, rb_num_t op_type, VALUE
expr_type = DEFINED_METHOD;
break;
default:
rb_bug("vm_defined: unreachable: %u", (unsigned int)def->flags.visi);
rb_bug("vm_defined: unreachable: %u", (unsigned int)METHOD_ENTRY_VISI(me));
}
}
else {

Просмотреть файл

@ -134,23 +134,17 @@ static void
rb_method_definition_release(rb_method_definition_t *def)
{
if (def != NULL) {
if (def->alias_count_ptr == NULL) {
if (METHOD_DEBUG) fprintf(stderr, " %p-%s:NULL\n", def, rb_id2name(def->original_id));
const int count = def->alias_count;
if (METHOD_DEBUG) assert(count >= 0);
if (count == 0) {
if (METHOD_DEBUG) fprintf(stderr, "-%p-%s:%d\n", def, rb_id2name(def->original_id), count);
xfree(def);
}
else {
int *iptr = def->alias_count_ptr;
if (*iptr == 0) {
if (METHOD_DEBUG) fprintf(stderr, "-%p-%s:%d\n", def, rb_id2name(def->original_id), *iptr);
xfree(iptr);
}
else {
if (METHOD_DEBUG) fprintf(stderr, "-%p-%s:%d->%d\n", def, rb_id2name(def->original_id), *iptr, *iptr-1);
*iptr -= 1;
}
if (METHOD_DEBUG) fprintf(stderr, "-%p-%s:%d->%d\n", def, rb_id2name(def->original_id), count, count-1);
def->alias_count--;
}
xfree(def);
}
}
@ -293,13 +287,9 @@ rb_method_definition_set(rb_method_definition_t *def, void *opts)
}
static rb_method_definition_t *
rb_method_definition_create(rb_method_visibility_t visi, rb_method_type_t type, ID mid, void *opts)
rb_method_definition_create(rb_method_type_t type, ID mid, void *opts)
{
rb_method_definition_t *def = ZALLOC(rb_method_definition_t);
/* def->alias_count_ptr = NULL; already cleared */
def->flags.visi = visi;
def->flags.basic = ruby_running ? FALSE : TRUE;
def->flags.safe = rb_safe_level();
def->type = type;
def->original_id = mid;
if (opts != NULL) rb_method_definition_set(def, opts);
@ -334,47 +324,40 @@ rb_method_definition_reset(const rb_method_entry_t *me, rb_method_definition_t *
}
static rb_method_definition_t *
rb_method_definition_clone(rb_method_definition_t *src_def)
rb_method_definition_addref(rb_method_definition_t *def)
{
int *iptr = src_def->alias_count_ptr;
rb_method_definition_t *def = rb_method_definition_create(src_def->flags.visi, src_def->type, src_def->original_id, NULL);
def->flags.basic = src_def->flags.basic;
def->flags.safe = src_def->flags.safe;
memcpy(&def->body, &src_def->body, sizeof(def->body));
def->alias_count_ptr = src_def->alias_count_ptr;
if (!src_def->alias_count_ptr) {
iptr = def->alias_count_ptr = src_def->alias_count_ptr = ALLOC(int);
*iptr = 0;
}
*iptr += 1;
if (METHOD_DEBUG) fprintf(stderr, "+%p-%s:%d\n", src_def, rb_id2name(src_def->original_id), *iptr);
def->alias_count++;
if (METHOD_DEBUG) fprintf(stderr, "+%p-%s:%d\n", def, rb_id2name(def->original_id), def->alias_count);
return def;
}
rb_method_entry_t *
rb_method_entry_create(ID called_id, VALUE klass, rb_method_definition_t *def)
rb_method_entry_create(ID called_id, VALUE klass, rb_method_visibility_t visi, rb_method_definition_t *def)
{
rb_method_entry_t *me = (rb_method_entry_t *)rb_imemo_new(imemo_ment, (VALUE)NULL, (VALUE)called_id, (VALUE)klass, 0);
METHOD_ENTRY_VISI(me) = visi;
METHOD_ENTRY_BASIC(me) = ruby_running ? FALSE : TRUE;
METHOD_ENTRY_SAFE(me) = rb_safe_level();
rb_method_definition_reset(me, def);
assert(def != NULL);
return me;
}
rb_method_entry_t *
rb_method_entry_clone(const rb_method_entry_t *src_me)
{
rb_method_entry_t *me = rb_method_entry_create(src_me->called_id, src_me->klass, rb_method_definition_clone(src_me->def));
rb_method_entry_t *me = rb_method_entry_create(src_me->called_id, src_me->klass,
METHOD_ENTRY_VISI(src_me),
rb_method_definition_addref(src_me->def));
return me;
}
void
rb_method_entry_copy(rb_method_entry_t *dst, const rb_method_entry_t *src)
{
rb_method_definition_reset(dst, rb_method_definition_clone(src->def));
rb_method_definition_reset(dst, rb_method_definition_addref(src->def));
dst->called_id = src->called_id;
RB_OBJ_WRITE((VALUE)dst, &dst->klass, src->klass);
}
@ -388,8 +371,9 @@ make_method_entry_refined(rb_method_entry_t *me)
rb_vm_check_redefinition_opt_method(me, me->klass);
new_def = rb_method_definition_create(METHOD_VISI_PUBLIC, VM_METHOD_TYPE_REFINED, me->called_id, rb_method_entry_clone(me));
new_def = rb_method_definition_create(VM_METHOD_TYPE_REFINED, me->called_id, rb_method_entry_clone(me));
rb_method_definition_reset(me, new_def);
METHOD_ENTRY_VISI(me) = METHOD_VISI_PUBLIC;
}
void
@ -470,7 +454,7 @@ rb_method_entry_make(VALUE klass, ID mid, rb_method_type_t type, rb_method_defin
if (RTEST(ruby_verbose) &&
type != VM_METHOD_TYPE_UNDEF &&
(old_def->alias_count_ptr == NULL || *old_def->alias_count_ptr == 0) &&
(old_def->alias_count == 0) &&
old_def->type != VM_METHOD_TYPE_UNDEF &&
old_def->type != VM_METHOD_TYPE_ZSUPER &&
old_def->type != VM_METHOD_TYPE_ALIAS) {
@ -496,9 +480,7 @@ rb_method_entry_make(VALUE klass, ID mid, rb_method_type_t type, rb_method_defin
}
}
me = rb_method_entry_create(mid, defined_class, def);
def->flags.visi = visi;
def->flags.safe = rb_safe_level(); /* TODO: maybe we need to remove it. */
me = rb_method_entry_create(mid, defined_class, visi, def);
rb_clear_method_cache_by_class(klass);
@ -545,7 +527,7 @@ method_added(VALUE klass, ID mid)
rb_method_entry_t *
rb_add_method(VALUE klass, ID mid, rb_method_type_t type, void *opts, rb_method_visibility_t visi)
{
rb_method_definition_t *def = rb_method_definition_create(visi, type, mid, opts);
rb_method_definition_t *def = rb_method_definition_create(type, mid, opts);
rb_method_entry_t *me = rb_method_entry_make(klass, mid, type, def, visi, klass);
if (me->def->type == VM_METHOD_TYPE_REFINED && me->def->body.refined.orig_me) { /* TODO: really needed? */
@ -577,9 +559,9 @@ static rb_method_entry_t *
method_entry_set(VALUE klass, ID mid, const rb_method_entry_t *me,
rb_method_visibility_t visi, VALUE defined_class)
{
rb_method_definition_t *def = rb_method_definition_clone(me->def);
rb_method_definition_t *def = rb_method_definition_addref(me->def);
rb_method_entry_t *newme = rb_method_entry_make(klass, mid, me->def->type, def, visi, defined_class);
def->flags.safe = me->def->flags.safe;
METHOD_ENTRY_SAFE(newme) = METHOD_ENTRY_SAFE(me);
method_added(klass, mid);
return newme;
}
@ -891,14 +873,14 @@ rb_export_method(VALUE klass, ID name, rb_method_visibility_t visi)
rb_print_undef(klass, name, 0);
}
if (me->def->flags.visi != visi) {
if (METHOD_ENTRY_VISI(me) != visi) {
rb_vm_check_redefinition_opt_method(me, klass);
if (klass == defined_class || RCLASS_ORIGIN(klass) == defined_class) {
me->def->flags.visi = visi;
METHOD_ENTRY_VISI(me) = visi;
if (me->def->type == VM_METHOD_TYPE_REFINED && me->def->body.refined.orig_me) {
me->def->body.refined.orig_me->def->flags.visi = visi;
METHOD_ENTRY_VISI((rb_method_entry_t *)me->def->body.refined.orig_me) = visi;
}
rb_clear_method_cache_by_class(klass);
}
@ -917,15 +899,13 @@ rb_method_boundp(VALUE klass, ID id, int ex)
const rb_method_entry_t *me = rb_method_entry_without_refinements(klass, id, 0);
if (me != 0) {
rb_method_definition_t *def = me->def;
if ((ex & ~BOUND_RESPONDS) &&
((def->flags.visi == METHOD_VISI_PRIVATE) ||
((ex & BOUND_RESPONDS) && (def->flags.visi == METHOD_VISI_PROTECTED)))) {
((METHOD_ENTRY_VISI(me) == METHOD_VISI_PRIVATE) ||
((ex & BOUND_RESPONDS) && (METHOD_ENTRY_VISI(me) == METHOD_VISI_PROTECTED)))) {
return 0;
}
if (def->type == VM_METHOD_TYPE_NOTIMPLEMENTED) {
if (me->def->type == VM_METHOD_TYPE_NOTIMPLEMENTED) {
if (ex & BOUND_RESPONDS) return 2;
return 0;
}
@ -1157,7 +1137,7 @@ check_definition(VALUE mod, VALUE mid, rb_method_visibility_t visi)
if (!id) return Qfalse;
me = rb_method_entry_without_refinements(mod, id, 0);
if (me) {
if (me->def->flags.visi == visi) return Qtrue;
if (METHOD_ENTRY_VISI(me) == visi) return Qtrue;
}
return Qfalse;
}
@ -1401,11 +1381,11 @@ rb_alias(VALUE klass, ID alias_name, ID original_name)
if (orig_me->def->type == VM_METHOD_TYPE_ZSUPER) {
klass = RCLASS_SUPER(klass);
original_name = orig_me->def->original_id;
visi = orig_me->def->flags.visi;
visi = METHOD_ENTRY_VISI(orig_me);
goto again;
}
if (visi == METHOD_VISI_UNDEF) visi = orig_me->def->flags.visi;
if (visi == METHOD_VISI_UNDEF) visi = METHOD_ENTRY_VISI(orig_me);
if (defined_class != target_klass) { /* inter class/module alias */
VALUE real_owner;
@ -1423,7 +1403,7 @@ rb_alias(VALUE klass, ID alias_name, ID original_name)
RB_OBJ_WRITE(alias_me, &alias_me->klass, defined_class);
alias_me->def->original_id = orig_me->called_id;
*(ID *)&alias_me->def->body.alias.original_me->called_id = alias_name;
alias_me->def->flags.safe = orig_me->def->flags.safe;
METHOD_ENTRY_SAFE(alias_me) = METHOD_ENTRY_SAFE(orig_me);
}
else {
method_entry_set(target_klass, alias_name, orig_me, visi, defined_class);
@ -1723,7 +1703,7 @@ int
rb_method_basic_definition_p(VALUE klass, ID id)
{
const rb_method_entry_t *me = rb_method_entry(klass, id, 0);
return (me && me->def->flags.basic) ? TRUE : FALSE;
return (me && METHOD_ENTRY_BASIC(me)) ? TRUE : FALSE;
}
static inline int