variable.c: avoid `mrb_undef_value()` for delete entries.

Instead embed deleted flag in the key (`mrb_sym` only occupies 30bits).
parent 4f2a153a
...@@ -18,6 +18,10 @@ typedef struct iv_tbl { ...@@ -18,6 +18,10 @@ typedef struct iv_tbl {
mrb_value *ptr; mrb_value *ptr;
} iv_tbl; } iv_tbl;
#define IV_EMPTY 0
#define IV_DELETED (1<<31)
#define IV_KEY_P(k) (((k)&~((uint32_t)IV_DELETED))!=0)
/* Creates the instance variable table. */ /* Creates the instance variable table. */
static iv_tbl* static iv_tbl*
iv_new(mrb_state *mrb) iv_new(mrb_state *mrb)
...@@ -50,16 +54,13 @@ iv_rehash(mrb_state *mrb, iv_tbl *t) ...@@ -50,16 +54,13 @@ iv_rehash(mrb_state *mrb, iv_tbl *t)
mrb_sym *keys = (mrb_sym*)&old_ptr[old_alloc]; mrb_sym *keys = (mrb_sym*)&old_ptr[old_alloc];
mrb_value *vals = old_ptr; mrb_value *vals = old_ptr;
for (size_t i = 0; i < old_alloc; i++) { for (size_t i = 0; i < old_alloc; i++) {
/* key = 0 means empty; val = undef means deleted */ if (IV_KEY_P(keys[i])) {
if (keys[i] != 0 && !mrb_undef_p(vals[i])) {
iv_put(mrb, t, keys[i], vals[i]); iv_put(mrb, t, keys[i], vals[i]);
} }
} }
mrb_free(mrb, old_ptr); mrb_free(mrb, old_ptr);
} }
#define slot_empty_p(k,v) ((k) == 0 && !mrb_undef_p(v))
/* Set the value for the symbol in the instance variable table. */ /* Set the value for the symbol in the instance variable table. */
static void static void
iv_put(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value val) iv_put(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value val)
...@@ -81,12 +82,12 @@ iv_put(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value val) ...@@ -81,12 +82,12 @@ iv_put(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value val)
vals[pos] = val; vals[pos] = val;
return; return;
} }
else if (slot_empty_p(keys[pos], vals[pos])) { else if (keys[pos] == IV_EMPTY) {
keys[pos] = sym; keys[pos] = sym;
vals[pos] = val; vals[pos] = val;
return; return;
} }
else if (dpos < 0 && mrb_undef_p(vals[pos])) { /* deleted */ else if (keys[pos] == IV_DELETED && dpos < 0) {
dpos = pos; dpos = pos;
} }
pos = (pos+1) & (t->alloc-1); pos = (pos+1) & (t->alloc-1);
...@@ -123,7 +124,7 @@ iv_get(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value *vp) ...@@ -123,7 +124,7 @@ iv_get(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value *vp)
if (vp) *vp = vals[pos]; if (vp) *vp = vals[pos];
return TRUE; return TRUE;
} }
else if (slot_empty_p(keys[pos], vals[pos])) { else if (keys[pos] == IV_EMPTY) {
return FALSE; return FALSE;
} }
pos = (pos+1) & (t->alloc-1); pos = (pos+1) & (t->alloc-1);
...@@ -149,11 +150,10 @@ iv_del(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value *vp) ...@@ -149,11 +150,10 @@ iv_del(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value *vp)
for (;;) { for (;;) {
if (keys[pos] == sym) { if (keys[pos] == sym) {
if (vp) *vp = vals[pos]; if (vp) *vp = vals[pos];
keys[pos] = 0; keys[pos] = IV_DELETED;
vals[pos] = mrb_undef_value();
return TRUE; return TRUE;
} }
else if (slot_empty_p(keys[pos], vals[pos])) { else if (keys[pos] == IV_EMPTY) {
return FALSE; return FALSE;
} }
pos = (pos+1) & (t->alloc-1); pos = (pos+1) & (t->alloc-1);
...@@ -175,7 +175,7 @@ iv_foreach(mrb_state *mrb, iv_tbl *t, mrb_iv_foreach_func *func, void *p) ...@@ -175,7 +175,7 @@ iv_foreach(mrb_state *mrb, iv_tbl *t, mrb_iv_foreach_func *func, void *p)
mrb_sym *keys = (mrb_sym*)&t->ptr[t->alloc]; mrb_sym *keys = (mrb_sym*)&t->ptr[t->alloc];
mrb_value *vals = t->ptr; mrb_value *vals = t->ptr;
for (i=0; i<t->alloc; i++) { for (i=0; i<t->alloc; i++) {
if (keys[i] && !mrb_undef_p(vals[i])) { if (IV_KEY_P(keys[i])) {
if ((*func)(mrb, keys[i], vals[i], p) != 0) { if ((*func)(mrb, keys[i], vals[i], p) != 0) {
return; return;
} }
...@@ -207,7 +207,7 @@ iv_copy(mrb_state *mrb, iv_tbl *t) ...@@ -207,7 +207,7 @@ iv_copy(mrb_state *mrb, iv_tbl *t)
mrb_value *vals = t->ptr; mrb_value *vals = t->ptr;
t2 = iv_new(mrb); t2 = iv_new(mrb);
for (i=0; i<t->alloc; i++) { for (i=0; i<t->alloc; i++) {
if (keys[i] && !mrb_undef_p(vals[i])) { if (IV_KEY_P(keys[i])) {
iv_put(mrb, t2, keys[i], vals[i]); iv_put(mrb, t2, keys[i], vals[i]);
} }
} }
......
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