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

make overloaded_cme_table truly weak key map #5316

Merged
merged 1 commit into from Dec 21, 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
8 changes: 1 addition & 7 deletions gc.c
Expand Up @@ -6379,9 +6379,7 @@ mark_method_entry(rb_objspace_t *objspace, const rb_method_entry_t *me)
if (def->body.iseq.iseqptr) gc_mark(objspace, (VALUE)def->body.iseq.iseqptr);
gc_mark(objspace, (VALUE)def->body.iseq.cref);
if (def->iseq_overload && me->defined_class) { // cme
const rb_callable_method_entry_t *monly_cme = rb_vm_lookup_overloaded_cme((const rb_callable_method_entry_t *)me);
if (monly_cme) {
gc_mark(objspace, (VALUE)monly_cme);
if (rb_vm_lookup_overloaded_cme((const rb_callable_method_entry_t *)me)) {
gc_mark_and_pin(objspace, (VALUE)me);
}
}
Expand Down Expand Up @@ -10113,9 +10111,6 @@ gc_ref_update(void *vstart, void *vend, size_t stride, rb_objspace_t * objspace,
extern rb_symbols_t ruby_global_symbols;
#define global_symbols ruby_global_symbols


st_table *rb_vm_overloaded_cme_table(void);

static void
gc_update_references(rb_objspace_t *objspace)
{
Expand Down Expand Up @@ -10151,7 +10146,6 @@ gc_update_references(rb_objspace_t *objspace)
gc_update_table_refs(objspace, objspace->id_to_obj_tbl);
gc_update_table_refs(objspace, global_symbols.str_sym);
gc_update_table_refs(objspace, finalizer_table);
gc_update_table_refs(objspace, rb_vm_overloaded_cme_table());
}

static VALUE
Expand Down
6 changes: 5 additions & 1 deletion vm.c
Expand Up @@ -2540,6 +2540,8 @@ rb_vm_update_references(void *ptr)
vm->top_self = rb_gc_location(vm->top_self);
vm->orig_progname = rb_gc_location(vm->orig_progname);

rb_gc_update_tbl_refs(vm->overloaded_cme_table);

if (vm->coverages) {
vm->coverages = rb_gc_location(vm->coverages);
vm->me2counter = rb_gc_location(vm->me2counter);
Expand Down Expand Up @@ -2637,9 +2639,10 @@ rb_vm_mark(void *ptr)
rb_mark_tbl(vm->loading_table);
}

rb_gc_mark_values(RUBY_NSIG, vm->trap_list.cmd);
rb_gc_mark_values(RUBY_NSIG, vm->trap_list.cmd);

rb_id_table_foreach_values(vm->negative_cme_table, vm_mark_negative_cme, NULL);
rb_mark_tbl_no_pin(vm->overloaded_cme_table);
for (i=0; i<VM_GLOBAL_CC_CACHE_TABLE_SIZE; i++) {
const struct rb_callcache *cc = vm->global_cc_cache_table[i];

Expand Down Expand Up @@ -3801,6 +3804,7 @@ Init_BareVM(void)
vm->objspace = rb_objspace_alloc();
ruby_current_vm_ptr = vm;
vm->negative_cme_table = rb_id_table_create(16);
vm->overloaded_cme_table = st_init_numtable();

Init_native_thread(th);
th->vm = vm;
Expand Down
1 change: 1 addition & 0 deletions vm_core.h
Expand Up @@ -714,6 +714,7 @@ typedef struct rb_vm_struct {
int builtin_inline_index;

struct rb_id_table *negative_cme_table;
st_table *overloaded_cme_table; // cme -> overloaded_cme

#ifndef VM_GLOBAL_CC_CACHE_TABLE_SIZE
#define VM_GLOBAL_CC_CACHE_TABLE_SIZE 1023
Expand Down
38 changes: 15 additions & 23 deletions vm_method.c
Expand Up @@ -150,7 +150,7 @@ static rb_method_entry_t *rb_method_entry_alloc(ID called_id, VALUE owner, VALUE
const rb_method_entry_t * rb_method_entry_clone(const rb_method_entry_t *src_me);
static const rb_callable_method_entry_t *complemented_callable_method_entry(VALUE klass, ID id);
static const rb_callable_method_entry_t *lookup_overloaded_cme(const rb_callable_method_entry_t *cme);
static void delete_overloaded_cme(const rb_callable_method_entry_t *cme);


static void
clear_method_cache_by_id_in_class(VALUE klass, ID mid)
Expand Down Expand Up @@ -216,7 +216,6 @@ clear_method_cache_by_id_in_class(VALUE klass, ID mid)
rb_callable_method_entry_t *monly_cme = (rb_callable_method_entry_t *)lookup_overloaded_cme(cme);
if (monly_cme) {
vm_cme_invalidate(monly_cme);
delete_overloaded_cme(monly_cme);
}
}
}
Expand Down Expand Up @@ -393,6 +392,8 @@ rb_method_definition_release(rb_method_definition_t *def, int complemented)
}
}

static void delete_overloaded_cme(const rb_callable_method_entry_t *cme);

void
rb_free_method_entry(const rb_method_entry_t *me)
{
Expand Down Expand Up @@ -922,12 +923,12 @@ rb_method_entry_make(VALUE klass, ID mid, VALUE defined_class, rb_method_visibil
}

static rb_method_entry_t *rb_method_entry_alloc(ID called_id, VALUE owner, VALUE defined_class, const rb_method_definition_t *def);
static st_table *overloaded_cme_table;

st_table *
rb_vm_overloaded_cme_table(void)
static st_table *
overloaded_cme_table(void)
{
return overloaded_cme_table;
VM_ASSERT(GET_VM()->overloaded_cme_table != NULL);
return GET_VM()->overloaded_cme_table;
}

#if VM_CHECK_MODE > 0
Expand All @@ -943,7 +944,7 @@ void
rb_vm_dump_overloaded_cme_table(void)
{
fprintf(stderr, "== rb_vm_dump_overloaded_cme_table\n");
st_foreach(overloaded_cme_table, vm_dump_overloaded_cme_table, 0);
st_foreach(overloaded_cme_table(), vm_dump_overloaded_cme_table, 0);
}
#endif

Expand All @@ -956,10 +957,7 @@ lookup_overloaded_cme_i(st_data_t *key, st_data_t *value, st_data_t data, int ex
const rb_callable_method_entry_t **ptr = (const rb_callable_method_entry_t **)data;

if (rb_objspace_garbage_object_p((VALUE)cme) ||
rb_objspace_garbage_object_p((VALUE)monly_cme) ||
METHOD_ENTRY_INVALIDATED(cme) ||
METHOD_ENTRY_INVALIDATED(monly_cme)) {

rb_objspace_garbage_object_p((VALUE)monly_cme)) {
*ptr = NULL;
return ST_DELETE;
}
Expand All @@ -977,14 +975,8 @@ lookup_overloaded_cme(const rb_callable_method_entry_t *cme)
ASSERT_vm_locking();

const rb_callable_method_entry_t *monly_cme = NULL;
st_update(overloaded_cme_table, (st_data_t)cme, lookup_overloaded_cme_i, (st_data_t)&monly_cme);

if (monly_cme) {
return monly_cme;
}
else {
return NULL;
}
st_update(overloaded_cme_table(), (st_data_t)cme, lookup_overloaded_cme_i, (st_data_t)&monly_cme);
return monly_cme;
}

// used by gc.c
Expand All @@ -998,15 +990,15 @@ static void
delete_overloaded_cme(const rb_callable_method_entry_t *cme)
{
ASSERT_vm_locking();
st_delete(overloaded_cme_table, (st_data_t *)&cme, NULL);
st_delete(overloaded_cme_table(), (st_data_t *)&cme, NULL);
}

static const rb_callable_method_entry_t *
get_overloaded_cme(const rb_callable_method_entry_t *cme)
{
const rb_callable_method_entry_t *monly_cme = lookup_overloaded_cme(cme);

if (monly_cme) {
if (monly_cme && !METHOD_ENTRY_INVALIDATED(monly_cme)) {
return monly_cme;
}
else {
Expand All @@ -1021,7 +1013,7 @@ get_overloaded_cme(const rb_callable_method_entry_t *cme)
def);

ASSERT_vm_locking();
st_insert(overloaded_cme_table, (st_data_t)cme, (st_data_t)me);
st_insert(overloaded_cme_table(), (st_data_t)cme, (st_data_t)me);

METHOD_ENTRY_VISI_SET(me, METHOD_ENTRY_VISI(cme));
return (rb_callable_method_entry_t *)me;
Expand Down Expand Up @@ -2828,7 +2820,7 @@ obj_respond_to_missing(VALUE obj, VALUE mid, VALUE priv)
void
Init_Method(void)
{
overloaded_cme_table = st_init_numtable();
//
}

void
Expand Down