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
end87608760
87618761
# Return "static inline " for short methods so gcc has permission8762-
# to inline them, or "static " otherwise. Body of ≤ 3 statements8763-
# 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+
end8767+
8768+
def method_linkage_named(body_id, has_yield, mname)87658769
if has_yield == 187668770
return "static "87678771
end@@ -8769,10 +8773,61 @@
87698773
return "static inline "87708774
end87718775
stmts = get_stmts(body_id)8772-
if stmts.length <= 38773-
return "static inline "8776+
if stmts.length > 38777+
return "static "8778+
end8779+
# Avoid inlining self-recursive methods: static inline on a recursive8780+
# function can blow up code size and hurt performance (gcc tries8781+
# to inline harder than it should).8782+
if mname != "" && node_calls_name?(body_id, mname) == 18783+
return "static "8784+
end8785+
"static inline "8786+
end8787+
8788+
# Return 1 if any CallNode in the subtree invokes mname.8789+
def node_calls_name?(nid, mname)8790+
if nid < 08791+
return 08792+
end8793+
if @nd_type[nid] == "CallNode" && @nd_name[nid] == mname8794+
return 18795+
end8796+
if @nd_receiver[nid] >= 08797+
if node_calls_name?(@nd_receiver[nid], mname) == 18798+
return 18799+
end8800+
end8801+
args_id = @nd_arguments[nid]8802+
if args_id >= 08803+
arr = get_args(args_id)8804+
k = 08805+
while k < arr.length8806+
if node_calls_name?(arr[k], mname) == 18807+
return 18808+
end8809+
k = k + 18810+
end8811+
end8812+
if @nd_body[nid] >= 08813+
if node_calls_name?(@nd_body[nid], mname) == 18814+
return 18815+
end87748816
end8775-
"static "8817+
stmts = parse_id_list(@nd_stmts[nid])8818+
k = 08819+
while k < stmts.length8820+
if node_calls_name?(stmts[k], mname) == 18821+
return 18822+
end8823+
k = k + 18824+
end8825+
if @nd_subsequent[nid] >= 08826+
if node_calls_name?(@nd_subsequent[nid], mname) == 18827+
return 18828+
end8829+
end8830+
087768831
end87778832
87788833
def emit_tuple_structs@@ -8972,7 +9027,7 @@
89729027
if @meth_has_yield[i] == 189739028
yp = yield_params_suffix(i)89749029
end8975-
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 + 189779032
end89789033
# Class methods@@ -9011,7 +9066,7 @@
90119066
end90129067
bids = @cls_meth_bodies[i].split(";")90139068
bid_j = j < bids.length ? bids[j].to_i : -19014-
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
end90169071
j = j + 190179072
end@@ -9554,7 +9609,7 @@
95549609
if @in_yield_method == 195559610
yp = yield_params_suffix_cls(ci, midx)95569611
end9557-
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] == 195599614
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
else97319786
pdecl = self_ctype + " self"97329787
end9733-
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_scope97359790
declare_var("__self_type", oc_type)97369791
else9737-
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_scope97399794
end97409795