Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 51 additions & 63 deletions class.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,46 +135,42 @@ duplicate_classext_id_table(struct rb_id_table *orig, bool init_missing)
return tbl;
}

static rb_const_entry_t *
duplicate_classext_const_entry(rb_const_entry_t *src, VALUE klass)
static VALUE
duplicate_classext_const_entry(VALUE src_value)
{
rb_const_entry_t *src = (rb_const_entry_t *)src_value;

// See also: setup_const_entry (variable.c)
rb_const_entry_t *dst = ZALLOC(rb_const_entry_t);
VALUE dst_value = rb_const_entry_new();
rb_const_entry_t *dst = (rb_const_entry_t *)dst_value;

dst->flag = src->flag;
dst->line = src->line;
RB_OBJ_WRITE(klass, &dst->value, src->value);
RB_OBJ_WRITE(klass, &dst->file, src->file);
MEMCPY(dst, src, rb_const_entry_t, 1);
RB_OBJ_WRITTEN(dst_value, Qundef, src->value);
RB_OBJ_WRITTEN(dst_value, Qundef, src->file);

return dst;
return dst_value;
}

static enum rb_id_table_iterator_result
duplicate_classext_const_tbl_i(ID key, VALUE value, void *data)
{
struct duplicate_id_tbl_data *arg = (struct duplicate_id_tbl_data *)data;
rb_const_entry_t *entry = duplicate_classext_const_entry((rb_const_entry_t *)value, arg->klass);
VALUE dst = (VALUE)data;

rb_id_table_insert(arg->tbl, key, (VALUE)entry);
VALUE entry = duplicate_classext_const_entry(value);
rb_marked_id_table_insert(dst, key, entry);

return ID_TABLE_CONTINUE;
}

static struct rb_id_table *
duplicate_classext_const_tbl(struct rb_id_table *src, VALUE klass)
static VALUE
duplicate_classext_const_tbl(VALUE src)
{
struct rb_id_table *dst;

if (!src)
return NULL;

dst = rb_id_table_create(rb_id_table_size(src));
if (!src) {
return 0;
}

struct duplicate_id_tbl_data data = {
.tbl = dst,
.klass = klass,
};
rb_id_table_foreach(src, duplicate_classext_const_tbl_i, (void *)&data);
VALUE dst = rb_marked_id_table_new(rb_marked_id_table_size(src));
rb_marked_id_table_foreach(src, duplicate_classext_const_tbl_i, (void *)dst);

return dst;
}
Expand Down Expand Up @@ -308,11 +304,11 @@ rb_class_duplicate_classext(rb_classext_t *orig, VALUE klass, const rb_namespace
}

