dumping ground for random patches and texts
 help / color / mirror / Atom feed
* [PATCH] iv_index_tbl: convert to id_table
@ 2015-11-11 20:11 Eric Wong
  0 siblings, 0 replies; only message in thread
From: Eric Wong @ 2015-11-11 20:11 UTC (permalink / raw)
  To: spew

But this breaks because of ID_TABLE_USE_ID_SERIAL...
---
 gc.c            |   4 +-
 internal.h      |   2 +-
 variable.c      | 167 ++++++++++++++++++++++++++++----------------------------
 vm_insnhelper.c |  13 +++--
 4 files changed, 94 insertions(+), 92 deletions(-)

diff --git a/gc.c b/gc.c
index 8518d36..b65a9e4 100644
--- a/gc.c
+++ b/gc.c
@@ -2083,7 +2083,7 @@ obj_free(rb_objspace_t *objspace, VALUE obj)
 	    rb_free_const_table(RCLASS_CONST_TBL(obj));
 	}
 	if (RCLASS_IV_INDEX_TBL(obj)) {
-	    st_free_table(RCLASS_IV_INDEX_TBL(obj));
+	    rb_id_table_free(RCLASS_IV_INDEX_TBL(obj));
 	}
 	if (RCLASS_EXT(obj)->subclasses) {
 	    if (BUILTIN_TYPE(obj) == T_MODULE) {
@@ -3075,7 +3075,7 @@ obj_memsize_of(VALUE obj, int use_all_types)
 		size += st_memsize(RCLASS_IV_TBL(obj));
 	    }
 	    if (RCLASS_IV_INDEX_TBL(obj)) {
-		size += st_memsize(RCLASS_IV_INDEX_TBL(obj));
+		size += rb_id_table_memsize(RCLASS_IV_INDEX_TBL(obj));
 	    }
 	    if (RCLASS(obj)->ptr->iv_tbl) {
 		size += st_memsize(RCLASS(obj)->ptr->iv_tbl);
diff --git a/internal.h b/internal.h
index d135035..5257322 100644
--- a/internal.h
+++ b/internal.h
@@ -461,7 +461,7 @@ typedef unsigned long rb_serial_t;
 #endif
 
 struct rb_classext_struct {
-    struct st_table *iv_index_tbl;
+    struct rb_id_table *iv_index_tbl;
     struct st_table *iv_tbl;
     struct st_table *const_tbl;
     struct rb_id_table *callable_m_tbl;
diff --git a/variable.c b/variable.c
index 000c48d..4cc15cb 100644
--- a/variable.c
+++ b/variable.c
@@ -36,10 +36,10 @@ struct gen_ivtbl {
 
 struct ivar_update {
     union {
-	st_table *iv_index_tbl;
+	struct rb_id_table *iv_index_tbl;
 	struct gen_ivtbl *ivtbl;
     } u;
-    st_data_t index;
+    VALUE index;
     int extended;
 };
 
@@ -954,10 +954,10 @@ struct gen_ivar_compat_tbl {
     st_table *tbl;
 };
 
-static int
-gen_ivar_compat_tbl_i(st_data_t id, st_data_t index, st_data_t arg)
+static enum rb_id_table_iterator_result
+gen_ivar_compat_tbl_i(ID id, VALUE index, void *arg)
 {
-    struct gen_ivar_compat_tbl *a = (struct gen_ivar_compat_tbl *)arg;
+    struct gen_ivar_compat_tbl *a = arg;
 
     if ((long)index < a->ivtbl->numiv) {
 	VALUE val = a->ivtbl->ivptr[index];
@@ -965,7 +965,7 @@ gen_ivar_compat_tbl_i(st_data_t id, st_data_t index, st_data_t arg)
 	    st_add_direct(a->tbl, id, (st_data_t)val);
 	}
     }
-    return ST_CONTINUE;
+    return ID_TABLE_CONTINUE;
 }
 
 static int
@@ -984,7 +984,7 @@ gen_ivtbl_get(VALUE obj, struct gen_ivtbl **ivtbl)
 st_table*
 rb_generic_ivar_table(VALUE obj)
 {
-    st_table *iv_index_tbl = RCLASS_IV_INDEX_TBL(rb_obj_class(obj));
+    struct rb_id_table *iv_index_tbl = RCLASS_IV_INDEX_TBL(rb_obj_class(obj));
     struct gen_ivar_compat_tbl a;
     st_data_t d;
 
@@ -1007,7 +1007,7 @@ rb_generic_ivar_table(VALUE obj)
 	d = (st_data_t)a.tbl;
 	st_add_direct(generic_iv_tbl_compat, (st_data_t)obj, d);
     }
-    st_foreach_safe(iv_index_tbl, gen_ivar_compat_tbl_i, (st_data_t)&a);
+    rb_id_table_foreach(iv_index_tbl, gen_ivar_compat_tbl_i, &a);
 
     return a.tbl;
 }
@@ -1018,10 +1018,10 @@ generic_ivar_delete(VALUE obj, ID id, VALUE undef)
     struct gen_ivtbl *ivtbl;
 
     if (gen_ivtbl_get(obj, &ivtbl)) {
-	st_table *iv_index_tbl = RCLASS_IV_INDEX_TBL(rb_obj_class(obj));
-	st_data_t index;
+	struct rb_id_table *tbl = RCLASS_IV_INDEX_TBL(rb_obj_class(obj));
+	VALUE index;
 
-	if (st_lookup(iv_index_tbl, (st_data_t)id, &index)) {
+	if (rb_id_table_lookup(tbl, id, &index)) {
 	    if ((long)index < ivtbl->numiv) {
 		VALUE ret = ivtbl->ivptr[index];
 
@@ -1039,10 +1039,10 @@ generic_ivar_get(VALUE obj, ID id, VALUE undef)
     struct gen_ivtbl *ivtbl;
 
     if (gen_ivtbl_get(obj, &ivtbl)) {
-	st_table *iv_index_tbl = RCLASS_IV_INDEX_TBL(rb_obj_class(obj));
-	st_data_t index;
+	struct rb_id_table *tbl = RCLASS_IV_INDEX_TBL(rb_obj_class(obj));
+	VALUE index;
 
-	if (st_lookup(iv_index_tbl, (st_data_t)id, &index)) {
+	if (rb_id_table_lookup(tbl, id, &index)) {
 	    if ((long)index < ivtbl->numiv) {
 		VALUE ret = ivtbl->ivptr[index];
 
@@ -1088,10 +1088,11 @@ static long
 iv_index_tbl_newsize(struct ivar_update *ivup)
 {
     long newsize = (ivup->index+1) + (ivup->index+1)/4; /* (index+1)*1.25 */
+    size_t n;
 
     if (!ivup->extended &&
-        ivup->u.iv_index_tbl->num_entries < (st_index_t)newsize) {
-        newsize = ivup->u.iv_index_tbl->num_entries;
+        (n = rb_id_table_size(ivup->u.iv_index_tbl)) < (size_t)newsize) {
+        newsize = (long)n;
     }
     return newsize;
 }
@@ -1128,11 +1129,11 @@ static VALUE
 generic_ivar_defined(VALUE obj, ID id)
 {
     struct gen_ivtbl *ivtbl;
-    st_table *iv_index_tbl = RCLASS_IV_INDEX_TBL(rb_obj_class(obj));
-    st_data_t index;
+    struct rb_id_table *tbl = RCLASS_IV_INDEX_TBL(rb_obj_class(obj));
+    VALUE index;
 
-    if (!iv_index_tbl) return Qfalse;
-    if (!st_lookup(iv_index_tbl, (st_data_t)id, &index)) return Qfalse;
+    if (!tbl) return Qfalse;
+    if (!rb_id_table_lookup(tbl, id, &index)) return Qfalse;
     if (!gen_ivtbl_get(obj, &ivtbl)) return Qfalse;
 
     if (((long)index < ivtbl->numiv) && (ivtbl->ivptr[index] != Qundef))
@@ -1145,12 +1146,11 @@ static int
 generic_ivar_remove(VALUE obj, ID id, VALUE *valp)
 {
     struct gen_ivtbl *ivtbl;
-    st_data_t key = (st_data_t)id;
-    st_data_t index;
-    st_table *iv_index_tbl = RCLASS_IV_INDEX_TBL(rb_obj_class(obj));
+    VALUE index;
+    struct rb_id_table *tbl = RCLASS_IV_INDEX_TBL(rb_obj_class(obj));
 
-    if (!iv_index_tbl) return 0;
-    if (!st_lookup(iv_index_tbl, key, &index)) return 0;
+    if (!tbl) return 0;
+    if (!rb_id_table_lookup(tbl, id, &index)) return 0;
     if (!gen_ivtbl_get(obj, &ivtbl)) return 0;
 
     if ((long)index < ivtbl->numiv) {
@@ -1229,18 +1229,18 @@ VALUE
 rb_ivar_lookup(VALUE obj, ID id, VALUE undef)
 {
     VALUE val, *ptr;
-    struct st_table *iv_index_tbl;
+    struct rb_id_table *tbl;
     long len;
-    st_data_t index;
+    VALUE index;
 
     if (SPECIAL_CONST_P(obj)) return undef;
     switch (BUILTIN_TYPE(obj)) {
       case T_OBJECT:
         len = ROBJECT_NUMIV(obj);
         ptr = ROBJECT_IVPTR(obj);
-        iv_index_tbl = ROBJECT_IV_INDEX_TBL(obj);
-        if (!iv_index_tbl) break;
-        if (!st_lookup(iv_index_tbl, (st_data_t)id, &index)) break;
+        tbl = ROBJECT_IV_INDEX_TBL(obj);
+        if (!tbl) break;
+        if (!rb_id_table_lookup(tbl, id, &index)) break;
         if (len <= (long)index) break;
         val = ptr[index];
         if (val != Qundef)
@@ -1283,18 +1283,18 @@ static VALUE
 rb_ivar_delete(VALUE obj, ID id, VALUE undef)
 {
     VALUE val, *ptr;
-    struct st_table *iv_index_tbl;
+    struct rb_id_table *tbl;
     long len;
-    st_data_t index;
+    VALUE index;
 
     rb_check_frozen(obj);
     switch (BUILTIN_TYPE(obj)) {
       case T_OBJECT:
         len = ROBJECT_NUMIV(obj);
         ptr = ROBJECT_IVPTR(obj);
-        iv_index_tbl = ROBJECT_IV_INDEX_TBL(obj);
-        if (!iv_index_tbl) break;
-        if (!st_lookup(iv_index_tbl, (st_data_t)id, &index)) break;
+        tbl = ROBJECT_IV_INDEX_TBL(obj);
+        if (!tbl) break;
+        if (!rb_id_table_lookup(tbl, id, &index)) break;
         if (len <= (long)index) break;
         val = ptr[index];
         ptr[index] = Qundef;
@@ -1321,30 +1321,30 @@ rb_attr_delete(VALUE obj, ID id)
     return rb_ivar_delete(obj, id, Qnil);
 }
 
-static st_table *
+static struct rb_id_table *
 iv_index_tbl_make(VALUE obj)
 {
     VALUE klass = rb_obj_class(obj);
-    st_table *iv_index_tbl = RCLASS_IV_INDEX_TBL(klass);
+    struct rb_id_table *tbl = RCLASS_IV_INDEX_TBL(klass);
 
-    if (!iv_index_tbl) {
-	iv_index_tbl = RCLASS_IV_INDEX_TBL(klass) = st_init_numtable();
+    if (!tbl) {
+	tbl = RCLASS_IV_INDEX_TBL(klass) = rb_id_table_create(0);
     }
 
-    return iv_index_tbl;
+    return tbl;
 }
 
 static void
 iv_index_tbl_extend(struct ivar_update *ivup, ID id)
 {
-    if (st_lookup(ivup->u.iv_index_tbl, (st_data_t)id, &ivup->index)) {
+    if (rb_id_table_lookup(ivup->u.iv_index_tbl, id, &ivup->index)) {
 	return;
     }
-    if (ivup->u.iv_index_tbl->num_entries >= INT_MAX) {
+    if (rb_id_table_size(ivup->u.iv_index_tbl) >= INT_MAX) {
 	rb_raise(rb_eArgError, "too many instance variables");
     }
-    ivup->index = (st_data_t)ivup->u.iv_index_tbl->num_entries;
-    st_add_direct(ivup->u.iv_index_tbl, (st_data_t)id, ivup->index);
+    ivup->index = (VALUE)rb_id_table_size(ivup->u.iv_index_tbl);
+    rb_id_table_insert(ivup->u.iv_index_tbl, id, ivup->index);
     ivup->extended = 1;
 }
 
@@ -1425,15 +1425,15 @@ VALUE
 rb_ivar_defined(VALUE obj, ID id)
 {
     VALUE val;
-    struct st_table *iv_index_tbl;
-    st_data_t index;
+    struct rb_id_table *tbl;
+    VALUE index;
 
     if (SPECIAL_CONST_P(obj)) return Qfalse;
     switch (BUILTIN_TYPE(obj)) {
       case T_OBJECT:
-        iv_index_tbl = ROBJECT_IV_INDEX_TBL(obj);
-        if (!iv_index_tbl) break;
-        if (!st_lookup(iv_index_tbl, (st_data_t)id, &index)) break;
+        tbl = ROBJECT_IV_INDEX_TBL(obj);
+        if (!tbl) break;
+        if (!rb_id_table_lookup(tbl, id, &index)) break;
         if (ROBJECT_NUMIV(obj) <= (long)index) break;
         val = ROBJECT_IVPTR(obj)[index];
         if (val != Qundef)
@@ -1458,26 +1458,26 @@ struct obj_ivar_tag {
     st_data_t arg;
 };
 
-static int
-obj_ivar_i(st_data_t key, st_data_t index, st_data_t arg)
+static enum rb_id_table_iterator_result
+obj_ivar_i(ID key, VALUE index, void *arg)
 {
-    struct obj_ivar_tag *data = (struct obj_ivar_tag *)arg;
+    struct obj_ivar_tag *data = arg;
+
     if ((long)index < ROBJECT_NUMIV(data->obj)) {
         VALUE val = ROBJECT_IVPTR(data->obj)[(long)index];
         if (val != Qundef) {
-            return (data->func)((ID)key, val, data->arg);
+            return (data->func)(key, val, data->arg);
         }
     }
-    return ST_CONTINUE;
+    return ID_TABLE_CONTINUE;
 }
 
 static void
 obj_ivar_each(VALUE obj, int (*func)(ANYARGS), st_data_t arg)
 {
-    st_table *tbl;
+    struct rb_id_table *tbl = ROBJECT_IV_INDEX_TBL(obj);
     struct obj_ivar_tag data;
 
-    tbl = ROBJECT_IV_INDEX_TBL(obj);
     if (!tbl)
         return;
 
@@ -1485,54 +1485,54 @@ obj_ivar_each(VALUE obj, int (*func)(ANYARGS), st_data_t arg)
     data.func = (int (*)(ID key, VALUE val, st_data_t arg))func;
     data.arg = arg;
 
-    st_foreach_safe(tbl, obj_ivar_i, (st_data_t)&data);
+    rb_id_table_foreach(tbl, obj_ivar_i, &data);
 }
 
 struct gen_ivar_tag {
     struct gen_ivtbl *ivtbl;
-    int (*func)(ID key, VALUE val, st_data_t arg);
-    st_data_t arg;
+    rb_id_table_foreach_func_t *func;
+    void *arg;
 };
 
-static int
-gen_ivar_each_i(st_data_t key, st_data_t index, st_data_t data)
+static enum rb_id_table_iterator_result
+gen_ivar_each_i(ID key, VALUE index, void *data)
 {
-    struct gen_ivar_tag *arg = (struct gen_ivar_tag *)data;
+    struct gen_ivar_tag *arg = data;
 
     if ((long)index < arg->ivtbl->numiv) {
         VALUE val = arg->ivtbl->ivptr[index];
         if (val != Qundef) {
-            return (arg->func)((ID)key, val, arg->arg);
+            return (arg->func)(key, val, arg->arg);
         }
     }
-    return ST_CONTINUE;
+    return ID_TABLE_CONTINUE;
 }
 
 static void
-gen_ivar_each(VALUE obj, int (*func)(ANYARGS), st_data_t arg)
+gen_ivar_each(VALUE obj, rb_id_table_foreach_func_t *func, void *arg)
 {
     struct gen_ivar_tag data;
-    st_table *iv_index_tbl = RCLASS_IV_INDEX_TBL(rb_obj_class(obj));
+    struct rb_id_table *tbl = RCLASS_IV_INDEX_TBL(rb_obj_class(obj));
 
-    if (!iv_index_tbl) return;
+    if (!tbl) return;
     if (!gen_ivtbl_get(obj, &data.ivtbl)) return;
 
-    data.func = (int (*)(ID key, VALUE val, st_data_t arg))func;
+    data.func = func;
     data.arg = arg;
 
-    st_foreach_safe(iv_index_tbl, gen_ivar_each_i, (st_data_t)&data);
+    rb_id_table_foreach(tbl, gen_ivar_each_i, &data);
 }
 
 struct givar_copy {
     VALUE obj;
-    st_table *iv_index_tbl;
+    struct rb_id_table *iv_index_tbl;
     struct gen_ivtbl *ivtbl;
 };
 
-static int
-gen_ivar_copy(ID id, VALUE val, st_data_t arg)
+static enum rb_id_table_iterator_result
+gen_ivar_copy(ID id, VALUE val, void *arg)
 {
-    struct givar_copy *c = (struct givar_copy *)arg;
+    struct givar_copy *c = arg;
     struct ivar_update ivup;
 
     ivup.extended = 0;
@@ -1547,7 +1547,7 @@ gen_ivar_copy(ID id, VALUE val, st_data_t arg)
 
     RB_OBJ_WRITTEN(c->obj, Qundef, val);
 
-    return ST_CONTINUE;
+    return ID_TABLE_CONTINUE;
 }
 
 void
@@ -1583,7 +1583,7 @@ rb_copy_generic_ivar(VALUE clone, VALUE obj)
 
 	c.iv_index_tbl = iv_index_tbl_make(clone);
 	c.obj = clone;
-	gen_ivar_each(obj, gen_ivar_copy, (st_data_t)&c);
+	gen_ivar_each(obj, gen_ivar_copy, &c);
 	/*
 	 * c.ivtbl may change in gen_ivar_copy due to realloc,
 	 * no need to free
@@ -1608,7 +1608,7 @@ rb_ivar_foreach(VALUE obj, int (*func)(ANYARGS), st_data_t arg)
 	break;
       default:
 	if (FL_TEST(obj, FL_EXIVAR)) {
-	    gen_ivar_each(obj, func, arg);
+	    gen_ivar_each(obj, (rb_id_table_foreach_func_t *)func, (void *)arg);
 	}
 	break;
     }
@@ -1617,14 +1617,15 @@ rb_ivar_foreach(VALUE obj, int (*func)(ANYARGS), st_data_t arg)
 st_index_t
 rb_ivar_count(VALUE obj)
 {
+    struct rb_id_table *itbl;
     st_table *tbl;
 
     if (SPECIAL_CONST_P(obj)) return 0;
 
     switch (BUILTIN_TYPE(obj)) {
       case T_OBJECT:
-	if ((tbl = ROBJECT_IV_INDEX_TBL(obj)) != 0) {
-	    st_index_t i, count, num = tbl->num_entries;
+	if ((itbl = ROBJECT_IV_INDEX_TBL(obj)) != 0) {
+	    size_t i, count, num = rb_id_table_size(itbl);
 	    const VALUE *const ivptr = ROBJECT_IVPTR(obj);
 	    for (i = count = 0; i < num; ++i) {
 		if (ivptr[i] != Qundef) {
@@ -1741,8 +1742,8 @@ rb_obj_remove_instance_variable(VALUE obj, VALUE name)
     VALUE val = Qnil;
     const ID id = id_for_var(obj, name, an, instance);
     st_data_t n, v;
-    struct st_table *iv_index_tbl;
-    st_data_t index;
+    struct rb_id_table *tbl;
+    VALUE index;
 
     rb_check_frozen(obj);
     if (!id) {
@@ -1751,9 +1752,9 @@ rb_obj_remove_instance_variable(VALUE obj, VALUE name)
 
     switch (BUILTIN_TYPE(obj)) {
       case T_OBJECT:
-        iv_index_tbl = ROBJECT_IV_INDEX_TBL(obj);
-        if (!iv_index_tbl) break;
-        if (!st_lookup(iv_index_tbl, (st_data_t)id, &index)) break;
+        tbl = ROBJECT_IV_INDEX_TBL(obj);
+        if (!tbl) break;
+        if (!rb_id_table_lookup(tbl, id, &index)) break;
         if (ROBJECT_NUMIV(obj) <= (long)index) break;
         val = ROBJECT_IVPTR(obj)[index];
         if (val != Qundef) {
diff --git a/vm_insnhelper.c b/vm_insnhelper.c
index 4130c17..d919de3 100644
--- a/vm_insnhelper.c
+++ b/vm_insnhelper.c
@@ -15,6 +15,7 @@
 #include "internal.h"
 #include "probes.h"
 #include "probes_helper.h"
+#include "id_table.h"
 
 /* control stack frame */
 
@@ -754,11 +755,11 @@ vm_getivar(VALUE obj, ID id, IC ic, struct rb_call_cache *cc, int is_attr)
 	    }
 	}
 	else {
-	    st_data_t index;
-	    struct st_table *iv_index_tbl = ROBJECT_IV_INDEX_TBL(obj);
+	    VALUE index;
+	    struct rb_id_table *tbl = ROBJECT_IV_INDEX_TBL(obj);
 
-	    if (iv_index_tbl) {
-		if (st_lookup(iv_index_tbl, id, &index)) {
+	    if (tbl) {
+		if (rb_id_table_lookup(tbl, id, &index)) {
 		    if ((long)index < len) {
 			val = ptr[index];
 		    }
@@ -809,9 +810,9 @@ vm_setivar(VALUE obj, ID id, VALUE val, IC ic, struct rb_call_cache *cc, int is_
 	    }
 	}
 	else {
-	    struct st_table *iv_index_tbl = ROBJECT_IV_INDEX_TBL(obj);
+	    struct rb_id_table *tbl = ROBJECT_IV_INDEX_TBL(obj);
 
-	    if (iv_index_tbl && st_lookup(iv_index_tbl, (st_data_t)id, &index)) {
+	    if (tbl && rb_id_table_lookup(tbl, id, (VALUE *)&index)) {
 		if (!is_attr) {
 		    ic->ic_value.index = index;
 		    ic->ic_serial = RCLASS_SERIAL(klass);
-- 
EW


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

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

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-11-11 20:11 [PATCH] iv_index_tbl: convert to id_table 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).