From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS16276 158.69.0.0/16 X-Spam-Status: No, score=-2.1 required=3.0 tests=AWL,BAYES_00,RCVD_IN_XBL, URIBL_BLOCKED shortcircuit=no autolearn=no version=3.3.2 X-Original-To: spew@80x24.org Received: from 80x24.org (this-is-a-tor-node-1.hostaproxy.com [158.69.192.220]) by dcvr.yhbt.net (Postfix) with ESMTP id E30811F537 for ; Thu, 22 Oct 2015 02:37:53 +0000 (UTC) From: Eric Wong To: spew@80x24.org Subject: [PATCH] use id_table for constant tables Date: Thu, 22 Oct 2015 02:37:52 +0000 Message-Id: <20151022023752.9038-1-e@80x24.org> List-Id: Oops, apparently constant order matters as webrick tests fail path resolution. All tests pass with ID_TABLE_IMPL 1 (st) with id_table, though. 1) Failure: TestWEBrickHTTPAuth#test_basic_auth [/ruby/test/webrick/utils.rb:60]: exceptions on 1 threads: #: /ruby/test/lib/minitest/unit.rb:200:in `assert': Expected /ERROR WEBrick::HTTPStatus::Unauthorized/ to match "[2015-10-22 02:35:17] ERROR WEBrick::HTTPAuth::Authenticator::AuthException\n". (MiniTest::Assertion) from /ruby/test/lib/test/unit/assertions.rb:36:in `assert' from /ruby/test/lib/minitest/unit.rb:292:in `assert_match' from /ruby/test/webrick/test_httpauth.rb:12:in `block in test_basic_auth' from /ruby/test/webrick/utils.rb:49:in `block in start_server' 2) Failure: TestWEBrickHTTPAuth#test_basic_auth2 [/ruby/test/webrick/utils.rb:60]: exceptions on 1 threads: #: /ruby/test/lib/minitest/unit.rb:200:in `assert': webrick log doesn't have expected error: /ERROR WEBrick::HTTPStatus::Unauthorized/ (MiniTest::Assertion) from /ruby/test/lib/test/unit/assertions.rb:36:in `assert' from /ruby/test/webrick/test_httpauth.rb:41:in `block (2 levels) in test_basic_auth2' from /ruby/test/webrick/test_httpauth.rb:40:in `each' from /ruby/test/webrick/test_httpauth.rb:40:in `block in test_basic_auth2' from /ruby/test/webrick/utils.rb:49:in `block in start_server' 3) Failure: TestWEBrickHTTPAuth#test_digest_auth [/ruby/test/webrick/utils.rb:60]: exceptions on 1 threads: #: /ruby/test/lib/minitest/unit.rb:200:in `assert': webrick log doesn't have expected error: /ERROR WEBrick::HTTPStatus::Unauthorized/ (MiniTest::Assertion) from /ruby/test/lib/test/unit/assertions.rb:36:in `assert' from /ruby/test/webrick/test_httpauth.rb:119:in `block (2 levels) in test_digest_auth' from /ruby/test/webrick/test_httpauth.rb:118:in `each' from /ruby/test/webrick/test_httpauth.rb:118:in `block in test_digest_auth' from /ruby/test/webrick/utils.rb:49:in `block in start_server' --- class.c | 33 +++++++------ constant.h | 2 +- gc.c | 26 +++++----- internal.h | 2 +- variable.c | 161 ++++++++++++++++++++++++++++++------------------------------- 5 files changed, 112 insertions(+), 112 deletions(-) diff --git a/class.c b/class.c index a1b3eba..a98a594 100644 --- a/class.c +++ b/class.c @@ -274,7 +274,7 @@ clone_method_i(ID key, VALUE value, void *data) 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_t *ce, struct clone_const_arg *arg) 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) 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(VALUE obj, VALUE attach) } 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); @@ -810,7 +808,7 @@ rb_include_class_new(VALUE module, VALUE super) 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); @@ -867,6 +865,7 @@ include_modules_at(const VALUE klass, VALUE c, VALUE module, int search_super) while (module) { int superclass_seen = FALSE; + struct rb_id_table *tbl; if (RCLASS_ORIGIN(module) != module) goto skip; @@ -904,8 +903,12 @@ include_modules_at(const VALUE klass, VALUE c, VALUE module, int search_super) 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); } diff --git a/constant.h b/constant.h index 23d17ac..76b59fa 100644 --- a/constant.h +++ b/constant.h @@ -38,7 +38,7 @@ typedef struct rb_const_entry_struct { 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); diff --git a/gc.c b/gc.c index 4e9df0c..285d228 100644 --- a/gc.c +++ b/gc.c @@ -1966,19 +1966,19 @@ is_pointer_to_heap(rb_objspace_t *objspace, void *ptr) 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 @@ -3046,7 +3046,7 @@ obj_memsize_of(VALUE obj, int use_all_types) 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); } @@ -4013,22 +4013,22 @@ mark_m_tbl(rb_objspace_t *objspace, struct rb_id_table *tbl) } } -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 diff --git a/internal.h b/internal.h index 6941586..b6c8f3d 100644 --- a/internal.h +++ b/internal.h @@ -463,7 +463,7 @@ typedef unsigned long rb_serial_t; 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; diff --git a/variable.c b/variable.c index 84eaf17..315a467 100644 --- a/variable.c +++ b/variable.c @@ -14,6 +14,7 @@ #include "internal.h" #include "ruby/st.h" #include "ruby/util.h" +#include "id_table.h" #include "constant.h" #include "id.h" @@ -22,7 +23,6 @@ static ID autoload, classpath, tmp_classpath, classid; 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; @@ -90,28 +90,27 @@ fc_path(struct fc_result *fc, ID name) 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; } @@ -121,14 +120,14 @@ fc_i(st_data_t k, st_data_t v, st_data_t a) 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; } /** @@ -150,7 +149,7 @@ find_class_path(VALUE klass, ID preferred) 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; @@ -2053,6 +2052,8 @@ struct autoload_const_set_args { VALUE value; }; +static void const_tbl_update(struct autoload_const_set_args *); + static VALUE autoload_const_set(VALUE arg) { @@ -2060,8 +2061,7 @@ 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 */ } @@ -2262,10 +2262,11 @@ VALUE 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)); @@ -2276,12 +2277,12 @@ rb_const_remove(VALUE mod, 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; } @@ -2293,36 +2294,35 @@ cv_i_update(st_data_t *k, st_data_t *v, st_data_t a, int existing) 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; } @@ -2334,7 +2334,7 @@ rb_mod_const_at(VALUE mod, void *data) 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; } @@ -2486,7 +2486,7 @@ void 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"", @@ -2495,10 +2495,10 @@ rb_const_set(VALUE klass, ID id, VALUE val) 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 { @@ -2506,64 +2506,61 @@ rb_const_set(VALUE klass, ID id, VALUE val) 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 @@ -3003,10 +3000,10 @@ rb_st_copy(VALUE obj, struct st_table *orig_tbl) 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; -- EW