if (RCLASSEXT_SHARED_CONST_TBL(orig)) {
RCLASSEXT_CONST_TBL(ext) = RCLASSEXT_CONST_TBL(orig);
RB_OBJ_WRITE(klass, &RCLASSEXT_CONST_TBL(ext), RCLASSEXT_CONST_TBL(orig));
RCLASSEXT_SHARED_CONST_TBL(ext) = true;
}
else {
RCLASSEXT_CONST_TBL(ext) = duplicate_classext_const_tbl(RCLASSEXT_CONST_TBL(orig), klass);
RB_OBJ_WRITE(klass, &RCLASSEXT_CONST_TBL(ext), duplicate_classext_const_tbl(RCLASSEXT_CONST_TBL(orig)));
RCLASSEXT_SHARED_CONST_TBL(ext) = false;
}
/*
Expand Down Expand Up @@ -898,29 +894,22 @@ clone_method_i(ID key, VALUE value, void *data)
return ID_TABLE_CONTINUE;
}

struct clone_const_arg {
VALUE klass;
struct rb_id_table *tbl;
};

static int
clone_const(ID key, const rb_const_entry_t *ce, struct clone_const_arg *arg)
static enum rb_id_table_iterator_result
clone_const_i(ID key, VALUE value, void *data)
{
rb_const_entry_t *nce = ALLOC(rb_const_entry_t);
VALUE new_table = (VALUE)data;
const rb_const_entry_t *ce = (const rb_const_entry_t *)value;

VALUE nce_value = rb_const_entry_new();
rb_const_entry_t *nce = (rb_const_entry_t *)nce_value;
MEMCPY(nce, ce, rb_const_entry_t, 1);
RB_OBJ_WRITTEN(arg->klass, Qundef, ce->value);
RB_OBJ_WRITTEN(arg->klass, Qundef, ce->file);
RB_OBJ_WRITTEN(nce_value, Qundef, ce->value);
RB_OBJ_WRITTEN(nce_value, Qundef, ce->file);

rb_id_table_insert(arg->tbl, key, (VALUE)nce);
rb_marked_id_table_insert(new_table, key, (VALUE)nce);
return ID_TABLE_CONTINUE;
}

static enum rb_id_table_iterator_result
clone_const_i(ID key, VALUE value, void *data)
{
return clone_const(key, (const rb_const_entry_t *)value, data);
}

static void
class_init_copy_check(VALUE clone, VALUE orig)
{
Expand Down Expand Up @@ -964,7 +953,6 @@ static void
copy_tables(VALUE clone, VALUE orig)
{
if (RCLASS_CONST_TBL(clone)) {
rb_free_const_table(RCLASS_CONST_TBL(clone));
RCLASS_WRITE_CONST_TBL(clone, 0, false);
}
if (RCLASS_CVC_TBL(orig)) {
Expand All @@ -982,14 +970,12 @@ copy_tables(VALUE clone, VALUE orig)
if (!RB_TYPE_P(clone, T_ICLASS)) {
rb_fields_tbl_copy(clone, orig);
}
if (RCLASS_CONST_TBL(orig)) {
struct clone_const_arg arg;
struct rb_id_table *const_tbl;
struct rb_id_table *orig_tbl = RCLASS_CONST_TBL(orig);
arg.tbl = const_tbl = rb_id_table_create(rb_id_table_size(orig_tbl));
arg.klass = clone;
rb_id_table_foreach(orig_tbl, clone_const_i, &arg);
RCLASS_WRITE_CONST_TBL(clone, const_tbl, false);

VALUE const_table = RCLASS_CONST_TBL(orig);
if (const_table) {
VALUE new_const_tbl = rb_marked_id_table_new(rb_marked_id_table_size(const_table));
rb_marked_id_table_foreach(const_table, clone_const_i, (void *)new_const_tbl);
RCLASS_WRITE_CONST_TBL(clone, new_const_tbl, false);
}
}

Expand Down Expand Up @@ -1177,14 +1163,15 @@ rb_singleton_class_clone_and_attach(VALUE obj, VALUE attach)

rb_class_set_super(clone, RCLASS_SUPER(klass));
rb_fields_tbl_copy(clone, klass);
if (RCLASS_CONST_TBL(klass)) {
struct clone_const_arg arg;
struct rb_id_table *table;
arg.tbl = table = rb_id_table_create(0);
arg.klass = clone;
rb_id_table_foreach(RCLASS_CONST_TBL(klass), clone_const_i, &arg);
RCLASS_SET_CONST_TBL(clone, table, false);


VALUE const_table = RCLASS_CONST_TBL(klass);
if (const_table) {
VALUE new_const_tbl = rb_marked_id_table_new(rb_marked_id_table_size(const_table));
rb_marked_id_table_foreach(const_table, clone_const_i, (void *)new_const_tbl);
RCLASS_WRITE_CONST_TBL(clone, new_const_tbl, false);
}

if (!UNDEF_P(attach)) {
rb_singleton_class_attached(clone, attach);
}
Expand Down Expand Up @@ -1669,7 +1656,7 @@ rb_include_class_new(VALUE module, VALUE super)
RCLASS_SET_CONST_TBL(klass, RCLASS_WRITABLE_CONST_TBL(module), true);
}
else {
RCLASS_WRITE_CONST_TBL(module, rb_id_table_create(0), false);
RCLASS_WRITE_CONST_TBL(module, rb_marked_id_table_new(0), false);
RCLASS_SET_CONST_TBL(klass, RCLASS_WRITABLE_CONST_TBL(module), true);
}

Expand Down Expand Up @@ -1863,9 +1850,10 @@ do_include_modules_at(const VALUE klass, VALUE c, VALUE module, int search_super
RUBY_ASSERT(BUILTIN_TYPE(c) == T_MODULE);
}

tbl = RCLASS_CONST_TBL(module);
if (tbl && rb_id_table_size(tbl))
rb_id_table_foreach(tbl, clear_constant_cache_i, NULL);
VALUE const_table = RCLASS_CONST_TBL(module);
if (const_table) {
rb_marked_id_table_foreach(const_table, clear_constant_cache_i, NULL);
}
skip:
module = RCLASS_SUPER(module);
}
Expand Down
8 changes: 5 additions & 3 deletions constant.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,19 @@ typedef enum {
#define RB_CONST_DEPRECATED_P(ce) \
((ce)->flag & CONST_DEPRECATED)

// imemo_constentry
typedef struct rb_const_entry_struct {
rb_const_flag_t flag;
int line;
VALUE _imemo_flags;

VALUE value; /* should be mark */
VALUE file; /* should be mark */
int line;
rb_const_flag_t flag;
} rb_const_entry_t;

