dumping ground for random patches and texts
 help / color / mirror / Atom feed
* id_table wip
@ 2015-10-20 21:49 Eric Wong
  0 siblings, 0 replies; only message in thread
From: Eric Wong @ 2015-10-20 21:49 UTC (permalink / raw)
  To: spew

diff --git a/class.c b/class.c
index a1b3eba..3f69e9b 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, 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,10 @@ 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();
+	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 +393,10 @@ 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();
+	    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 +810,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 +867,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 +905,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..8f0dd96 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(ID id, 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(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 e4a6520..c633cf1 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..c9fadde 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,18 +90,17 @@ 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;
@@ -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 */
 }
 
@@ -2261,11 +2261,13 @@ rb_mod_remove_const(VALUE mod, VALUE name)
 VALUE
 rb_const_remove(VALUE mod, ID id)
 {
-    VALUE val;
-    st_data_t v, n = id;
+    VALUE val, v;
+    struct rb_id_table *tbl = RCLASS_CONST_TBL(mod);
+    rb_const_entry_t *ce;
 
     rb_check_frozen(mod);
-    if (!RCLASS_CONST_TBL(mod) || !st_delete(RCLASS_CONST_TBL(mod), &n, &v)) {
+    if (!tbl || !rb_id_table_lookup(tbl, id, &v) ||
+		!rb_id_table_delete(tbl, 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 +2278,13 @@ rb_const_remove(VALUE mod, ID id)
 
     rb_clear_constant_cache();
 
-    val = ((rb_const_entry_t*)v)->value;
+    ce = (rb_const_entry_t *)v;
+    val = ce->value;
     if (val == Qundef) {
 	autoload_delete(mod, id);
 	val = Qnil;
     }
-    xfree((rb_const_entry_t*)v);
+    xfree(ce);
     return val;
 }
 
@@ -2293,48 +2296,51 @@ 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 *data)
 {
-    ID key = (ID)k;
     rb_const_entry_t *ce = (rb_const_entry_t *)v;
-    st_table *tbl = (st_table *)a;
+    struct rb_id_table *tbl = data;
 
     if (rb_is_const_id(key)) {
-	st_update(tbl, (st_data_t)key, cv_i_update, (st_data_t)ce);
+	VALUE val;
+
+	if (!rb_id_table_lookup(tbl, key, &val)) {
+	    rb_id_table_insert(tbl, key, (VALUE)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;
 }
 
 void*
 rb_mod_const_at(VALUE mod, void *data)
 {
-    st_table *tbl = data;
+    struct rb_id_table *tbl = data;
     if (!tbl) {
-	tbl = st_init_numtable();
+	tbl = rb_id_table_create(0);
     }
     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 +2492,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 +2501,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,22 +2512,23 @@ 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 (rb_id_table_lookup(tbl, id, &value)) {
+	ce = (rb_const_entry_t *)value;
 	if (ce) {
 	    if (ce->value == Qundef) {
 		VALUE load;
@@ -2533,8 +2540,8 @@ const_update(st_data_t *key, st_data_t *value, st_data_t arg, int existing)
 			    (ele->thread == rb_thread_current())) {
 		    rb_clear_constant_cache();
 
-		    ele->value = val; /* autoload_i is non-WB-protected */
-		    return ST_STOP;
+		    ele->value = value; /* autoload_i is non-WB-protected */
+		    return;
 		}
 		/* otherwise, allow to override */
 		autoload_delete(klass, id);
@@ -2554,16 +2561,15 @@ const_update(st_data_t *key, st_data_t *value, st_data_t arg, int existing)
 	    }
 	    rb_clear_constant_cache();
 	    setup_const_entry(ce, klass, val, visibility);
-	    return ST_STOP;
+	    return;
 	}
     }
 
     rb_clear_constant_cache();
 
     ce = ZALLOC(rb_const_entry_t);
-    *value = (st_data_t)ce;
+    rb_id_table_insert(tbl, id, (VALUE)ce);
     setup_const_entry(ce, klass, val, visibility);
-    return ST_CONTINUE;
 }
 
 static void
@@ -3003,10 +3009,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;

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2015-10-20 21:49 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-10-20 21:49 id_table wip Eric Wong

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).