From 7b84fd4ce8d0a8dac62e7fa2e09bd3511d2f62ec Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Sat, 11 Dec 2021 20:52:23 +0900 Subject: variable.c: resurrect `size` member in `iv_tbl`. The existence of this member reduces memory and execution time. --- src/variable.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/variable.c b/src/variable.c index bd598dd37..5c1682082 100644 --- a/src/variable.c +++ b/src/variable.c @@ -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; -- cgit v1.2.3