Commit 96029f8e
96029f8eda9aMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
perf: hash table with FNV-1a hashing (O(1) lookup)
Replace linear-scan hash implementation with open-addressing hash table using FNV-1a hash function for both StrIntHash and StrStrHash.
- O(1) average get/set/has_key vs O(n) linear scan
- Automatic grow at 50% load factor (power-of-2 capacity)
- Insertion-order preserved via separate 'order' array
- Robin Hood deletion for correctness
- Hash.each and Hash.keys use order array for Ruby-compatible ordering
- Fix: emit sp_str_hash function before either hash type
wordfreq n=10M: 1.11s → 0.34s (3.3x faster, 8.7x vs Ruby). 67 tests pass, bootstrap clean.
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- 96029f8eda9a
- Parent
- a68222f9cf42
spinel_codegen.rbmodifiedView file
@@ -6358,6 +6358,9 @@
63586358
if @needs_str_array == 163596359
emit_str_array_runtime63606360
end6361+
if @needs_str_int_hash == 1 || @needs_str_str_hash == 16362+
emit_str_hash_func6363+
end63616364
if @needs_str_int_hash == 163626365
emit_str_int_hash_runtime63636366
end@@ -6615,30 +6618,36 @@
66156618
emit_raw("")66166619
end66176620
6621+
def emit_str_hash_func6622+
emit_raw("static inline uint64_t sp_str_hash(const char*s){uint64_t h=14695981039346656037ULL;while(*s){h^=(unsigned char)*s++;h*=1099511628211ULL;}return h;}")6623+
end6624+
66186625
def emit_str_int_hash_runtime6619-
emit_raw("typedef struct{const char**keys;mrb_int*vals;mrb_int len;mrb_int cap;}sp_StrIntHash;")6620-
emit_raw("static void sp_StrIntHash_fin(void*p){sp_StrIntHash*h=(sp_StrIntHash*)p;free(h->keys);free(h->vals);}")6621-
emit_raw("static sp_StrIntHash*sp_StrIntHash_new(void){sp_StrIntHash*h=(sp_StrIntHash*)sp_gc_alloc(sizeof(sp_StrIntHash),sp_StrIntHash_fin,NULL);h->cap=16;h->keys=(const char**)malloc(sizeof(const char*)*h->cap);h->vals=(mrb_int*)malloc(sizeof(mrb_int)*h->cap);h->len=0;return h;}")6622-
emit_raw("static mrb_int sp_StrIntHash_get(sp_StrIntHash*h,const char*k){for(mrb_int i=0;i<h->len;i++)if(strcmp(h->keys[i],k)==0)return h->vals[i];return 0;}")6623-
emit_raw("static void sp_StrIntHash_set(sp_StrIntHash*h,const char*k,mrb_int v){for(mrb_int i=0;i<h->len;i++){if(strcmp(h->keys[i],k)==0){h->vals[i]=v;return;}}if(h->len>=h->cap){h->cap*=2;h->keys=(const char**)realloc(h->keys,sizeof(const char*)*h->cap);h->vals=(mrb_int*)realloc(h->vals,sizeof(mrb_int)*h->cap);}h->keys[h->len]=k;h->vals[h->len]=v;h->len++;}")6624-
emit_raw("static mrb_bool sp_StrIntHash_has_key(sp_StrIntHash*h,const char*k){for(mrb_int i=0;i<h->len;i++)if(strcmp(h->keys[i],k)==0)return TRUE;return FALSE;}")6626+
emit_raw("typedef struct{const char**keys;mrb_int*vals;const char**order;mrb_int len;mrb_int cap;mrb_int mask;}sp_StrIntHash;")6627+
emit_raw("static void sp_StrIntHash_fin(void*p){sp_StrIntHash*h=(sp_StrIntHash*)p;free(h->keys);free(h->vals);free(h->order);}")6628+
emit_raw("static sp_StrIntHash*sp_StrIntHash_new(void){sp_StrIntHash*h=(sp_StrIntHash*)sp_gc_alloc(sizeof(sp_StrIntHash),sp_StrIntHash_fin,NULL);h->cap=16;h->mask=15;h->keys=(const char**)calloc(h->cap,sizeof(const char*));h->vals=(mrb_int*)calloc(h->cap,sizeof(mrb_int));h->order=(const char**)malloc(sizeof(const char*)*h->cap);h->len=0;return h;}")6629+
emit_raw("static void sp_StrIntHash_grow(sp_StrIntHash*h){mrb_int oc=h->cap;const char**ok=h->keys;mrb_int*ov=h->vals;h->cap*=2;h->mask=h->cap-1;h->keys=(const char**)calloc(h->cap,sizeof(const char*));h->vals=(mrb_int*)calloc(h->cap,sizeof(mrb_int));h->order=(const char**)realloc(h->order,sizeof(const char*)*h->cap);mrb_int ol=h->len;h->len=0;for(mrb_int i=0;i<oc;i++){if(ok[i]){mrb_int idx=(mrb_int)(sp_str_hash(ok[i])&h->mask);while(h->keys[idx])idx=(idx+1)&h->mask;h->keys[idx]=ok[i];h->vals[idx]=ov[i];h->len++;}}free(ok);free(ov);}")6630+
emit_raw("static mrb_int sp_StrIntHash_get(sp_StrIntHash*h,const char*k){mrb_int idx=(mrb_int)(sp_str_hash(k)&h->mask);while(h->keys[idx]){if(strcmp(h->keys[idx],k)==0)return h->vals[idx];idx=(idx+1)&h->mask;}return 0;}")6631+
emit_raw("static void sp_StrIntHash_set(sp_StrIntHash*h,const char*k,mrb_int v){if(h->len*2>=h->cap)sp_StrIntHash_grow(h);mrb_int idx=(mrb_int)(sp_str_hash(k)&h->mask);while(h->keys[idx]){if(strcmp(h->keys[idx],k)==0){h->vals[idx]=v;return;}idx=(idx+1)&h->mask;}h->keys[idx]=k;h->vals[idx]=v;h->order[h->len]=k;h->len++;}")6632+
emit_raw("static mrb_bool sp_StrIntHash_has_key(sp_StrIntHash*h,const char*k){mrb_int idx=(mrb_int)(sp_str_hash(k)&h->mask);while(h->keys[idx]){if(strcmp(h->keys[idx],k)==0)return TRUE;idx=(idx+1)&h->mask;}return FALSE;}")66256633
emit_raw("static mrb_int sp_StrIntHash_length(sp_StrIntHash*h){return h->len;}")6626-
emit_raw("static void sp_StrIntHash_delete(sp_StrIntHash*h,const char*k){for(mrb_int i=0;i<h->len;i++){if(strcmp(h->keys[i],k)==0){for(mrb_int j=i;j<h->len-1;j++){h->keys[j]=h->keys[j+1];h->vals[j]=h->vals[j+1];}h->len--;return;}}}")6627-
emit_raw("static sp_StrArray*sp_StrIntHash_keys(sp_StrIntHash*h){sp_StrArray*a=sp_StrArray_new();for(mrb_int i=0;i<h->len;i++)sp_StrArray_push(a,h->keys[i]);return a;}")6628-
emit_raw("static sp_StrIntHash*sp_StrIntHash_merge(sp_StrIntHash*a,sp_StrIntHash*b){sp_StrIntHash*r=sp_StrIntHash_new();for(mrb_int i=0;i<a->len;i++)sp_StrIntHash_set(r,a->keys[i],a->vals[i]);for(mrb_int i=0;i<b->len;i++)sp_StrIntHash_set(r,b->keys[i],b->vals[i]);return r;}")6634+
emit_raw("static void sp_StrIntHash_delete(sp_StrIntHash*h,const char*k){mrb_int idx=(mrb_int)(sp_str_hash(k)&h->mask);while(h->keys[idx]){if(strcmp(h->keys[idx],k)==0){h->keys[idx]=NULL;h->vals[idx]=0;h->len--;mrb_int j=(idx+1)&h->mask;while(h->keys[j]){mrb_int nj=(mrb_int)(sp_str_hash(h->keys[j])&h->mask);if((j>idx&&(nj<=idx||nj>j))||(j<idx&&nj<=idx&&nj>j)){h->keys[idx]=h->keys[j];h->vals[idx]=h->vals[j];h->keys[j]=NULL;h->vals[j]=0;idx=j;}j=(j+1)&h->mask;}return;}idx=(idx+1)&h->mask;}}")6635+
emit_raw("static sp_StrArray*sp_StrIntHash_keys(sp_StrIntHash*h){sp_StrArray*a=sp_StrArray_new();for(mrb_int i=0;i<h->len;i++)sp_StrArray_push(a,h->order[i]);return a;}")6636+
emit_raw("static sp_StrIntHash*sp_StrIntHash_merge(sp_StrIntHash*a,sp_StrIntHash*b){sp_StrIntHash*r=sp_StrIntHash_new();for(mrb_int i=0;i<a->len;i++)sp_StrIntHash_set(r,a->order[i],sp_StrIntHash_get(a,a->order[i]));for(mrb_int i=0;i<b->len;i++)sp_StrIntHash_set(r,b->order[i],sp_StrIntHash_get(b,b->order[i]));return r;}")66296637
emit_raw("")66306638
end66316639
66326640
def emit_str_str_hash_runtime6633-
emit_raw("typedef struct{const char**keys;const char**vals;mrb_int len;mrb_int cap;}sp_StrStrHash;")6634-
emit_raw("static void sp_StrStrHash_fin(void*p){sp_StrStrHash*h=(sp_StrStrHash*)p;free(h->keys);free(h->vals);}")6635-
emit_raw("static sp_StrStrHash*sp_StrStrHash_new(void){sp_StrStrHash*h=(sp_StrStrHash*)sp_gc_alloc(sizeof(sp_StrStrHash),sp_StrStrHash_fin,NULL);h->cap=16;h->keys=(const char**)malloc(sizeof(const char*)*h->cap);h->vals=(const char**)malloc(sizeof(const char*)*h->cap);h->len=0;return h;}")6636-
emit_raw("static const char*sp_StrStrHash_get(sp_StrStrHash*h,const char*k){for(mrb_int i=0;i<h->len;i++)if(strcmp(h->keys[i],k)==0)return h->vals[i];return\"\";}")6637-
emit_raw("static void sp_StrStrHash_set(sp_StrStrHash*h,const char*k,const char*v){for(mrb_int i=0;i<h->len;i++){if(strcmp(h->keys[i],k)==0){h->vals[i]=v;return;}}if(h->len>=h->cap){h->cap*=2;h->keys=(const char**)realloc(h->keys,sizeof(const char*)*h->cap);h->vals=(const char**)realloc(h->vals,sizeof(const char*)*h->cap);}h->keys[h->len]=k;h->vals[h->len]=v;h->len++;}")6638-
emit_raw("static mrb_bool sp_StrStrHash_has_key(sp_StrStrHash*h,const char*k){for(mrb_int i=0;i<h->len;i++)if(strcmp(h->keys[i],k)==0)return TRUE;return FALSE;}")6641+
emit_raw("typedef struct{const char**keys;const char**vals;const char**order;mrb_int len;mrb_int cap;mrb_int mask;}sp_StrStrHash;")6642+
emit_raw("static void sp_StrStrHash_fin(void*p){sp_StrStrHash*h=(sp_StrStrHash*)p;free(h->keys);free(h->vals);free(h->order);}")6643+
emit_raw("static sp_StrStrHash*sp_StrStrHash_new(void){sp_StrStrHash*h=(sp_StrStrHash*)sp_gc_alloc(sizeof(sp_StrStrHash),sp_StrStrHash_fin,NULL);h->cap=16;h->mask=15;h->keys=(const char**)calloc(h->cap,sizeof(const char*));h->vals=(const char**)calloc(h->cap,sizeof(const char*));h->order=(const char**)malloc(sizeof(const char*)*h->cap);h->len=0;return h;}")6644+
emit_raw("static void sp_StrStrHash_grow(sp_StrStrHash*h){mrb_int oc=h->cap;const char**ok=h->keys;const char**ov=h->vals;h->cap*=2;h->mask=h->cap-1;h->keys=(const char**)calloc(h->cap,sizeof(const char*));h->vals=(const char**)calloc(h->cap,sizeof(const char*));h->order=(const char**)realloc(h->order,sizeof(const char*)*h->cap);h->len=0;for(mrb_int i=0;i<oc;i++){if(ok[i]){mrb_int idx=(mrb_int)(sp_str_hash(ok[i])&h->mask);while(h->keys[idx])idx=(idx+1)&h->mask;h->keys[idx]=ok[i];h->vals[idx]=ov[i];h->len++;}}free(ok);free(ov);}")6645+
emit_raw("static const char*sp_StrStrHash_get(sp_StrStrHash*h,const char*k){mrb_int idx=(mrb_int)(sp_str_hash(k)&h->mask);while(h->keys[idx]){if(strcmp(h->keys[idx],k)==0)return h->vals[idx];idx=(idx+1)&h->mask;}return\"\";}")6646+
emit_raw("static void sp_StrStrHash_set(sp_StrStrHash*h,const char*k,const char*v){if(h->len*2>=h->cap)sp_StrStrHash_grow(h);mrb_int idx=(mrb_int)(sp_str_hash(k)&h->mask);while(h->keys[idx]){if(strcmp(h->keys[idx],k)==0){h->vals[idx]=v;return;}idx=(idx+1)&h->mask;}h->keys[idx]=k;h->vals[idx]=v;h->order[h->len]=k;h->len++;}")6647+
emit_raw("static mrb_bool sp_StrStrHash_has_key(sp_StrStrHash*h,const char*k){mrb_int idx=(mrb_int)(sp_str_hash(k)&h->mask);while(h->keys[idx]){if(strcmp(h->keys[idx],k)==0)return TRUE;idx=(idx+1)&h->mask;}return FALSE;}")66396648
emit_raw("static mrb_int sp_StrStrHash_length(sp_StrStrHash*h){return h->len;}")6640-
emit_raw("static void sp_StrStrHash_delete(sp_StrStrHash*h,const char*k){for(mrb_int i=0;i<h->len;i++){if(strcmp(h->keys[i],k)==0){for(mrb_int j=i;j<h->len-1;j++){h->keys[j]=h->keys[j+1];h->vals[j]=h->vals[j+1];}h->len--;return;}}}")6641-
emit_raw("static sp_StrArray*sp_StrStrHash_keys(sp_StrStrHash*h){sp_StrArray*a=sp_StrArray_new();for(mrb_int i=0;i<h->len;i++)sp_StrArray_push(a,h->keys[i]);return a;}")6649+
emit_raw("static void sp_StrStrHash_delete(sp_StrStrHash*h,const char*k){mrb_int idx=(mrb_int)(sp_str_hash(k)&h->mask);while(h->keys[idx]){if(strcmp(h->keys[idx],k)==0){h->keys[idx]=NULL;h->vals[idx]=NULL;h->len--;mrb_int j=(idx+1)&h->mask;while(h->keys[j]){mrb_int nj=(mrb_int)(sp_str_hash(h->keys[j])&h->mask);if((j>idx&&(nj<=idx||nj>j))||(j<idx&&nj<=idx&&nj>j)){h->keys[idx]=h->keys[j];h->vals[idx]=h->vals[j];h->keys[j]=NULL;h->vals[j]=NULL;idx=j;}j=(j+1)&h->mask;}return;}idx=(idx+1)&h->mask;}}")6650+
emit_raw("static sp_StrArray*sp_StrStrHash_keys(sp_StrStrHash*h){sp_StrArray*a=sp_StrArray_new();for(mrb_int i=0;i<h->len;i++)sp_StrArray_push(a,h->order[i]);return a;}")66426651
emit_raw("")66436652
end66446653
@@ -11617,7 +11626,7 @@
1161711626
tmp = new_temp1161811627
emit(" sp_StrIntHash *" + tmp + " = sp_StrIntHash_new();")1161911628
emit(" for (mrb_int _i = 0; _i < " + rc + "->len; _i++) {")11620-
emit(" mrb_int lv_" + bp + " = " + rc + "->vals[_i];")11629+
emit(" mrb_int lv_" + bp + " = sp_StrIntHash_get(" + rc + ", " + rc + "->order[_i]);")1162111630
bbody = @nd_body[blk]1162211631
bexpr = "0"1162311632
if bbody >= 0@@ -11626,7 +11635,7 @@
1162611635
bexpr = compile_expr(bs.last)1162711636
end1162811637
end11629-
emit(" sp_StrIntHash_set(" + tmp + ", " + rc + "->keys[_i], " + bexpr + ");")11638+
emit(" sp_StrIntHash_set(" + tmp + ", " + rc + "->order[_i], " + bexpr + ");")1163011639
emit(" }")1163111640
return tmp1163211641
end@@ -15037,9 +15046,9 @@
1503715046
if rt == "str_int_hash"1503815047
tmp = new_temp1503915048
emit(" for (mrb_int " + tmp + " = 0; " + tmp + " < " + rc + "->len; " + tmp + "++) {")15040-
emit(" lv_" + bp1 + " = " + rc + "->keys[" + tmp + "];")15049+
emit(" lv_" + bp1 + " = " + rc + "->order[" + tmp + "];")1504115050
if bp2 != ""15042-
emit(" lv_" + bp2 + " = " + rc + "->vals[" + tmp + "];")15051+
emit(" lv_" + bp2 + " = sp_StrIntHash_get(" + rc + ", " + rc + "->order[" + tmp + "]);")1504315052
end1504415053
@indent = @indent + 11504515054
compile_stmts_body(@nd_body[@nd_block[nid]])@@ -15049,9 +15058,9 @@
1504915058
if rt == "str_str_hash"1505015059
tmp = new_temp1505115060
emit(" for (mrb_int " + tmp + " = 0; " + tmp + " < " + rc + "->len; " + tmp + "++) {")15052-
emit(" lv_" + bp1 + " = " + rc + "->keys[" + tmp + "];")15061+
emit(" lv_" + bp1 + " = " + rc + "->order[" + tmp + "];")1505315062
if bp2 != ""15054-
emit(" lv_" + bp2 + " = " + rc + "->vals[" + tmp + "];")15063+
emit(" lv_" + bp2 + " = sp_StrStrHash_get(" + rc + ", " + rc + "->order[" + tmp + "]);")1505515064
end1505615065
@indent = @indent + 11505715066
compile_stmts_body(@nd_body[@nd_block[nid]])