VALUE rb_mod_private_constant(int argc, const VALUE *argv, VALUE obj);
VALUE rb_mod_public_constant(int argc, const VALUE *argv, VALUE obj);
VALUE rb_mod_deprecate_constant(int argc, const VALUE *argv, VALUE obj);
void rb_free_const_table(struct rb_id_table *tbl);
VALUE rb_const_source_location(VALUE, ID);

int rb_autoloading_value(VALUE mod, ID id, VALUE *value, rb_const_flag_t *flag);
Expand Down
1 change: 1 addition & 0 deletions debug_counter.h
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ RB_DEBUG_COUNTER(obj_imemo_parser_strterm)
RB_DEBUG_COUNTER(obj_imemo_callinfo)
RB_DEBUG_COUNTER(obj_imemo_callcache)
RB_DEBUG_COUNTER(obj_imemo_constcache)
RB_DEBUG_COUNTER(obj_imemo_constentry)
RB_DEBUG_COUNTER(obj_imemo_fields)

RB_DEBUG_COUNTER(opt_new_hit)
Expand Down
1 change: 1 addition & 0 deletions ext/objspace/objspace.c
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@ count_imemo_objects(int argc, VALUE *argv, VALUE self)
INIT_IMEMO_TYPE_ID(imemo_callinfo);
INIT_IMEMO_TYPE_ID(imemo_callcache);
INIT_IMEMO_TYPE_ID(imemo_constcache);
INIT_IMEMO_TYPE_ID(imemo_constentry);
INIT_IMEMO_TYPE_ID(imemo_fields);
#undef INIT_IMEMO_TYPE_ID
}
Expand Down
55 changes: 3 additions & 52 deletions gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1212,9 +1212,6 @@ classext_free(rb_classext_t *ext, bool is_prime, VALUE namespace, void *arg)

rb_id_table_free(RCLASSEXT_M_TBL(ext));
rb_cc_tbl_free(RCLASSEXT_CC_TBL(ext), args->klass);
if (!RCLASSEXT_SHARED_CONST_TBL(ext) && (tbl = RCLASSEXT_CONST_TBL(ext)) != NULL) {
rb_free_const_table(tbl);
}
if ((tbl = RCLASSEXT_CVC_TBL(ext)) != NULL) {
rb_id_table_foreach_values(tbl, cvar_table_free_i, NULL);
rb_id_table_free(tbl);
Expand Down Expand Up @@ -2295,9 +2292,6 @@ classext_memsize(rb_classext_t *ext, bool prime, VALUE namespace, void *arg)
if (RCLASSEXT_CVC_TBL(ext)) {
s += rb_id_table_memsize(RCLASSEXT_CVC_TBL(ext));
}
if (RCLASSEXT_CONST_TBL(ext)) {
s += rb_id_table_memsize(RCLASSEXT_CONST_TBL(ext));
}
if (RCLASSEXT_CC_TBL(ext)) {
s += cc_table_memsize(RCLASSEXT_CC_TBL(ext));
}
Expand Down Expand Up @@ -2813,23 +2807,6 @@ mark_m_tbl(void *objspace, struct rb_id_table *tbl)
}
}

static enum rb_id_table_iterator_result
mark_const_entry_i(VALUE value, void *objspace)
{
const rb_const_entry_t *ce = (const rb_const_entry_t *)value;

gc_mark_internal(ce->value);
gc_mark_internal(ce->file);
return ID_TABLE_CONTINUE;
}

