Skip to content

Commit

Permalink
per-method serial number
Browse files Browse the repository at this point in the history
Methods and their definitions can be allocated/deallocated on-the-fly.
One pathological situation is when a method is deallocated then another
one is allocated immediately after that.  Address of those old/new method
entries/definitions can be the same then, depending on underlying
malloc/free implementation.

So pointer comparison is insufficient.  We have to check the contents.
To do so we introduce def->method_serial, which is an integer unique to
that specific method definition.

PS: Note that method_serial being uintptr_t rather than rb_serial_t is
intentional.  This is because rb_serial_t can be bigger than a pointer
on a 32bit system (rb_serial_t is at least 64bit).  In order to preserve
old packing of struct rb_call_cache, rb_serial_t is inappropriate.
  • Loading branch information
shyouhei committed Dec 18, 2019
1 parent 77e3078 commit f054f11
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 6 deletions.
4 changes: 2 additions & 2 deletions internal.h
Expand Up @@ -2350,7 +2350,7 @@ struct rb_call_cache {
(CACHELINE
- sizeof(rb_serial_t) /* method_state */
- sizeof(struct rb_callable_method_entry_struct *) /* me */
- sizeof(struct rb_callable_method_definition_struct *) /* def */
- sizeof(uintptr_t) /* method_serial */
- sizeof(enum method_missing_reason) /* aux */
- sizeof(VALUE (*)( /* call */
struct rb_execution_context_struct *e,
Expand All @@ -2362,7 +2362,7 @@ struct rb_call_cache {

/* inline cache: values */
const struct rb_callable_method_entry_struct *me;
const struct rb_method_definition_struct *def;
uintptr_t method_serial; /* me->def->method_serial */

VALUE (*call)(struct rb_execution_context_struct *ec,
struct rb_control_frame_struct *cfp,
Expand Down
1 change: 1 addition & 0 deletions method.h
Expand Up @@ -177,6 +177,7 @@ struct rb_method_definition_struct {
} body;

ID original_id;
uintptr_t method_serial;
};

typedef struct rb_method_definition_struct rb_method_definition_t;
Expand Down
2 changes: 1 addition & 1 deletion vm_eval.c
Expand Up @@ -47,7 +47,7 @@ rb_vm_call0(rb_execution_context_t *ec, VALUE recv, ID id, int argc, const VALUE
{
struct rb_calling_info calling = { Qundef, recv, argc, kw_splat, };
struct rb_call_info ci = { id, (kw_splat ? VM_CALL_KW_SPLAT : 0), argc, };
struct rb_call_cache cc = { 0, { 0, }, me, me->def, vm_call_general, { 0, }, };
struct rb_call_cache cc = { 0, { 0, }, me, me->def->method_serial, vm_call_general, { 0, }, };
struct rb_call_data cd = { cc, ci, };
return vm_call0_body(ec, &calling, &cd, argv);
}
Expand Down
4 changes: 2 additions & 2 deletions vm_insnhelper.c
Expand Up @@ -1443,7 +1443,7 @@ calccall(const struct rb_call_data *cd, const rb_callable_method_entry_t *me)
RB_DEBUG_COUNTER_INC(mc_miss_by_distinct);
return vm_call_general; /* normal cases */
}
else if (UNLIKELY(cc->def != me->def)) {
else if (UNLIKELY(cc->method_serial != me->def->method_serial)) {
RB_DEBUG_COUNTER_INC(mc_miss_by_refine);
return vm_call_general; /* cc->me was refined elsewhere */
}
Expand Down Expand Up @@ -1475,7 +1475,7 @@ rb_vm_search_method_slowpath(struct rb_call_data *cd, VALUE klass)
GET_GLOBAL_METHOD_STATE(),
{ RCLASS_SERIAL(klass) },
me,
me ? me->def : NULL,
me ? me->def->method_serial : 0,
call,
};
if (call != vm_call_general) {
Expand Down
2 changes: 1 addition & 1 deletion vm_insnhelper.h
Expand Up @@ -132,7 +132,7 @@ static inline void
CC_SET_ME(CALL_CACHE cc, const rb_callable_method_entry_t *me)
{
cc->me = me;
cc->def = me ? me->def : NULL;
cc->method_serial = me ? me->def->method_serial : 0;
}

#define GET_BLOCK_HANDLER() (GET_LEP()[VM_ENV_DATA_INDEX_SPECVAL])
Expand Down
2 changes: 2 additions & 0 deletions vm_method.c
Expand Up @@ -351,6 +351,8 @@ rb_method_definition_create(rb_method_type_t type, ID mid)
def = ZALLOC(rb_method_definition_t);
def->type = type;
def->original_id = mid;
static uintptr_t method_serial = 1;
def->method_serial = method_serial++;
return def;
}

Expand Down

0 comments on commit f054f11

Please sign in to comment.