variable.c: resurrect `size` member in `iv_tbl`.

The existence of this member reduces memory and execution time.
parent 786156d4
......@@ -14,7 +14,7 @@
/* Instance variable table structure */
typedef struct iv_tbl {
size_t alloc;
size_t size, alloc;
mrb_value *ptr;
} iv_tbl;
......@@ -47,6 +47,7 @@ iv_rehash(mrb_state *mrb, iv_tbl *t)
khash_power2(new_alloc);
if (old_alloc == new_alloc) return;
t->size = 0;
t->alloc = new_alloc;
t->ptr = (mrb_value*)mrb_calloc(mrb, sizeof(mrb_value)+sizeof(mrb_sym), new_alloc);
if (old_alloc == 0) return;
......@@ -84,6 +85,7 @@ iv_put(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value val)
return;
}
else if (key == IV_EMPTY) {
t->size++;
keys[pos] = sym;
vals[pos] = val;
return;
......@@ -94,6 +96,7 @@ iv_put(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value val)
pos = (pos+1) & (t->alloc-1);
if (pos == start) { /* not found */
if (dpos >= 0) {
t->size++;
keys[dpos] = sym;
vals[dpos] = val;
return;
......@@ -115,6 +118,7 @@ iv_get(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value *vp)
if (t == NULL) return FALSE;
if (t->alloc == 0) return FALSE;
if (t->size == 0) return FALSE;
mrb_sym *keys = (mrb_sym*)&t->ptr[t->alloc];
mrb_value *vals = t->ptr;
......@@ -143,7 +147,8 @@ iv_del(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value *vp)
size_t hash, pos, start;
if (t == NULL) return FALSE;
if (t->alloc == 0) return FALSE;
if (t->alloc == 0) return FALSE;
if (t->size == 0) return FALSE;
mrb_sym *keys = (mrb_sym*)&t->ptr[t->alloc];
mrb_value *vals = t->ptr;
......@@ -153,6 +158,7 @@ iv_del(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value *vp)
mrb_sym key = keys[pos];
if (key == sym) {
if (vp) *vp = vals[pos];
t->size--;
keys[pos] = IV_DELETED;
return TRUE;
}
......@@ -174,6 +180,7 @@ iv_foreach(mrb_state *mrb, iv_tbl *t, mrb_iv_foreach_func *func, void *p)
if (t == NULL) return;
if (t->alloc == 0) return;
if (t->size == 0) return;
mrb_sym *keys = (mrb_sym*)&t->ptr[t->alloc];
mrb_value *vals = t->ptr;
......@@ -193,7 +200,7 @@ static size_t
iv_size(mrb_state *mrb, iv_tbl *t)
{
if (t == NULL) return 0;
return t->alloc;
return t->size;
}
/* Copy the instance variable table. */
......@@ -205,6 +212,7 @@ iv_copy(mrb_state *mrb, iv_tbl *t)
if (t == NULL) return NULL;
if (t->alloc == 0) return NULL;
if (t->size == 0) return NULL;
mrb_sym *keys = (mrb_sym*)&t->ptr[t->alloc];
mrb_value *vals = t->ptr;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment