Commit 972c90cf

972c90cfcf65Merged

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

Description

fix(codegen+runtime): array-keyed Hash via IntArray content hash/eql

entries[[a, b]] ||= ... was broken because PolyPolyHash defaulted to pointer-identity for SP_TAG_OBJ keys — every [a, b] literal allocates a fresh IntArray, so two same-content keys hashed differently and never matched. The ||= always set, the cache grew unboundedly, and reads with a fresh [a, b] returned nil.

Extend sp_method_obj_hash_dispatch / sp_method_obj_eql_dispatch (now misnamed slightly — kept for back-compat with the existing Method-only emit) to also dispatch SP_BUILTIN_INT_ARRAY with element-wise content hash (DJB2 fold over elements) and content eql? (pairwise compare). Two IntArrays with the same elements now hash and eql? identically.

Also:

  • Drop the find_class_idx("Method") >= 0 gate on the dispatch install — IntArray dispatch is needed regardless of whether Method class is in scope.

  • Set @needs_poly_poly_hash in c_type and at the scan_locals empty-hash promotion site so the dispatch shim emits whenever the program uses a poly_poly_hash. Without these, the new local-var-promotion path silently dropped the flag and the hooks installed as NULL.

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

Details

Commit
972c90cfcf65
Parent
59f48e594a9d

spinel_codegen.rbmodifiedView file

@@ -5482,6 +5482,8 @@
54825482 return "sp_SymPolyHash *"
54835483 end
54845484 if t == "poly_poly_hash"
5485+ @needs_poly_poly_hash = 1
5486+ @needs_rb_value = 1
54855487 return "sp_PolyPolyHash *"
54865488 end
54875489 if t == "sym_array"
@@ -19670,6 +19672,9 @@
1967019672 elsif promoted == "sym_poly_hash"
1967119673 @needs_sym_poly_hash = 1
1967219674 @needs_rb_value = 1
19675+ elsif promoted == "poly_poly_hash"
19676+ @needs_poly_poly_hash = 1
19677+ @needs_rb_value = 1
1967319678 end
1967419679 end
1967519680 end
@@ -20147,22 +20152,49 @@
2014720152 end
2014820153
2014920154 # ---- Main emission ----
20155+ # Emit the cls_id-aware obj hash/eql dispatch shims that
20156+ # sp_PolyPolyHash uses for OBJ-tag keys. The default runtime
20157+ # behavior is pointer identity; this dispatch overrides it for
20158+ # classes whose `eql?` semantics are content-based:
20159+ #
20160+ # - sp_Method (when the class is in scope): equal iff bound
20161+ # receiver + fn_ptr match — covers `obj.method(:foo)` dedup.
20162+ # - sp_IntArray (SP_BUILTIN_INT_ARRAY = -1): element-wise content
20163+ # compare — covers `entries[[a, b]] ||= ...` array-keyed Hash
20164+ # patterns.
2015020165 def emit_method_eql_dispatch
2015120166 return unless @needs_poly_poly_hash == 1
20152- return if find_class_idx("Method") < 0
20153- method_cid = find_class_idx("Method").to_s
20167+ has_method = find_class_idx("Method") >= 0
20168+ method_cid = has_method ? find_class_idx("Method").to_s : ""
2015420169 emit_raw("static mrb_int sp_method_obj_hash_dispatch(int cls_id, void *p) {")
20155- emit_raw(" if (cls_id == " + method_cid + ") {")
20156- emit_raw(" sp_Method *m = (sp_Method *)p;")
20157- emit_raw(" return ((mrb_int)(uintptr_t)m->iv_self_obj * 31) ^ (mrb_int)m->iv_fn_ptr;")
20170+ if has_method
20171+ emit_raw(" if (cls_id == " + method_cid + ") {")
20172+ emit_raw(" sp_Method *m = (sp_Method *)p;")
20173+ emit_raw(" return ((mrb_int)(uintptr_t)m->iv_self_obj * 31) ^ (mrb_int)m->iv_fn_ptr;")
20174+ emit_raw(" }")
20175+ end
20176+ emit_raw(" if (cls_id == SP_BUILTIN_INT_ARRAY) {")
20177+ emit_raw(" sp_IntArray *a = (sp_IntArray *)p;")
20178+ emit_raw(" mrb_int h = 5381;")
20179+ emit_raw(" for (mrb_int i = 0; i < a->len; i++) h = ((h << 5) + h) ^ sp_IntArray_get(a, i);")
20180+ emit_raw(" return h;")
2015820181 emit_raw(" }")
2015920182 emit_raw(" return (mrb_int)((uintptr_t)p);")
2016020183 emit_raw("}")
2016120184 emit_raw("static mrb_bool sp_method_obj_eql_dispatch(int cls_id, void *a, void *b) {")
20162- emit_raw(" if (cls_id == " + method_cid + ") {")
20163- emit_raw(" sp_Method *ma = (sp_Method *)a;")
20164- emit_raw(" sp_Method *mb = (sp_Method *)b;")
20165- emit_raw(" return ma->iv_self_obj == mb->iv_self_obj && ma->iv_fn_ptr == mb->iv_fn_ptr;")
20185+ if has_method
20186+ emit_raw(" if (cls_id == " + method_cid + ") {")
20187+ emit_raw(" sp_Method *ma = (sp_Method *)a;")
20188+ emit_raw(" sp_Method *mb = (sp_Method *)b;")
20189+ emit_raw(" return ma->iv_self_obj == mb->iv_self_obj && ma->iv_fn_ptr == mb->iv_fn_ptr;")
20190+ emit_raw(" }")
20191+ end
20192+ emit_raw(" if (cls_id == SP_BUILTIN_INT_ARRAY) {")
20193+ emit_raw(" sp_IntArray *aa = (sp_IntArray *)a;")
20194+ emit_raw(" sp_IntArray *bb = (sp_IntArray *)b;")
20195+ emit_raw(" if (aa->len != bb->len) return FALSE;")
20196+ emit_raw(" for (mrb_int i = 0; i < aa->len; i++) if (sp_IntArray_get(aa, i) != sp_IntArray_get(bb, i)) return FALSE;")
20197+ emit_raw(" return TRUE;")
2016620198 emit_raw(" }")
2016720199 emit_raw(" return FALSE;")
2016820200 emit_raw("}")
@@ -20183,10 +20215,12 @@
2018320215 end
2018420216 # poly_poly_hash uses sp_obj_hash_hook / sp_obj_eql_hook for cls_id-
2018520217 # aware OBJ-tag dispatch. The hooks default to identity; install
20186- # the codegen-emitted Method-aware shim so equivalent
20218+ # the codegen-emitted dispatch shim so equivalent
2018720219 # `obj.method(:foo)` instances (fresh allocations, but same iv_self_obj
20188- # + iv_fn_ptr) dedup as eql? in @cache[m] ||= m style use.
20189- if @needs_poly_poly_hash == 1 && find_class_idx("Method") >= 0
20220+ # + iv_fn_ptr) dedup as eql? in @cache[m] ||= m style use, and
20221+ # IntArray keys (e.g. `entries[[a, b]] ||= ...`) compare by
20222+ # element-wise content rather than pointer identity.
20223+ if @needs_poly_poly_hash == 1
2019020224 emit_raw(" sp_obj_hash_hook = sp_method_obj_hash_dispatch;")
2019120225 emit_raw(" sp_obj_eql_hook = sp_method_obj_eql_dispatch;")
2019220226 end