Commit 0498336a

0498336aa3bdMerged

Yusuke Endoh <[email protected]> · 6 months ago · 6 files · +4 added · ~2 modified

Description

fix(codegen+runtime): poly-keyed Hash via PolyPolyHash variant

Optcarrot's CPU#add_mappings deduplicates Method / IntArray callbacks via @peeks[peek] ||= peek and @pokes[poke] ||= poke against an empty {} hash whose keys are heterogeneous Method or IntArray instances. Spinel had no hash variant accepting non-string/sym/int keys, so the lookup silently fell back to str_int_hash (key path mismatched) and the cache stayed empty — wasting ~5x memory on @fetch / @store and silently breaking the intended dedup.

Add SP_BUILTIN_POLY_POLY_HASH (-20) runtime variant and codegen:

  • runtime: sp_PolyPolyHash struct with sp_RbVal keys and values, pointer-identity comparison for SP_TAG_OBJ keys (matches CRuby's Method#hash semantics for our use case — same iv_self_obj + iv_fn_ptr → same key) and value comparison for primitive keys (SP_TAG_INT / STR / SYM).
  • codegen: cls_id_for_hash_type returns SP_BUILTIN_POLY_POLY_HASH for poly_poly_hash; the empty-hash promotion machinery refines {} to this variant when the first []= writes a poly-typed key. [k] ||= v, [k], [k] = v all dispatch through the new sp_PolyPolyHash_* runtime functions.

Tests added cover Method-keyed dedup (the optcarrot pattern) and a heterogeneous-key pipeline (Method / IntArray / int keys mixed).

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
0498336aa3bd
Parent
f6b89b028b0c

lib/sp_runtime.hmodifiedView file

@@ -793,6 +793,7 @@
793793 #define SP_BUILTIN_SYM_STR_HASH (-17)
794794 #define SP_BUILTIN_STR_POLY_HASH (-18)
795795 #define SP_BUILTIN_SYM_POLY_HASH (-19)
796+#define SP_BUILTIN_POLY_POLY_HASH (-20)
796797 typedef struct { int tag; int cls_id; union { mrb_int i; const char *s; mrb_float f; mrb_bool b; void *p; } v; } sp_RbVal;
797798 static sp_RbVal sp_box_int(mrb_int v) { sp_RbVal r; r.tag = SP_TAG_INT; r.cls_id = 0; r.v.i = v; return r; }
798799 static sp_RbVal sp_box_str(const char *v) { sp_RbVal r; r.tag = SP_TAG_STR; r.cls_id = 0; r.v.s = v; return r; }
@@ -1015,6 +1016,61 @@
10151016 static sp_IntArray*sp_SymPolyHash_keys(sp_SymPolyHash*h){sp_IntArray*a=sp_IntArray_new();for(mrb_int i=0;i<h->len;i++)sp_IntArray_push(a,(mrb_int)h->order[i]);return a;}
10161017 static sp_PolyArray*sp_SymPolyHash_values(sp_SymPolyHash*h){sp_PolyArray*a=sp_PolyArray_new();for(mrb_int i=0;i<h->len;i++)sp_PolyArray_push(a,sp_SymPolyHash_get(h,h->order[i]));return a;}
10171018
1019+/* PolyPolyHash: heterogeneous keys + values (both sp_RbVal). For
1020+ primitives the hash/eql is tag-based (value equality); for OBJ tag
1021+ the default is pointer identity. Codegen patches sp_obj_hash_hook /
1022+ sp_obj_eql_hook with class-aware overrides (e.g. Method#eql? compares
1023+ bound receiver + fn_ptr) when the program needs them — null hooks
1024+ leave the runtime at identity, which is the right default for typed
1025+ pointers like IntArray. */
1026+typedef mrb_int (*sp_obj_hash_fn)(int cls_id, void *p);
1027+typedef mrb_bool (*sp_obj_eql_fn)(int cls_id, void *a, void *b);
1028+static sp_obj_hash_fn sp_obj_hash_hook = NULL;
1029+static sp_obj_eql_fn sp_obj_eql_hook = NULL;
1030+static mrb_int sp_rbval_hash_key(sp_RbVal v) {
1031+ switch (v.tag) {
1032+ case SP_TAG_INT: case SP_TAG_BOOL: case SP_TAG_NIL: case SP_TAG_SYM:
1033+ return (mrb_int)v.v.i;
1034+ case SP_TAG_STR:
1035+ return v.v.s ? (mrb_int)sp_str_hash(v.v.s) : 0;
1036+ case SP_TAG_FLT: { uint64_t b; memcpy(&b, &v.v.f, sizeof(b)); return (mrb_int)b; }
1037+ case SP_TAG_OBJ:
1038+ if (sp_obj_hash_hook) return sp_obj_hash_hook(v.cls_id, v.v.p);
1039+ return (mrb_int)((uintptr_t)v.v.p);
1040+ }
1041+ return 0;
1042+}
1043+static mrb_bool sp_rbval_eql_key(sp_RbVal a, sp_RbVal b) {
1044+ if (a.tag != b.tag) return FALSE;
1045+ switch (a.tag) {
1046+ case SP_TAG_INT: case SP_TAG_BOOL: case SP_TAG_NIL: case SP_TAG_SYM:
1047+ return a.v.i == b.v.i;
1048+ case SP_TAG_STR:
1049+ if (a.v.s == b.v.s) return TRUE;
1050+ if (!a.v.s || !b.v.s) return FALSE;
1051+ return strcmp(a.v.s, b.v.s) == 0;
1052+ case SP_TAG_FLT:
1053+ return a.v.f == b.v.f;
1054+ case SP_TAG_OBJ:
1055+ if (a.cls_id != b.cls_id) return FALSE;
1056+ if (a.v.p == b.v.p) return TRUE;
1057+ if (sp_obj_eql_hook) return sp_obj_eql_hook(a.cls_id, a.v.p, b.v.p);
1058+ return FALSE;
1059+ }
1060+ return FALSE;
1061+}
1062+typedef struct{sp_RbVal*keys;sp_RbVal*vals;mrb_int*order;mrb_bool*occ;mrb_int len;mrb_int cap;mrb_int mask;}sp_PolyPolyHash;
1063+static void sp_PolyPolyHash_fin(void*p){sp_PolyPolyHash*h=(sp_PolyPolyHash*)p;free(h->keys);free(h->vals);free(h->order);free(h->occ);}
1064+static void sp_PolyPolyHash_scan(void*p){sp_PolyPolyHash*h=(sp_PolyPolyHash*)p;for(mrb_int i=0;i<h->cap;i++){if(h->occ[i]){sp_mark_rbval(h->keys[i]);sp_mark_rbval(h->vals[i]);}}}
1065+static sp_PolyPolyHash*sp_PolyPolyHash_new(void){sp_PolyPolyHash*h=(sp_PolyPolyHash*)sp_gc_alloc(sizeof(sp_PolyPolyHash),sp_PolyPolyHash_fin,sp_PolyPolyHash_scan);h->cap=16;h->mask=15;h->keys=(sp_RbVal*)calloc(h->cap,sizeof(sp_RbVal));h->vals=(sp_RbVal*)calloc(h->cap,sizeof(sp_RbVal));h->order=(mrb_int*)malloc(sizeof(mrb_int)*h->cap);h->occ=(mrb_bool*)calloc(h->cap,sizeof(mrb_bool));h->len=0;return h;}
1066+static void sp_PolyPolyHash_grow(sp_PolyPolyHash*h){mrb_int oc=h->cap;sp_RbVal*ok=h->keys;sp_RbVal*ov=h->vals;mrb_bool*oo=h->occ;mrb_int*oord=h->order;mrb_int olen=h->len;h->cap*=2;h->mask=h->cap-1;h->keys=(sp_RbVal*)calloc(h->cap,sizeof(sp_RbVal));h->vals=(sp_RbVal*)calloc(h->cap,sizeof(sp_RbVal));h->order=(mrb_int*)malloc(sizeof(mrb_int)*h->cap);h->occ=(mrb_bool*)calloc(h->cap,sizeof(mrb_bool));for(mrb_int i=0;i<olen;i++){mrb_int oi=oord[i];sp_RbVal k=ok[oi];mrb_int idx=(mrb_int)(sp_rbval_hash_key(k)&h->mask);while(h->occ[idx])idx=(idx+1)&h->mask;h->keys[idx]=k;h->vals[idx]=ov[oi];h->occ[idx]=TRUE;h->order[i]=idx;}free(ok);free(ov);free(oo);free(oord);}
1067+static sp_RbVal sp_PolyPolyHash_get(sp_PolyPolyHash*h,sp_RbVal k){mrb_int idx=(mrb_int)(sp_rbval_hash_key(k)&h->mask);while(h->occ[idx]){if(sp_rbval_eql_key(h->keys[idx],k))return h->vals[idx];idx=(idx+1)&h->mask;}return sp_box_nil();}
1068+static void sp_PolyPolyHash_set(sp_PolyPolyHash*h,sp_RbVal k,sp_RbVal v){if(h->len*2>=h->cap)sp_PolyPolyHash_grow(h);mrb_int idx=(mrb_int)(sp_rbval_hash_key(k)&h->mask);while(h->occ[idx]){if(sp_rbval_eql_key(h->keys[idx],k)){h->vals[idx]=v;return;}idx=(idx+1)&h->mask;}h->keys[idx]=k;h->vals[idx]=v;h->occ[idx]=TRUE;h->order[h->len]=idx;h->len++;}
1069+static mrb_bool sp_PolyPolyHash_has_key(sp_PolyPolyHash*h,sp_RbVal k){mrb_int idx=(mrb_int)(sp_rbval_hash_key(k)&h->mask);while(h->occ[idx]){if(sp_rbval_eql_key(h->keys[idx],k))return TRUE;idx=(idx+1)&h->mask;}return FALSE;}
1070+static mrb_int sp_PolyPolyHash_length(sp_PolyPolyHash*h){return h->len;}
1071+static sp_PolyArray*sp_PolyPolyHash_keys(sp_PolyPolyHash*h){sp_PolyArray*a=sp_PolyArray_new();for(mrb_int i=0;i<h->len;i++)sp_PolyArray_push(a,h->keys[h->order[i]]);return a;}
1072+static sp_PolyArray*sp_PolyPolyHash_values(sp_PolyPolyHash*h){sp_PolyArray*a=sp_PolyArray_new();for(mrb_int i=0;i<h->len;i++)sp_PolyArray_push(a,h->vals[h->order[i]]);return a;}
1073+
10181074 #include <setjmp.h>
10191075 #define SP_EXC_STACK_MAX 64
10201076 static jmp_buf sp_exc_stack[SP_EXC_STACK_MAX];