static void
mark_const_tbl(rb_objspace_t *objspace, struct rb_id_table *tbl)
{
if (!tbl) return;
rb_id_table_foreach_values(tbl, mark_const_entry_i, objspace);
}

struct mark_cc_entry_args {
rb_objspace_t *objspace;
VALUE klass;
Expand Down Expand Up @@ -3115,9 +3092,7 @@ gc_mark_classext_module(rb_classext_t *ext, bool prime, VALUE namespace, void *a
}
mark_m_tbl(objspace, RCLASSEXT_M_TBL(ext));
gc_mark_internal(RCLASSEXT_FIELDS_OBJ(ext));
if (!RCLASSEXT_SHARED_CONST_TBL(ext) && RCLASSEXT_CONST_TBL(ext)) {
mark_const_tbl(objspace, RCLASSEXT_CONST_TBL(ext));
}
gc_mark_internal(RCLASSEXT_CONST_TBL(ext));
mark_m_tbl(objspace, RCLASSEXT_CALLABLE_M_TBL(ext));
mark_cc_tbl(objspace, RCLASSEXT_CC_TBL(ext), obj);
mark_cvc_tbl(objspace, RCLASSEXT_CVC_TBL(ext));
Expand Down Expand Up @@ -3756,29 +3731,6 @@ update_cvc_tbl(void *objspace, struct rb_id_table *tbl)
rb_id_table_foreach_values(tbl, update_cvc_tbl_i, objspace);
}

static enum rb_id_table_iterator_result
update_const_tbl_i(VALUE value, void *objspace)
{
rb_const_entry_t *ce = (rb_const_entry_t *)value;

if (rb_gc_impl_object_moved_p(objspace, ce->value)) {
ce->value = gc_location_internal(objspace, ce->value);
}

if (rb_gc_impl_object_moved_p(objspace, ce->file)) {
ce->file = gc_location_internal(objspace, ce->file);
}

return ID_TABLE_CONTINUE;
}

static void
update_const_tbl(void *objspace, struct rb_id_table *tbl)
{
if (!tbl) return;
rb_id_table_foreach_values(tbl, update_const_tbl_i, objspace);
}

static void
update_subclasses(void *objspace, rb_classext_t *ext)
{
Expand Down Expand Up @@ -3828,9 +3780,7 @@ update_classext(rb_classext_t *ext, bool is_prime, VALUE namespace, void *arg)
update_m_tbl(objspace, RCLASSEXT_M_TBL(ext));

UPDATE_IF_MOVED(objspace, ext->fields_obj);
if (!RCLASSEXT_SHARED_CONST_TBL(ext)) {
update_const_tbl(objspace, RCLASSEXT_CONST_TBL(ext));
}
UPDATE_IF_MOVED(objspace, RCLASSEXT_CONST_TBL(ext));
update_cc_tbl(objspace, RCLASSEXT_CC_TBL(ext));
update_cvc_tbl(objspace, RCLASSEXT_CVC_TBL(ext));
update_superclasses(objspace, ext);
Expand All @@ -3849,6 +3799,7 @@ update_iclass_classext(rb_classext_t *ext, bool is_prime, VALUE namespace, void
UPDATE_IF_MOVED(objspace, RCLASSEXT_SUPER(ext));
}
update_m_tbl(objspace, RCLASSEXT_M_TBL(ext));
UPDATE_IF_MOVED(objspace, RCLASSEXT_CONST_TBL(ext));
update_m_tbl(objspace, RCLASSEXT_CALLABLE_M_TBL(ext));
update_cc_tbl(objspace, RCLASSEXT_CC_TBL(ext));
update_subclasses(objspace, ext);
Expand Down
2 changes: 1 addition & 1 deletion gems/bundled_gems
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ matrix 0.4.3 https://github.com/ruby/matrix
prime 0.1.4 https://github.com/ruby/prime
rbs 3.9.4 https://github.com/ruby/rbs
typeprof 0.30.1 https://github.com/ruby/typeprof
debug 1.11.0 https://github.com/ruby/debug
debug 1.11.0 https://github.com/byroot/debug 3f7332a15e7475bf94aa6f077cc9cc9f3210b0aa
racc 1.8.1 https://github.com/ruby/racc
mutex_m 0.3.0 https://github.com/ruby/mutex_m
getoptlong 0.2.1 https://github.com/ruby/getoptlong
Expand Down
Loading
Loading