Commit e4d31585

e4d3158536d3Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified

Description

Method-keyed hashes: eql/hash by (self, fn, name) + class-method binding

Bound Methods used as Hash keys (optcarrot's @peeks/@pokes dedup) now hash and eql by their (bound self, fn pointer, name) rather than by identity, so two obj.method(:m) instances collapse to one entry and distinct methods stay distinct. The name disambiguates class methods, whose fn slot is 0.

  • runtime: SP_BUILTIN_METHOD arms in sp_rbval_hash_key / sp_rbval_eql_key (sp_BoundMethod moved earlier so both see it); sp_poly_index_poly looks a heterogeneous key up directly in a PolyPolyHash.
  • codegen: Klass.method(:cmeth) passes NULL self (a class value is not a heap pointer); poly [] boxes a non-poly key; poly ||= index-or-write looks up via sp_poly_index_poly; poly length/size dispatch gains hash cases.

Fixes poly_keyed_hash_pipeline and poly_keyed_hash_method_dedup.

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

Details

Commit
e4d3158536d3
Parent
fa9ec8a3a75f

lib/sp_runtime.hmodifiedView file

@@ -3864,6 +3864,7 @@
38643864 typedef mrb_bool (*sp_obj_eql_fn)(int cls_id, void *a, void *b);
38653865 static sp_obj_hash_fn sp_obj_hash_hook = NULL;
38663866 static sp_obj_eql_fn sp_obj_eql_hook = NULL;
3867+typedef struct sp_BoundMethod { void *self; mrb_int fn; const char *name; } sp_BoundMethod;
38673868 static mrb_int sp_rbval_hash_key(sp_RbVal v) {
38683869 switch (v.tag) {
38693870 case SP_TAG_INT: case SP_TAG_BOOL: case SP_TAG_NIL: case SP_TAG_SYM:
@@ -3879,6 +3880,16 @@
38793880 mrb_int h = 0; if (ia) for (mrb_int i = 0; i < ia->len; i++) h = h * 31 + ia->data[ia->start+i];
38803881 return h;
38813882 }
3883+ if (v.cls_id == SP_BUILTIN_METHOD) {
3884+ /* Method keys hash/eql by (bound self, fn ptr, name), so two
3885+ `obj.method(:m)` instances collapse to one entry (optcarrot
3886+ @peeks/@pokes dedup). The name disambiguates class methods, whose
3887+ fn slot is 0 (no resolvable callable address). */
3888+ sp_BoundMethod *m = (sp_BoundMethod *)v.v.p;
3889+ if (!m) return 0;
3890+ return (mrb_int)((uintptr_t)m->self * 31 + m->fn) +
3891+ (m->name ? (mrb_int)sp_str_hash(m->name) : 0);
3892+ }
38823893 if (sp_obj_hash_hook) return sp_obj_hash_hook(v.cls_id, v.v.p);
38833894 return (mrb_int)((uintptr_t)v.v.p);
38843895 }
@@ -3909,6 +3920,13 @@
39093920 for (mrb_int i = 0; i < ia->len; i++) if (ia->data[ia->start+i] != ib->data[ib->start+i]) return FALSE;
39103921 return TRUE;
39113922 }
3923+ if (a.cls_id == SP_BUILTIN_METHOD) {
3924+ sp_BoundMethod *ma = (sp_BoundMethod *)a.v.p, *mb = (sp_BoundMethod *)b.v.p;
3925+ if (!ma || !mb) return ma == mb;
3926+ if (ma->self != mb->self || ma->fn != mb->fn) return FALSE;
3927+ if (ma->name == mb->name) return TRUE;
3928+ return ma->name && mb->name && strcmp(ma->name, mb->name) == 0;
3929+ }
39123930 if (sp_obj_eql_hook) return sp_obj_eql_hook(a.cls_id, a.v.p, b.v.p);
39133931 return FALSE;
39143932 }
@@ -4010,7 +4028,6 @@
40104028 }
40114029 }
40124030 /* poly_arr_get/set for PolyPolyHash with integer index key. */
4013-typedef struct sp_BoundMethod { void *self; mrb_int fn; const char *name; } sp_BoundMethod;
40144031 static sp_RbVal sp_poly_arr_get_hash(sp_RbVal a, mrb_int i) {
40154032 if (a.tag == SP_TAG_INT) return sp_box_int((a.v.i >> i) & 1);
40164033 if (a.tag == SP_TAG_OBJ && a.cls_id == SP_BUILTIN_POLY_POLY_HASH)
@@ -4028,6 +4045,9 @@
40284045 }
40294046 /* poly[poly_key]: dispatch on key tag at runtime. */
40304047 static sp_RbVal sp_poly_index_poly(sp_RbVal recv, sp_RbVal idx) {
4048+ /* heterogeneous-key hash: any key kind (incl. Method) looks up directly. */
4049+ if (recv.tag == SP_TAG_OBJ && recv.cls_id == SP_BUILTIN_POLY_POLY_HASH)
4050+ return sp_PolyPolyHash_get((sp_PolyPolyHash *)recv.v.p, idx);
40314051 if (idx.tag == SP_TAG_STR) return sp_poly_get_str(recv, idx.v.s);
40324052 if (idx.tag == SP_TAG_SYM) return sp_poly_get_sym(recv, (sp_sym)idx.v.i);
40334053 mrb_int i = (idx.tag == SP_TAG_INT) ? idx.v.i : 0;