Feature #11614 ยป 0001-use-id_table-for-constant-tables.patch
| class.c | ||
|---|---|---|
|
struct clone_const_arg {
|
||
|
VALUE klass;
|
||
|
st_table *tbl;
|
||
|
struct rb_id_table *tbl;
|
||
|
};
|
||
|
static int
|
||
| ... | ... | |
|
RB_OBJ_WRITTEN(arg->klass, Qundef, ce->value);
|
||
|
RB_OBJ_WRITTEN(arg->klass, Qundef, ce->file);
|
||
|
st_insert(arg->tbl, key, (st_data_t)nce);
|
||
|
return ST_CONTINUE;
|
||
|
rb_id_table_insert(arg->tbl, key, (VALUE)nce);
|
||
|
return ID_TABLE_CONTINUE;
|
||
|
}
|
||
|
static int
|
||
|
clone_const_i(st_data_t key, st_data_t value, st_data_t data)
|
||
|
static enum rb_id_table_iterator_result
|
||
|
clone_const_i(ID key, VALUE value, void *data)
|
||
|
{
|
||
|
return clone_const((ID)key, (const rb_const_entry_t *)value, (struct clone_const_arg *)data);
|
||
|
return clone_const(key, (const rb_const_entry_t *)value, data);
|
||
|
}
|
||
|
static void
|
||
| ... | ... | |
|
if (RCLASS_CONST_TBL(orig)) {
|
||
|
struct clone_const_arg arg;
|
||
|
RCLASS_CONST_TBL(clone) = st_init_numtable();
|
||
|
arg.tbl = RCLASS_CONST_TBL(clone) = rb_id_table_create(0);
|
||
|
arg.klass = clone;
|
||
|
arg.tbl = RCLASS_CONST_TBL(clone);
|
||
|
st_foreach(RCLASS_CONST_TBL(orig), clone_const_i, (st_data_t)&arg);
|
||
|
rb_id_table_foreach(RCLASS_CONST_TBL(orig), clone_const_i, &arg);
|
||
|
}
|
||
|
if (RCLASS_M_TBL(orig)) {
|
||
|
struct clone_method_arg arg;
|
||
| ... | ... | |
|
}
|
||
|
if (RCLASS_CONST_TBL(klass)) {
|
||
|
struct clone_const_arg arg;
|
||
|
RCLASS_CONST_TBL(clone) = st_init_numtable();
|
||
|
arg.tbl = RCLASS_CONST_TBL(clone) = rb_id_table_create(0);
|
||
|
arg.klass = clone;
|
||
|
arg.tbl = RCLASS_CONST_TBL(clone);
|
||
|
st_foreach(RCLASS_CONST_TBL(klass), clone_const_i, (st_data_t)&arg);
|
||
|
rb_id_table_foreach(RCLASS_CONST_TBL(klass), clone_const_i, &arg);
|
||
|
}
|
||
|
if (attach != Qundef) {
|
||
|
rb_singleton_class_attached(clone, attach);
|
||
| ... | ... | |
|
RCLASS_IV_TBL(module) = st_init_numtable();
|
||
|
}
|
||
|
if (!RCLASS_CONST_TBL(module)) {
|
||
|
RCLASS_CONST_TBL(module) = st_init_numtable();
|
||
|
RCLASS_CONST_TBL(module) = rb_id_table_create(0);
|
||
|
}
|
||
|
RCLASS_IV_TBL(klass) = RCLASS_IV_TBL(module);
|
||
|
RCLASS_CONST_TBL(klass) = RCLASS_CONST_TBL(module);
|
||
| ... | ... | |
|
while (module) {
|
||
|
int superclass_seen = FALSE;
|
||
|
struct rb_id_table *tbl;
|
||
|
if (RCLASS_ORIGIN(module) != module)
|
||
|
goto skip;
|
||
| ... | ... | |
|
rb_id_table_foreach(RMODULE_M_TBL(module), add_refined_method_entry_i, (void *)refined_class);
|
||
|
FL_SET(c, RMODULE_INCLUDED_INTO_REFINEMENT);
|
||
|
}
|
||
|
if (RMODULE_M_TBL(module) && rb_id_table_size(RMODULE_M_TBL(module))) method_changed = 1;
|
||
|
if (RMODULE_CONST_TBL(module) && RMODULE_CONST_TBL(module)->num_entries) constant_changed = 1;
|
||
|
tbl = RMODULE_M_TBL(module);
|
||
|
if (tbl && rb_id_table_size(tbl)) method_changed = 1;
|
||
|
tbl = RMODULE_CONST_TBL(module);
|
||
|
if (tbl && rb_id_table_size(tbl)) constant_changed = 1;
|
||
|
skip:
|
||
|
module = RCLASS_SUPER(module);
|
||
|
}
|
||
| constant.h | ||
|---|---|---|
|
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(st_table *tbl);
|
||
|
void rb_free_const_table(struct rb_id_table *tbl);
|
||
|
VALUE rb_public_const_get(VALUE klass, ID id);
|
||
|
VALUE rb_public_const_get_at(VALUE klass, ID id);
|
||
|
VALUE rb_public_const_get_from(VALUE klass, ID id);
|
||
| gc.c | ||
|---|---|---|
|
return FALSE;
|
||
|
}
|
||
|
static int
|
||
|
free_const_entry_i(st_data_t key, st_data_t value, st_data_t data)
|
||
|
static enum rb_id_table_iterator_result
|
||
|
free_const_entry_i(VALUE value, void *data)
|
||
|
{
|
||
|
rb_const_entry_t *ce = (rb_const_entry_t *)value;
|
||
|
xfree(ce);
|
||
|
return ST_CONTINUE;
|
||
|
return ID_TABLE_CONTINUE;
|
||
|
}
|
||
|
void
|
||
|
rb_free_const_table(st_table *tbl)
|
||
|
rb_free_const_table(struct rb_id_table *tbl)
|
||
|
{
|
||
|
st_foreach(tbl, free_const_entry_i, 0);
|
||
|
st_free_table(tbl);
|
||
|
rb_id_table_foreach_values(tbl, free_const_entry_i, 0);
|
||
|
rb_id_table_free(tbl);
|
||
|
}
|
||
|
static inline void
|
||
| ... | ... | |
|
size += st_memsize(RCLASS(obj)->ptr->iv_tbl);
|
||
|
}
|
||
|
if (RCLASS(obj)->ptr->const_tbl) {
|
||
|
size += st_memsize(RCLASS(obj)->ptr->const_tbl);
|
||
|
size += rb_id_table_memsize(RCLASS(obj)->ptr->const_tbl);
|
||
|
}
|
||
|
size += sizeof(rb_classext_t);
|
||
|
}
|
||
| ... | ... | |
|
}
|
||
|
}
|
||
|
static int
|
||
|
mark_const_entry_i(st_data_t key, st_data_t value, st_data_t data)
|
||
|
static enum rb_id_table_iterator_result
|
||
|
mark_const_entry_i(VALUE value, void *data)
|
||
|
{
|
||
|
const rb_const_entry_t *ce = (const rb_const_entry_t *)value;
|
||
|
rb_objspace_t *objspace = (rb_objspace_t *)data;
|
||
|
rb_objspace_t *objspace = data;
|
||
|
gc_mark(objspace, ce->value);
|
||
|
gc_mark(objspace, ce->file);
|
||
|
return ST_CONTINUE;
|
||
|
return ID_TABLE_CONTINUE;
|
||
|
}
|
||
|
static void
|
||
|
mark_const_tbl(rb_objspace_t *objspace, st_table *tbl)
|
||
|
mark_const_tbl(rb_objspace_t *objspace, struct rb_id_table *tbl)
|
||
|
{
|
||
|
if (!tbl) return;
|
||
|
st_foreach(tbl, mark_const_entry_i, (st_data_t)objspace);
|
||
|
rb_id_table_foreach_values(tbl, mark_const_entry_i, objspace);
|
||
|
}
|
||
|
#if STACK_GROW_DIRECTION < 0
|
||
| internal.h | ||
|---|---|---|
|
struct rb_classext_struct {
|
||
|
struct st_table *iv_index_tbl;
|
||
|
struct st_table *iv_tbl;
|
||
|
struct st_table *const_tbl;
|
||
|
struct rb_id_table *const_tbl;
|
||
|
struct rb_id_table *callable_m_tbl;
|
||
|
rb_subclass_entry_t *subclasses;
|
||
|
rb_subclass_entry_t **parent_subclasses;
|
||
| object.c | ||
|---|---|---|
|
{
|
||
|
ID id = id_for_setter(name, const, "wrong constant name %"PRIsVALUE);
|
||
|
rb_const_set(mod, id, value);
|
||
|
/*
|
||
|
* Resolve and cache class name immediately to resolve ambiguity
|
||
|
* and avoid order-dependency on const_tbl
|
||
|
*/
|
||
|
if (RB_TYPE_P(value, T_MODULE) || RB_TYPE_P(value, T_CLASS)) {
|
||
|
rb_class_name(value);
|
||
|
}
|
||
|
return value;
|
||
|
}
|
||
| variable.c | ||
|---|---|---|
|
#include "internal.h"
|
||
|
#include "ruby/st.h"
|
||
|
#include "ruby/util.h"
|
||
|
#include "id_table.h"
|
||
|
#include "constant.h"
|
||
|
#include "id.h"
|
||
| ... | ... | |
|
static void check_before_mod_set(VALUE, ID, VALUE, const char *);
|
||
|
static void setup_const_entry(rb_const_entry_t *, VALUE, VALUE, rb_const_flag_t);
|
||
|
static int const_update(st_data_t *, st_data_t *, st_data_t, int);
|
||
|
static st_table *generic_iv_tbl;
|
||
|
static st_table *generic_iv_tbl_compat;
|
||
| ... | ... | |
|
return path;
|
||
|
}
|
||
|
static int
|
||
|
fc_i(st_data_t k, st_data_t v, st_data_t a)
|
||
|
static enum rb_id_table_iterator_result
|
||
|
fc_i(ID key, VALUE v, void *a)
|
||
|
{
|
||
|
ID key = (ID)k;
|
||
|
rb_const_entry_t *ce = (rb_const_entry_t *)v;
|
||
|
struct fc_result *res = (struct fc_result *)a;
|
||
|
struct fc_result *res = a;
|
||
|
VALUE value = ce->value;
|
||
|
if (!rb_is_const_id(key)) return ST_CONTINUE;
|
||
|
if (!rb_is_const_id(key)) return ID_TABLE_CONTINUE;
|
||
|
if (value == res->klass && (!res->preferred || key == res->preferred)) {
|
||
|
res->path = fc_path(res, key);
|
||
|
return ST_STOP;
|
||
|
return ID_TABLE_STOP;
|
||
|
}
|
||
|
if (RB_TYPE_P(value, T_MODULE) || RB_TYPE_P(value, T_CLASS)) {
|
||
|
if (!RCLASS_CONST_TBL(value)) return ST_CONTINUE;
|
||
|
if (!RCLASS_CONST_TBL(value)) return ID_TABLE_CONTINUE;
|
||
|
else {
|
||
|
struct fc_result arg;
|
||
|
struct fc_result *list;
|
||
|
list = res;
|
||
|
while (list) {
|
||
|
if (list->track == value) return ST_CONTINUE;
|
||
|
if (list->track == value) return ID_TABLE_CONTINUE;
|
||
|
list = list->prev;
|
||
|
}
|
||
| ... | ... | |
|
arg.klass = res->klass;
|
||
|
arg.track = value;
|
||
|
arg.prev = res;
|
||
|
st_foreach(RCLASS_CONST_TBL(value), fc_i, (st_data_t)&arg);
|
||
|
rb_id_table_foreach(RCLASS_CONST_TBL(value), fc_i, &arg);
|
||
|
if (arg.path) {
|
||
|
res->path = arg.path;
|
||
|
return ST_STOP;
|
||
|
return ID_TABLE_STOP;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return ST_CONTINUE;
|
||
|
return ID_TABLE_CONTINUE;
|
||
|
}
|
||
|
/**
|
||
| ... | ... | |
|
arg.track = rb_cObject;
|
||
|
arg.prev = 0;
|
||
|
if (RCLASS_CONST_TBL(rb_cObject)) {
|
||
|
st_foreach_safe(RCLASS_CONST_TBL(rb_cObject), fc_i, (st_data_t)&arg);
|
||
|
rb_id_table_foreach(RCLASS_CONST_TBL(rb_cObject), fc_i, &arg);
|
||
|
}
|
||
|
if (arg.path) {
|
||
|
st_data_t tmp = tmp_classpath;
|
||
| ... | ... | |
|
VALUE value;
|
||
|
};
|
||
|
static void const_tbl_update(struct autoload_const_set_args *);
|
||
|
static VALUE
|
||
|
autoload_const_set(VALUE arg)
|
||
|
{
|
||
| ... | ... | |
|
VALUE klass = args->mod;
|
||
|
ID id = args->id;
|
||
|
check_before_mod_set(klass, id, args->value, "constant");
|
||
|
st_update(RCLASS_CONST_TBL(klass), (st_data_t)id,
|
||
|
const_update, (st_data_t)args);
|
||
|
const_tbl_update(args);
|
||
|
return 0; /* ignored */
|
||
|
}
|
||
| ... | ... | |
|
rb_const_remove(VALUE mod, ID id)
|
||
|
{
|
||
|
VALUE val;
|
||
|
st_data_t v, n = id;
|
||
|
rb_const_entry_t *ce;
|
||
|
rb_check_frozen(mod);
|
||
|
if (!RCLASS_CONST_TBL(mod) || !st_delete(RCLASS_CONST_TBL(mod), &n, &v)) {
|
||
|
ce = rb_const_lookup(mod, id);
|
||
|
if (!ce || !rb_id_table_delete(RCLASS_CONST_TBL(mod), id)) {
|
||
|
if (rb_const_defined_at(mod, id)) {
|
||
|
rb_name_error(id, "cannot remove %"PRIsVALUE"::%"PRIsVALUE"",
|
||
|
rb_class_name(mod), QUOTE_ID(id));
|
||
| ... | ... | |
|
rb_clear_constant_cache();
|
||
|
val = ((rb_const_entry_t*)v)->value;
|
||
|
val = ce->value;
|
||
|
if (val == Qundef) {
|
||
|
autoload_delete(mod, id);
|
||
|
val = Qnil;
|
||
|
}
|
||
|
xfree((rb_const_entry_t*)v);
|
||
|
xfree(ce);
|
||
|
return val;
|
||
|
}
|
||
| ... | ... | |
|
return ST_CONTINUE;
|
||
|
}
|
||
|
static int
|
||
|
sv_i(st_data_t k, st_data_t v, st_data_t a)
|
||
|
static enum rb_id_table_iterator_result
|
||
|
sv_i(ID key, VALUE v, void *a)
|
||
|
{
|
||
|
ID key = (ID)k;
|
||
|
rb_const_entry_t *ce = (rb_const_entry_t *)v;
|
||
|
st_table *tbl = (st_table *)a;
|
||
|
st_table *tbl = a;
|
||
|
if (rb_is_const_id(key)) {
|
||
|
st_update(tbl, (st_data_t)key, cv_i_update, (st_data_t)ce);
|
||
|
}
|
||
|
return ST_CONTINUE;
|
||
|
return ID_TABLE_CONTINUE;
|
||
|
}
|
||
|
static int
|
||
|
rb_local_constants_i(st_data_t const_name, st_data_t const_value, st_data_t ary)
|
||
|
static enum rb_id_table_iterator_result
|
||
|
rb_local_constants_i(ID const_name, VALUE const_value, void *ary)
|
||
|
{
|
||
|
rb_ary_push((VALUE)ary, ID2SYM((ID)const_name));
|
||
|
return ST_CONTINUE;
|
||
|
rb_ary_push((VALUE)ary, ID2SYM(const_name));
|
||
|
return ID_TABLE_CONTINUE;
|
||
|
}
|
||
|
static VALUE
|
||
|
rb_local_constants(VALUE mod)
|
||
|
{
|
||
|
st_table *tbl = RCLASS_CONST_TBL(mod);
|
||
|
struct rb_id_table *tbl = RCLASS_CONST_TBL(mod);
|
||
|
VALUE ary;
|
||
|
if (!tbl) return rb_ary_new2(0);
|
||
|
ary = rb_ary_new2(tbl->num_entries);
|
||
|
st_foreach(tbl, rb_local_constants_i, ary);
|
||
|
ary = rb_ary_new2(rb_id_table_size(tbl));
|
||
|
rb_id_table_foreach(tbl, rb_local_constants_i, (void *)ary);
|
||
|
return ary;
|
||
|
}
|
||
| ... | ... | |
|
tbl = st_init_numtable();
|
||
|
}
|
||
|
if (RCLASS_CONST_TBL(mod)) {
|
||
|
st_foreach_safe(RCLASS_CONST_TBL(mod), sv_i, (st_data_t)tbl);
|
||
|
rb_id_table_foreach(RCLASS_CONST_TBL(mod), sv_i, tbl);
|
||
|
}
|
||
|
return tbl;
|
||
|
}
|
||
| ... | ... | |
|
rb_const_set(VALUE klass, ID id, VALUE val)
|
||
|
{
|
||
|
rb_const_entry_t *ce;
|
||
|
st_table *tbl = RCLASS_CONST_TBL(klass);
|
||
|
struct rb_id_table *tbl = RCLASS_CONST_TBL(klass);
|
||
|
if (NIL_P(klass)) {
|
||
|
rb_raise(rb_eTypeError, "no class/module to define constant %"PRIsVALUE"",
|
||
| ... | ... | |
|
check_before_mod_set(klass, id, val, "constant");
|
||
|
if (!tbl) {
|
||
|
RCLASS_CONST_TBL(klass) = tbl = st_init_numtable();
|
||
|
RCLASS_CONST_TBL(klass) = tbl = rb_id_table_create(0);
|
||
|
rb_clear_constant_cache();
|
||
|
ce = ZALLOC(rb_const_entry_t);
|
||
|
st_insert(tbl, (st_data_t)id, (st_data_t)ce);
|
||
|
rb_id_table_insert(tbl, id, (VALUE)ce);
|
||
|
setup_const_entry(ce, klass, val, CONST_PUBLIC);
|
||
|
}
|
||
|
else {
|
||
| ... | ... | |
|
args.mod = klass;
|
||
|
args.id = id;
|
||
|
args.value = val;
|
||
|
st_update(tbl, (st_data_t)id, const_update, (st_data_t)&args);
|
||
|
const_tbl_update(&args);
|
||
|
}
|
||
|
}
|
||
|
static int
|
||
|
const_update(st_data_t *key, st_data_t *value, st_data_t arg, int existing)
|
||
|
static void
|
||
|
const_tbl_update(struct autoload_const_set_args *args)
|
||
|
{
|
||
|
struct autoload_const_set_args *args = (struct autoload_const_set_args *)arg;
|
||
|
VALUE value;
|
||
|
VALUE klass = args->mod;
|
||
|
VALUE val = args->value;
|
||
|
ID id = args->id;
|
||
|
struct rb_id_table *tbl = RCLASS_CONST_TBL(klass);
|
||
|
rb_const_flag_t visibility = CONST_PUBLIC;
|
||
|
rb_const_entry_t *ce;
|
||
|
if (existing) {
|
||
|
ce = (rb_const_entry_t *)*value;
|
||
|
if (ce) {
|
||
|
if (ce->value == Qundef) {
|
||
|
VALUE load;
|
||
|
struct autoload_data_i *ele;
|
||
|
load = autoload_data(klass, id);
|
||
|
/* for autoloading thread, keep the defined value to autoloading storage */
|
||
|
if (load && (ele = check_autoload_data(load)) &&
|
||
|
(ele->thread == rb_thread_current())) {
|
||
|
rb_clear_constant_cache();
|
||
|
ele->value = val; /* autoload_i is non-WB-protected */
|
||
|
return ST_STOP;
|
||
|
}
|
||
|
/* otherwise, allow to override */
|
||
|
autoload_delete(klass, id);
|
||
|
if (rb_id_table_lookup(tbl, id, &value)) {
|
||
|
ce = (rb_const_entry_t *)value;
|
||
|
if (ce->value == Qundef) {
|
||
|
VALUE load;
|
||
|
struct autoload_data_i *ele;
|
||
|
load = autoload_data(klass, id);
|
||
|
/* for autoloading thread, keep the defined value to autoloading storage */
|
||
|
if (load && (ele = check_autoload_data(load)) &&
|
||
|
(ele->thread == rb_thread_current())) {
|
||
|
rb_clear_constant_cache();
|
||
|
ele->value = val; /* autoload_i is non-WB-protected */
|
||
|
return;
|
||
|
}
|
||
|
else {
|
||
|
VALUE name = QUOTE_ID(id);
|
||
|
visibility = ce->flag;
|
||
|
if (klass == rb_cObject)
|
||
|
rb_warn("already initialized constant %"PRIsVALUE"", name);
|
||
|
else
|
||
|
rb_warn("already initialized constant %"PRIsVALUE"::%"PRIsVALUE"",
|
||
|
rb_class_name(klass), name);
|
||
|
if (!NIL_P(ce->file) && ce->line) {
|
||
|
rb_compile_warn(RSTRING_PTR(ce->file), ce->line,
|
||
|
"previous definition of %"PRIsVALUE" was here", name);
|
||
|
}
|
||
|
/* otherwise, allow to override */
|
||
|
autoload_delete(klass, id);
|
||
|
}
|
||
|
else {
|
||
|
VALUE name = QUOTE_ID(id);
|
||
|
visibility = ce->flag;
|
||
|
if (klass == rb_cObject)
|
||
|
rb_warn("already initialized constant %"PRIsVALUE"", name);
|
||
|
else
|
||
|
rb_warn("already initialized constant %"PRIsVALUE"::%"PRIsVALUE"",
|
||
|
rb_class_name(klass), name);
|
||
|
if (!NIL_P(ce->file) && ce->line) {
|
||
|
rb_compile_warn(RSTRING_PTR(ce->file), ce->line,
|
||
|
"previous definition of %"PRIsVALUE" was here", name);
|
||
|
}
|
||
|
rb_clear_constant_cache();
|
||
|
setup_const_entry(ce, klass, val, visibility);
|
||
|
return ST_STOP;
|
||
|
}
|
||
|
}
|
||
|
rb_clear_constant_cache();
|
||
|
rb_clear_constant_cache();
|
||
|
setup_const_entry(ce, klass, val, visibility);
|
||
|
} else {
|
||
|
rb_clear_constant_cache();
|
||
|
ce = ZALLOC(rb_const_entry_t);
|
||
|
*value = (st_data_t)ce;
|
||
|
setup_const_entry(ce, klass, val, visibility);
|
||
|
return ST_CONTINUE;
|
||
|
ce = ZALLOC(rb_const_entry_t);
|
||
|
rb_id_table_insert(tbl, id, (VALUE)ce);
|
||
|
setup_const_entry(ce, klass, val, visibility);
|
||
|
}
|
||
|
}
|
||
|
static void
|
||
| ... | ... | |
|
rb_const_entry_t *
|
||
|
rb_const_lookup(VALUE klass, ID id)
|
||
|
{
|
||
|
st_table *tbl = RCLASS_CONST_TBL(klass);
|
||
|
st_data_t val;
|
||
|
struct rb_id_table *tbl = RCLASS_CONST_TBL(klass);
|
||
|
VALUE val;
|
||
|
if (tbl && st_lookup(tbl, (st_data_t)id, &val)) {
|
||
|
if (tbl && rb_id_table_lookup(tbl, id, &val)) {
|
||
|
return (rb_const_entry_t *)val;
|
||
|
}
|
||
|
return 0;
|
||
|
-
|
||