Commit ef0ad01c

ef0ad01cfc3dMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 2 files · +1 added · ~1 modified

Description

feat: Symbol Phase 2 Step 5 - cross-type eq, type-filtered hash lookups, static intern

Complete Phase 2 with correctness fixes and an optimization pass:

  1. compile_eq handles symbol:

    • sym == sym → int compare
    • sym vs non-sym (or vice versa) → always unequal (Ruby semantics)
  2. sym_int_hash / sym_str_hash [] and has_key? now short-circuit to FALSE / sentinel when the arg is not a symbol, matching Ruby (distinct key types never collide) and avoiding compiler warnings from int/pointer confusion.

  3. Static intern optimization:

    • collect_sym_names scans CallNode for "literal".to_sym / .intern and adds the receiver string to @sym_names.
    • compile_method_call_expr short-circuits "literal".to_sym to the compile-time SPS_ constant instead of the sp_sym_intern runtime strcmp/malloc.
    • "foo".to_sym == :foo now compiles to SPS_foo == SPS_foo, which the C compiler folds to 1.
  4. Add test/bm_sym_semantics.rb locking in the Ruby-compat requirements: :a != "a", h.has_key?("a") on sym-keyed hash false, "foo".to_sym == :foo true, etc.

74/74 tests pass, bootstrap stable, benchmarks 51 pass / 2 err.

Phase 2 complete.

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

Details

Commit
ef0ad01cfc3d
Parent
34d56ecea51a

spinel_codegen.rbmodifiedView file

@@ -7643,12 +7643,27 @@
76437643 local = "".split(",")
76447644 i = 0
76457645 while i < @nd_type.length
7646- if @nd_type[i] == "SymbolNode"
7646+ t = @nd_type[i]
7647+ if t == "SymbolNode"
76477648 sname = @nd_content[i]
76487649 if not_in(sname, local) == 1
76497650 local.push(sname)
76507651 end
76517652 end
7653+ # Also collect "literal".to_sym / .intern receivers so the
7654+ # static-intern optimization can resolve them to SPS_ constants.
7655+ if t == "CallNode"
7656+ mn = @nd_name[i]
7657+ if mn == "to_sym" || mn == "intern"
7658+ r = @nd_receiver[i]
7659+ if r >= 0 && @nd_type[r] == "StringNode"
7660+ lname = @nd_content[r]
7661+ if not_in(lname, local) == 1
7662+ local.push(lname)
7663+ end
7664+ end
7665+ end
7666+ end
76527667 i = i + 1
76537668 end
76547669 @sym_names = local
@@ -11457,6 +11472,19 @@
1145711472 end
1145811473 end
1145911474
11475+ # Static intern optimization: "literal".to_sym / .intern where the
11476+ # string content is already in @sym_names becomes a compile-time
11477+ # constant (SPS_<name> or ((sp_sym)<idx>)), avoiding the runtime
11478+ # sp_sym_intern strcmp loop and dynamic pool allocation.
11479+ if recv_type == "string" && (mname == "to_sym" || mname == "intern")
11480+ if recv >= 0 && @nd_type[recv] == "StringNode"
11481+ sname = @nd_content[recv]
11482+ if sym_name_index(sname) >= 0
11483+ return compile_symbol_literal(sname)
11484+ end
11485+ end
11486+ end
11487+
1146011488 # String methods
1146111489 if recv_type == "string"
1146211490 r = compile_string_method_expr(nid, mname, rc)
@@ -13617,9 +13645,23 @@
1361713645 # Hash methods
1361813646 if recv_type == "sym_int_hash"
1361913647 if mname == "[]"
13648+ args_id0 = @nd_arguments[nid]
13649+ if args_id0 >= 0
13650+ aa0 = get_args(args_id0)
13651+ if aa0.length > 0 && infer_type(aa0[0]) != "symbol"
13652+ return "((mrb_int)0)"
13653+ end
13654+ end
1362013655 return "sp_SymIntHash_get(" + rc + ", " + compile_arg0(nid) + ")"
1362113656 end
1362213657 if mname == "has_key?" || mname == "key?" || mname == "include?" || mname == "member?"
13658+ args_id1 = @nd_arguments[nid]
13659+ if args_id1 >= 0
13660+ aa1 = get_args(args_id1)
13661+ if aa1.length > 0 && infer_type(aa1[0]) != "symbol"
13662+ return "FALSE"
13663+ end
13664+ end
1362313665 return "sp_SymIntHash_has_key(" + rc + ", " + compile_arg0(nid) + ")"
1362413666 end
1362513667 if mname == "length" || mname == "size" || (mname == "count" && @nd_block[nid] < 0 && @nd_arguments[nid] < 0)
@@ -13646,9 +13688,23 @@
1364613688 end
1364713689 if recv_type == "sym_str_hash"
1364813690 if mname == "[]"
13691+ args_id0s = @nd_arguments[nid]
13692+ if args_id0s >= 0
13693+ aa0s = get_args(args_id0s)
13694+ if aa0s.length > 0 && infer_type(aa0s[0]) != "symbol"
13695+ return "(\"\\xff\" + 1)"
13696+ end
13697+ end
1364913698 return "sp_SymStrHash_get(" + rc + ", " + compile_arg0(nid) + ")"
1365013699 end
1365113700 if mname == "has_key?" || mname == "key?" || mname == "include?" || mname == "member?"
13701+ args_id1s = @nd_arguments[nid]
13702+ if args_id1s >= 0
13703+ aa1s = get_args(args_id1s)
13704+ if aa1s.length > 0 && infer_type(aa1s[0]) != "symbol"
13705+ return "FALSE"
13706+ end
13707+ end
1365213708 return "sp_SymStrHash_has_key(" + rc + ", " + compile_arg0(nid) + ")"
1365313709 end
1365413710 if mname == "length" || mname == "size" || (mname == "count" && @nd_block[nid] < 0 && @nd_arguments[nid] < 0)
@@ -14384,6 +14440,22 @@
1438414440 if arg_id >= 0
1438514441 rc = compile_expr(arg_id)
1438614442 end
14443+ # Symbol equality: distinct from all non-symbol types in Ruby.
14444+ if lt == "symbol"
14445+ if at == "symbol"
14446+ if op == "=="
14447+ return "(" + lc + " == " + rc + ")"
14448+ else
14449+ return "(" + lc + " != " + rc + ")"
14450+ end
14451+ end
14452+ # sym vs non-sym: always unequal in Ruby
14453+ return op == "==" ? "FALSE" : "TRUE"
14454+ end
14455+ if at == "symbol"
14456+ # non-sym lhs vs sym rhs: also always unequal
14457+ return op == "==" ? "FALSE" : "TRUE"
14458+ end
1438714459 if lt == "string"
1438814460 if at == "nil"
1438914461 if op == "=="