ruby-changes:41304
From: normal <ko1@a...>
Date: Wed, 30 Dec 2015 05:19:23 +0900 (JST)
Subject: [ruby-changes:41304] normal:r53376 (trunk): use id_table for constant tables
normal 2015-12-30 05:19:14 +0900 (Wed, 30 Dec 2015) New Revision: 53376 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=53376 Log: use id_table for constant tables valgrind 3.9.0 on x86-64 reports a minor reduction in memory usage when loading only RubyGems and RDoc by running: ruby -rrdoc -eexit before: HEAP SUMMARY: in use at exit: 2,913,448 bytes in 27,394 blocks total heap usage: 48,362 allocs, 20,968 frees, 9,034,621 bytes alloc after: HEAP SUMMARY: in use at exit: 2,880,056 bytes in 26,712 blocks total heap usage: 47,791 allocs, 21,079 frees, 9,046,507 bytes alloc * class.c (struct clone_const_arg): adjust for id_table (clone_const): ditto (clone_const_i): ditto (rb_mod_init_copy): ditto (rb_singleton_class_clone_and_attach): ditto (rb_include_class_new): ditto (include_modules_at): ditto * constant.h (rb_free_const_table): ditto * gc.c (free_const_entry_i): ditto (rb_free_const_table): ditto (obj_memsize_of): ditto (mark_const_entry_i): ditto (mark_const_tbl): ditto * internal.h (struct rb_classext_struct): ditto * object.c (rb_mod_const_set): resolve class name on assignment * variable.c (const_update): replace with const_tbl_update (const_tbl_update): new function (fc_i): adjust for id_table (find_class_path): ditto (autoload_const_set): st_update => const_tbl_update (rb_const_remove): adjust for id_table (sv_i): ditto (rb_local_constants_i): ditto (rb_local_constants): ditto (rb_mod_const_at): ditto (rb_mod_const_set): ditto (rb_const_lookup): ditto Modified files: trunk/class.c trunk/constant.h trunk/gc.c trunk/internal.h trunk/object.c trunk/variable.c Index: variable.c =================================================================== --- variable.c (revision 53375) +++ variable.c (revision 53376) @@ -14,6 +14,7 @@ https://github.com/ruby/ruby/blob/trunk/variable.c#L14 #include "internal.h" #include "ruby/st.h" #include "ruby/util.h" +#include "id_table.h" #include "constant.h" #include "id.h" #include "ccan/list/list.h" @@ -24,7 +25,6 @@ static ID autoload, classpath, tmp_class https://github.com/ruby/ruby/blob/trunk/variable.c#L25 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; @@ -92,28 +92,27 @@ fc_path(struct fc_result *fc, ID name) https://github.com/ruby/ruby/blob/trunk/variable.c#L92 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; } @@ -123,14 +122,14 @@ fc_i(st_data_t k, st_data_t v, st_data_t https://github.com/ruby/ruby/blob/trunk/variable.c#L122 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; } /** @@ -152,7 +151,7 @@ find_class_path(VALUE klass, ID preferre https://github.com/ruby/ruby/blob/trunk/variable.c#L151 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; @@ -2070,6 +2069,8 @@ struct autoload_const_set_args { https://github.com/ruby/ruby/blob/trunk/variable.c#L2069 VALUE value; }; +static void const_tbl_update(struct autoload_const_set_args *); + static VALUE autoload_const_set(VALUE arg) { @@ -2077,8 +2078,7 @@ autoload_const_set(VALUE arg) https://github.com/ruby/ruby/blob/trunk/variable.c#L2078 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 */ } @@ -2323,10 +2323,11 @@ VALUE https://github.com/ruby/ruby/blob/trunk/variable.c#L2323 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_err_raise("cannot remove %2$s::%1$s", mod, ID2SYM(id)); @@ -2337,12 +2338,12 @@ rb_const_remove(VALUE mod, ID id) https://github.com/ruby/ruby/blob/trunk/variable.c#L2338 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; } @@ -2354,36 +2355,35 @@ cv_i_update(st_data_t *k, st_data_t *v, https://github.com/ruby/ruby/blob/trunk/variable.c#L2355 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; } @@ -2395,7 +2395,7 @@ rb_mod_const_at(VALUE mod, void *data) https://github.com/ruby/ruby/blob/trunk/variable.c#L2395 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; } @@ -2547,7 +2547,7 @@ void https://github.com/ruby/ruby/blob/trunk/variable.c#L2547 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"", @@ -2556,10 +2556,10 @@ rb_const_set(VALUE klass, ID id, VALUE v https://github.com/ruby/ruby/blob/trunk/variable.c#L2556 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 { @@ -2567,64 +2567,61 @@ rb_const_set(VALUE klass, ID id, VALUE v https://github.com/ruby/ruby/blob/trunk/variable.c#L2567 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->state && - (ele->state->thread == rb_thread_current())) { - rb_clear_constant_cache(); + 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->state && + (ele->state->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); + 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 @@ -3053,10 +3050,10 @@ rb_st_copy(VALUE obj, struct st_table *o https://github.com/ruby/ruby/blob/trunk/variable.c#L3050 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; Index: object.c =================================================================== --- object.c (revision 53375) +++ object.c (revision 53376) @@ -2180,6 +2180,14 @@ rb_mod_const_set(VALUE mod, VALUE name, https://github.com/ruby/ruby/blob/trunk/object.c#L2180 ID id = id_for_setter(mod, name, const, wrong_constant_name); if (!id) id = rb_intern_str(name); 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; } Index: gc.c =================================================================== --- gc.c (revision 53375) +++ gc.c (revision 53376) @@ -2003,19 +2003,19 @@ is_pointer_to_heap(rb_objspace_t *objspa https://github.com/ruby/ruby/blob/trunk/gc.c#L2003 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 @@ -3089,7 +3089,7 @@ obj_memsize_of(VALUE obj, int use_all_ty https://github.com/ruby/ruby/blob/trunk/gc.c#L3089 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); } @@ -4056,22 +4056,22 @@ mark_m_tbl(rb_objspace_t *objspace, stru https://github.com/ruby/ruby/blob/trunk/gc.c#L4056 } } -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 Index: class.c =================================================================== --- class.c (revision 53375) +++ class.c (revision 53376) @@ -274,7 +274,7 @@ clone_method_i(ID key, VALUE value, void https://github.com/ruby/ruby/blob/trunk/class.c#L274 struct clone_const_arg { VALUE klass; - st_table *tbl; + struct rb_id_table *tbl; }; static int @@ -285,14 +285,14 @@ clone_const(ID key, const rb_const_entry https://github.com/ruby/ruby/blob/trunk/class.c#L285 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 @@ -346,10 +346,9 @@ rb_mod_init_copy(VALUE clone, VALUE orig https://github.com/ruby/ruby/blob/trunk/class.c#L346 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; @@ -393,10 +392,9 @@ rb_singleton_class_clone_and_attach(VALU https://github.com/ruby/ruby/blob/trunk/class.c#L392 } 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); @@ -823,7 +821,7 @@ rb_include_class_new(VALUE module, VALUE https://github.com/ruby/ruby/blob/trunk/class.c#L821 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); @@ -880,6 +878,7 @@ include_modules_at(const VALUE klass, VA https://github.com/ruby/ruby/blob/trunk/class.c#L878 while (module) { int superclass_seen = FALSE; + struct rb_id_table *tbl; if (RCLASS_ORIGIN(module) != module) goto skip; @@ -917,8 +916,12 @@ include_modules_at(const VALUE klass, VA https://github.com/ruby/ruby/blob/trunk/class.c#L916 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); } Index: internal.h =================================================================== --- internal.h (revision 53375) +++ internal.h (revision 53376) @@ -463,7 +463,7 @@ typedef unsigned long rb_serial_t; https://github.com/ruby/ruby/blob/trunk/internal.h#L463 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; Index: constant.h =================================================================== --- constant.h (revision 53375) +++ constant.h (revision 53376) @@ -38,7 +38,7 @@ typedef struct rb_const_entry_struct { https://github.com/ruby/ruby/blob/trunk/constant.h#L38 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); -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/