Commit f1aaf058

f1aaf058a17eMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified

Description

fix: don't mark self-recursive methods as static inline

static inline on a recursive function tempts gcc to inline harder than it should, blowing up code size and hurting performance.

Observed regression: bm_fib went from 12ms to 20ms (+66%) after marking the recursive fib as static inline. With this fix, fib stays plain static and performance is preserved.

Add node_calls_name? helper that scans a method body AST for calls to a given name (self-recursion check).

75/75 tests pass, bootstrap stable.

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

Details

Commit
f1aaf058a17e
Parent
b75f70ce1e5c

spinel_codegen.rbmodifiedView file

@@ -8759,9 +8759,13 @@
87598759 end
87608760
87618761 # Return "static inline " for short methods so gcc has permission
8762- # to inline them, or "static " otherwise. Body of ≤ 3 statements
8763- # and no yield is considered short.
8762+ # to inline them, or "static " otherwise. Body of ≤ 3 statements,
8763+ # no yield, and not self-recursive are considered inlineable.
87648764 def method_linkage(body_id, has_yield)
8765+ method_linkage_named(body_id, has_yield, "")
8766+ end
8767+
8768+ def method_linkage_named(body_id, has_yield, mname)
87658769 if has_yield == 1
87668770 return "static "
87678771 end
@@ -8769,10 +8773,61 @@
87698773 return "static inline "
87708774 end
87718775 stmts = get_stmts(body_id)
8772- if stmts.length <= 3
8773- return "static inline "
8776+ if stmts.length > 3
8777+ return "static "
8778+ end
8779+ # Avoid inlining self-recursive methods: static inline on a recursive
8780+ # function can blow up code size and hurt performance (gcc tries
8781+ # to inline harder than it should).
8782+ if mname != "" && node_calls_name?(body_id, mname) == 1
8783+ return "static "
8784+ end
8785+ "static inline "
8786+ end
8787+
8788+ # Return 1 if any CallNode in the subtree invokes mname.
8789+ def node_calls_name?(nid, mname)
8790+ if nid < 0
8791+ return 0
8792+ end
8793+ if @nd_type[nid] == "CallNode" && @nd_name[nid] == mname
8794+ return 1
8795+ end
8796+ if @nd_receiver[nid] >= 0
8797+ if node_calls_name?(@nd_receiver[nid], mname) == 1
8798+ return 1
8799+ end
8800+ end
8801+ args_id = @nd_arguments[nid]
8802+ if args_id >= 0
8803+ arr = get_args(args_id)
8804+ k = 0
8805+ while k < arr.length
8806+ if node_calls_name?(arr[k], mname) == 1
8807+ return 1
8808+ end
8809+ k = k + 1
8810+ end
8811+ end
8812+ if @nd_body[nid] >= 0
8813+ if node_calls_name?(@nd_body[nid], mname) == 1
8814+ return 1
8815+ end
87748816 end
8775- "static "
8817+ stmts = parse_id_list(@nd_stmts[nid])
8818+ k = 0
8819+ while k < stmts.length
8820+ if node_calls_name?(stmts[k], mname) == 1
8821+ return 1
8822+ end
8823+ k = k + 1
8824+ end
8825+ if @nd_subsequent[nid] >= 0
8826+ if node_calls_name?(@nd_subsequent[nid], mname) == 1
8827+ return 1
8828+ end
8829+ end
8830+ 0
87768831 end
87778832
87788833 def emit_tuple_structs
@@ -8972,7 +9027,7 @@
89729027 if @meth_has_yield[i] == 1
89739028 yp = yield_params_suffix(i)
89749029 end
8975- emit_raw(method_linkage(@meth_body_ids[i], @meth_has_yield[i]) + c_type(@meth_return_types[i]) + " sp_" + sanitize_name(@meth_names[i]) + "(" + method_params_decl(i) + yp + ");")
9030+ emit_raw(method_linkage_named(@meth_body_ids[i], @meth_has_yield[i], @meth_names[i]) + c_type(@meth_return_types[i]) + " sp_" + sanitize_name(@meth_names[i]) + "(" + method_params_decl(i) + yp + ");")
89769031 i = i + 1
89779032 end
89789033 # Class methods
@@ -9011,7 +9066,7 @@
90119066 end
90129067 bids = @cls_meth_bodies[i].split(";")
90139068 bid_j = j < bids.length ? bids[j].to_i : -1
9014- emit_raw(method_linkage(bid_j, cls_method_has_yield(i, j)) + c_type(rt) + " sp_" + cname + "_" + sanitize_name(mnames[j]) + "(sp_" + cname + sp + method_with_self_params(j, all_params, all_ptypes) + yp + ");")
9069+ emit_raw(method_linkage_named(bid_j, cls_method_has_yield(i, j), mnames[j]) + c_type(rt) + " sp_" + cname + "_" + sanitize_name(mnames[j]) + "(sp_" + cname + sp + method_with_self_params(j, all_params, all_ptypes) + yp + ");")
90159070 end
90169071 j = j + 1
90179072 end
@@ -9554,7 +9609,7 @@
95549609 if @in_yield_method == 1
95559610 yp = yield_params_suffix_cls(ci, midx)
95569611 end
9557- cm_linkage = method_linkage(bid, @in_yield_method)
9612+ cm_linkage = method_linkage_named(bid, @in_yield_method, mname)
95589613 if @cls_is_value_type[ci] == 1
95599614 emit_raw(cm_linkage + c_type(rt) + " sp_" + cname + "_" + sanitize_name(mname) + "(sp_" + cname + " self" + build_params_str(pnames, ptypes) + yp + ") {")
95609615 else
@@ -9730,11 +9785,11 @@
97309785 else
97319786 pdecl = self_ctype + " self"
97329787 end
9733- emit_raw(method_linkage(@meth_body_ids[mi], @meth_has_yield[mi]) + c_type(rt) + " sp_" + sanitize_name(mfullname) + "(" + pdecl + ") {")
9788+ emit_raw(method_linkage_named(@meth_body_ids[mi], @meth_has_yield[mi], mfullname) + c_type(rt) + " sp_" + sanitize_name(mfullname) + "(" + pdecl + ") {")
97349789 push_scope
97359790 declare_var("__self_type", oc_type)
97369791 else
9737- emit_raw(method_linkage(@meth_body_ids[mi], @meth_has_yield[mi]) + c_type(@meth_return_types[mi]) + " sp_" + sanitize_name(mfullname) + "(" + method_params_decl(mi) + yp + ") {")
9792+ emit_raw(method_linkage_named(@meth_body_ids[mi], @meth_has_yield[mi], mfullname) + c_type(@meth_return_types[mi]) + " sp_" + sanitize_name(mfullname) + "(" + method_params_decl(mi) + yp + ") {")
97389793 push_scope
97399794 end
97409795