Commit bc92f28e
bc92f28e9f12Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
Merge pull request #364 from mame/pr/array-keyed-hash-int-array-eql
fix(codegen+runtime): array-keyed Hash via IntArray content hash/eql
Details
- Commit
- bc92f28e9f12
- Parent
- 4a8ca9dea493
- Merge parent
- 972c90cfcf65
spinel_codegen.rbmodifiedView file
@@ -5482,6 +5482,8 @@
54825482
return "sp_SymPolyHash *"54835483
end54845484
if t == "poly_poly_hash"5485+
@needs_poly_poly_hash = 15486+
@needs_rb_value = 154855487
return "sp_PolyPolyHash *"54865488
end54875489
if t == "sym_array"@@ -19670,6 +19672,9 @@
1967019672
elsif promoted == "sym_poly_hash"1967119673
@needs_sym_poly_hash = 11967219674
@needs_rb_value = 119675+
elsif promoted == "poly_poly_hash"19676+
@needs_poly_poly_hash = 119677+
@needs_rb_value = 11967319678
end1967419679
end1967519680
end@@ -20147,22 +20152,49 @@
2014720152
end2014820153
2014920154
# ---- Main emission ----20155+
# Emit the cls_id-aware obj hash/eql dispatch shims that20156+
# sp_PolyPolyHash uses for OBJ-tag keys. The default runtime20157+
# behavior is pointer identity; this dispatch overrides it for20158+
# classes whose `eql?` semantics are content-based:20159+
#20160+
# - sp_Method (when the class is in scope): equal iff bound20161+
# receiver + fn_ptr match — covers `obj.method(:foo)` dedup.20162+
# - sp_IntArray (SP_BUILTIN_INT_ARRAY = -1): element-wise content20163+
# compare — covers `entries[[a, b]] ||= ...` array-keyed Hash20164+
# patterns.2015020165
def emit_method_eql_dispatch2015120166
return unless @needs_poly_poly_hash == 120152-
return if find_class_idx("Method") < 020153-
method_cid = find_class_idx("Method").to_s20167+
has_method = find_class_idx("Method") >= 020168+
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_method20171+
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+
end20176+
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_method20186+
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+
end20192+
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
end2018420216
# 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; install20186-
# the codegen-emitted Method-aware shim so equivalent20218+
# the codegen-emitted dispatch shim so equivalent2018720219
# `obj.method(:foo)` instances (fresh allocations, but same iv_self_obj20188-
# + iv_fn_ptr) dedup as eql? in @cache[m] ||= m style use.20189-
if @needs_poly_poly_hash == 1 && find_class_idx("Method") >= 020220+
# + iv_fn_ptr) dedup as eql? in @cache[m] ||= m style use, and20221+
# IntArray keys (e.g. `entries[[a, b]] ||= ...`) compare by20222+
# element-wise content rather than pointer identity.20223+
if @needs_poly_poly_hash == 12019020224
emit_raw(" sp_obj_hash_hook = sp_method_obj_hash_dispatch;")2019120225
emit_raw(" sp_obj_eql_hook = sp_method_obj_eql_dispatch;")2019220226
endtest/array_keyed_hash_int_array_eql.rbaddedView file
Loading diff…
test/array_keyed_hash_int_array_eql.rb.expectedaddedView file
Loading diff…