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") >= 0gate on the dispatch install — IntArray dispatch is needed regardless of whether Method class is in scope. -
Set
@needs_poly_poly_hashin 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
return "sp_SymPolyHash *" end if t == "poly_poly_hash" @needs_poly_poly_hash = 1 @needs_rb_value = 1 return "sp_PolyPolyHash *" end if t == "sym_array" elsif promoted == "sym_poly_hash" @needs_sym_poly_hash = 1 @needs_rb_value = 1 elsif promoted == "poly_poly_hash" @needs_poly_poly_hash = 1 @needs_rb_value = 1 end end end end # ---- Main emission ---- # Emit the cls_id-aware obj hash/eql dispatch shims that # sp_PolyPolyHash uses for OBJ-tag keys. The default runtime # behavior is pointer identity; this dispatch overrides it for # classes whose `eql?` semantics are content-based: # # - sp_Method (when the class is in scope): equal iff bound # receiver + fn_ptr match — covers `obj.method(:foo)` dedup. # - sp_IntArray (SP_BUILTIN_INT_ARRAY = -1): element-wise content # compare — covers `entries[[a, b]] ||= ...` array-keyed Hash # patterns. def emit_method_eql_dispatch return unless @needs_poly_poly_hash == 1 return if find_class_idx("Method") < 0 method_cid = find_class_idx("Method").to_s has_method = find_class_idx("Method") >= 0 method_cid = has_method ? find_class_idx("Method").to_s : "" emit_raw("static mrb_int sp_method_obj_hash_dispatch(int cls_id, void *p) {") emit_raw(" if (cls_id == " + method_cid + ") {") emit_raw(" sp_Method *m = (sp_Method *)p;") emit_raw(" return ((mrb_int)(uintptr_t)m->iv_self_obj * 31) ^ (mrb_int)m->iv_fn_ptr;") if has_method emit_raw(" if (cls_id == " + method_cid + ") {") emit_raw(" sp_Method *m = (sp_Method *)p;") emit_raw(" return ((mrb_int)(uintptr_t)m->iv_self_obj * 31) ^ (mrb_int)m->iv_fn_ptr;") emit_raw(" }") end emit_raw(" if (cls_id == SP_BUILTIN_INT_ARRAY) {") emit_raw(" sp_IntArray *a = (sp_IntArray *)p;") emit_raw(" mrb_int h = 5381;") emit_raw(" for (mrb_int i = 0; i < a->len; i++) h = ((h << 5) + h) ^ sp_IntArray_get(a, i);") emit_raw(" return h;") emit_raw(" }") emit_raw(" return (mrb_int)((uintptr_t)p);") emit_raw("}") emit_raw("static mrb_bool sp_method_obj_eql_dispatch(int cls_id, void *a, void *b) {") emit_raw(" if (cls_id == " + method_cid + ") {") emit_raw(" sp_Method *ma = (sp_Method *)a;") emit_raw(" sp_Method *mb = (sp_Method *)b;") emit_raw(" return ma->iv_self_obj == mb->iv_self_obj && ma->iv_fn_ptr == mb->iv_fn_ptr;") if has_method emit_raw(" if (cls_id == " + method_cid + ") {") emit_raw(" sp_Method *ma = (sp_Method *)a;") emit_raw(" sp_Method *mb = (sp_Method *)b;") emit_raw(" return ma->iv_self_obj == mb->iv_self_obj && ma->iv_fn_ptr == mb->iv_fn_ptr;") emit_raw(" }") end emit_raw(" if (cls_id == SP_BUILTIN_INT_ARRAY) {") emit_raw(" sp_IntArray *aa = (sp_IntArray *)a;") emit_raw(" sp_IntArray *bb = (sp_IntArray *)b;") emit_raw(" if (aa->len != bb->len) return FALSE;") emit_raw(" for (mrb_int i = 0; i < aa->len; i++) if (sp_IntArray_get(aa, i) != sp_IntArray_get(bb, i)) return FALSE;") emit_raw(" return TRUE;") emit_raw(" }") emit_raw(" return FALSE;") emit_raw("}") end # poly_poly_hash uses sp_obj_hash_hook / sp_obj_eql_hook for cls_id- # aware OBJ-tag dispatch. The hooks default to identity; install # the codegen-emitted Method-aware shim so equivalent # the codegen-emitted dispatch shim so equivalent # `obj.method(:foo)` instances (fresh allocations, but same iv_self_obj # + iv_fn_ptr) dedup as eql? in @cache[m] ||= m style use. if @needs_poly_poly_hash == 1 && find_class_idx("Method") >= 0 # + iv_fn_ptr) dedup as eql? in @cache[m] ||= m style use, and # IntArray keys (e.g. `entries[[a, b]] ||= ...`) compare by # element-wise content rather than pointer identity. if @needs_poly_poly_hash == 1 emit_raw(" sp_obj_hash_hook = sp_method_obj_hash_dispatch;") emit_raw(" sp_obj_eql_hook = sp_method_obj_eql_dispatch;") endtest/array_keyed_hash_int_array_eql.rbaddedView file
Loading diff…
test/array_keyed_hash_int_array_eql.rb.expectedaddedView file
Loading diff…