Use hash table instead of segment list for instance variables.

parent 59b35250
...@@ -11,21 +11,16 @@ ...@@ -11,21 +11,16 @@
#include <mruby/string.h> #include <mruby/string.h>
#include <mruby/variable.h> #include <mruby/variable.h>
#ifndef MRB_IV_SEGMENT_SIZE struct iv_elem {
#define MRB_IV_SEGMENT_SIZE 4 mrb_sym key;
#endif mrb_value val;
};
typedef struct segment {
mrb_sym key[MRB_IV_SEGMENT_SIZE];
mrb_value val[MRB_IV_SEGMENT_SIZE];
struct segment *next;
} segment;
/* Instance variable table structure */ /* Instance variable table structure */
typedef struct iv_tbl { typedef struct iv_tbl {
segment *rootseg;
size_t size; size_t size;
size_t last_len; size_t alloc;
struct iv_elem *table;
} iv_tbl; } iv_tbl;
/* Creates the instance variable table. */ /* Creates the instance variable table. */
...@@ -36,67 +31,82 @@ iv_new(mrb_state *mrb) ...@@ -36,67 +31,82 @@ iv_new(mrb_state *mrb)
t = (iv_tbl*)mrb_malloc(mrb, sizeof(iv_tbl)); t = (iv_tbl*)mrb_malloc(mrb, sizeof(iv_tbl));
t->size = 0; t->size = 0;
t->rootseg = NULL; t->alloc = 0;
t->last_len = 0; t->table = NULL;
return t; return t;
} }
static void iv_put(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value val);
static void
iv_rehash(mrb_state *mrb, iv_tbl *t)
{
size_t old_alloc = t->alloc;
size_t new_alloc = old_alloc+1;
struct iv_elem *old_table = t->table;
khash_power2(new_alloc);
if (old_alloc == new_alloc) return;
t->alloc = new_alloc;
t->size = 0;
t->table = (struct iv_elem*)mrb_calloc(mrb, sizeof(struct iv_elem), new_alloc);
for (size_t i = 0; i < old_alloc; i++) {
struct iv_elem *slot = &old_table[i];
/* key = 0 means empty; val = undef means deleted */
if (slot->key != 0 && !mrb_undef_p(slot->val)) {
iv_put(mrb, t, slot->key, slot->val);
}
}
mrb_free(mrb, old_table);
}
#define slot_empty_p(slot) ((slot)->key == 0 && !mrb_undef_p((slot)->val))
/* 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)
{ {
segment *seg; size_t hash, pos, start;
segment *prev = NULL; struct iv_elem *dslot = NULL;
segment *matched_seg = NULL;
size_t matched_idx = 0;
size_t i;
if (t == NULL) return; if (t == NULL) return;
seg = t->rootseg; if (t->alloc == 0) {
while (seg) { iv_rehash(mrb, t);
for (i=0; i<MRB_IV_SEGMENT_SIZE; i++) { }
mrb_sym key = seg->key[i]; hash = kh_int_hash_func(mrb, sym);
/* Found room in last segment after last_len */ start = pos = hash & (t->alloc-1);
if (!seg->next && i >= t->last_len) { for (;;) {
seg->key[i] = sym; struct iv_elem *slot = &t->table[pos];
seg->val[i] = val;
t->last_len = i+1; if (slot->key == sym) {
slot->val = val;
return;
}
else if (slot_empty_p(slot)) {
t->size++;
slot->key = sym;
slot->val = val;
return;
}
else if (!dslot && mrb_undef_p(slot->val)) { /* deleted */
dslot = slot;
}
pos = (pos+1) & (t->alloc-1);
if (pos == start) { /* not found */
if (dslot) {
t->size++; t->size++;
dslot->key = sym;
dslot->val = val;
return; return;
} }
if (!matched_seg && key == 0) { /* no room */
matched_seg = seg; iv_rehash(mrb, t);
matched_idx = i; start = pos = hash & (t->alloc-1);
}
else if (key == sym) {
seg->val[i] = val;
return;
}
} }
prev = seg;
seg = seg->next;
}
/* Not found */
if (matched_seg) {
matched_seg->key[matched_idx] = sym;
matched_seg->val[matched_idx] = val;
t->size++;
return;
}
seg = (segment*)mrb_malloc(mrb, sizeof(segment));
seg->next = NULL;
seg->key[0] = sym;
seg->val[0] = val;
t->last_len = 1;
t->size++;
if (prev) {
prev->next = seg;
}
else {
t->rootseg = seg;
} }
} }
...@@ -104,80 +114,81 @@ iv_put(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value val) ...@@ -104,80 +114,81 @@ iv_put(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value val)
static mrb_bool static mrb_bool
iv_get(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value *vp) iv_get(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value *vp)
{ {
segment *seg; size_t hash, pos, start;
size_t i;
if (t == NULL) return FALSE; if (t == NULL) return FALSE;
seg = t->rootseg; if (t->alloc == 0) return FALSE;
while (seg) { if (t->size == 0) return FALSE;
for (i=0; i<MRB_IV_SEGMENT_SIZE; i++) {
mrb_sym key = seg->key[i];
if (!seg->next && i >= t->last_len) { hash = kh_int_hash_func(mrb, sym);
return FALSE; start = pos = hash & (t->alloc-1);
} for (;;) {
if (key == sym) { struct iv_elem *slot = &t->table[pos];
if (vp) *vp = seg->val[i];
return TRUE; if (slot->key == sym) {
} if (vp) *vp = slot->val;
return TRUE;
}
else if (slot_empty_p(slot)) {
return FALSE;
}
pos = (pos+1) & (t->alloc-1);
if (pos == start) { /* not found */
return FALSE;
} }
seg = seg->next;
} }
return FALSE;
} }
/* Deletes the value for the symbol from the instance variable table. */ /* Deletes the value for the symbol from the instance variable table. */
static mrb_bool static mrb_bool
iv_del(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value *vp) iv_del(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value *vp)
{ {
segment *seg; size_t hash, pos, start;
size_t i;
if (t == NULL) return FALSE; if (t == NULL) return FALSE;
seg = t->rootseg; if (t->alloc == 0) return FALSE;
while (seg) { if (t->size == 0) return FALSE;
for (i=0; i<MRB_IV_SEGMENT_SIZE; i++) {
mrb_sym key = seg->key[i];
if (!seg->next && i >= t->last_len) { hash = kh_int_hash_func(mrb, sym);
return FALSE; start = pos = hash & (t->alloc-1);
} for (;;) {
if (key == sym) { struct iv_elem *slot = &t->table[pos];
t->size--;
seg->key[i] = 0; if (slot->key == sym) {
if (vp) *vp = seg->val[i]; if (vp) *vp = slot->val;
return TRUE; t->size--;
} slot->key = 0;
slot->val = mrb_undef_value();
return TRUE;
}
else if (slot_empty_p(slot)) {
return FALSE;
}
pos = (pos+1) & (t->alloc-1);
if (pos == start) { /* not found */
return FALSE;
} }
seg = seg->next;
} }
return FALSE;
} }
/* Iterates over the instance variable table. */ /* Iterates over the instance variable table. */
static void static void
iv_foreach(mrb_state *mrb, iv_tbl *t, mrb_iv_foreach_func *func, void *p) iv_foreach(mrb_state *mrb, iv_tbl *t, mrb_iv_foreach_func *func, void *p)
{ {
segment *seg;
size_t i; size_t i;
if (t == NULL) return; if (t == NULL) return;
seg = t->rootseg; if (t->alloc == 0) return;
while (seg) { if (t->size == 0) return;
for (i=0; i<MRB_IV_SEGMENT_SIZE; i++) {
mrb_sym key = seg->key[i]; for (i=0; i<t->alloc; i++) {
struct iv_elem *slot = &t->table[i];
/* no value in last segment after last_len */ if (slot->key && !mrb_undef_p(slot->val)) {
if (!seg->next && i >= t->last_len) { if ((*func)(mrb, slot->key, slot->val, p) != 0) {
return; return;
} }
if (key != 0) {
if ((*func)(mrb, key, seg->val[i], p) != 0) {
return;
}
}
} }
seg = seg->next;
} }
return; return;
} }
...@@ -186,47 +197,28 @@ iv_foreach(mrb_state *mrb, iv_tbl *t, mrb_iv_foreach_func *func, void *p) ...@@ -186,47 +197,28 @@ iv_foreach(mrb_state *mrb, iv_tbl *t, mrb_iv_foreach_func *func, void *p)
static size_t static size_t
iv_size(mrb_state *mrb, iv_tbl *t) iv_size(mrb_state *mrb, iv_tbl *t)
{ {
segment *seg;
size_t size = 0;
if (t == NULL) return 0; if (t == NULL) return 0;
if (t->size > 0) return t->size; return t->size;
seg = t->rootseg;
while (seg) {
if (seg->next == NULL) {
size += t->last_len;
return size;
}
seg = seg->next;
size += MRB_IV_SEGMENT_SIZE;
}
/* empty iv_tbl */
return 0;
} }
/* Copy the instance variable table. */ /* Copy the instance variable table. */
static iv_tbl* static iv_tbl*
iv_copy(mrb_state *mrb, iv_tbl *t) iv_copy(mrb_state *mrb, iv_tbl *t)
{ {
segment *seg;
iv_tbl *t2; iv_tbl *t2;
size_t i; size_t i;
seg = t->rootseg; if (t == NULL) return NULL;
t2 = iv_new(mrb); if (t->alloc == 0) return NULL;
if (t->size == 0) return NULL;
while (seg != NULL) { t2 = iv_new(mrb);
for (i=0; i<MRB_IV_SEGMENT_SIZE; i++) { for (i=0; i<t->alloc; i++) {
mrb_sym key = seg->key[i]; struct iv_elem *slot = &t->table[i];
mrb_value val = seg->val[i];
if ((seg->next == NULL) && (i >= t->last_len)) { if (slot->key && !mrb_undef_p(slot->val)) {
return t2; iv_put(mrb, t2, slot->key, slot->val);
}
iv_put(mrb, t2, key, val);
} }
seg = seg->next;
} }
return t2; return t2;
} }
...@@ -235,14 +227,7 @@ iv_copy(mrb_state *mrb, iv_tbl *t) ...@@ -235,14 +227,7 @@ iv_copy(mrb_state *mrb, iv_tbl *t)
static void static void
iv_free(mrb_state *mrb, iv_tbl *t) iv_free(mrb_state *mrb, iv_tbl *t)
{ {
segment *seg; mrb_free(mrb, t->table);
seg = t->rootseg;
while (seg) {
segment *p = seg;
seg = seg->next;
mrb_free(mrb, p);
}
mrb_free(mrb, t); mrb_free(mrb, t);
} }
...@@ -1137,16 +1122,9 @@ mrb_class_find_path(mrb_state *mrb, struct RClass *c) ...@@ -1137,16 +1122,9 @@ mrb_class_find_path(mrb_state *mrb, struct RClass *c)
size_t size_t
mrb_obj_iv_tbl_memsize(mrb_state* mrb, mrb_value obj) mrb_obj_iv_tbl_memsize(mrb_state* mrb, mrb_value obj)
{ {
size_t nseg = 0; iv_tbl *t = mrb_obj_ptr(obj)->iv;
segment *seg; if (t == NULL) return 0;
return sizeof(iv_tbl) + t->alloc*sizeof(struct iv_elem);
if (mrb_obj_ptr(obj)->iv == NULL) return 0;
seg = mrb_obj_ptr(obj)->iv->rootseg;
while (seg) {
nseg++;
seg = seg->next;
}
return sizeof(iv_tbl) + sizeof(segment)*nseg;
} }
#define identchar(c) (ISALNUM(c) || (c) == '_' || !ISASCII(c)) #define identchar(c) (ISALNUM(c) || (c) == '_' || !ISASCII(c))
......
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