Commit b75f70ce
b75f70ce1e5cMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
perf: mark small methods as static inline
Methods with ≤ 3 statements in the body and no yield are now emitted with "static inline" linkage so gcc has permission to inline them at call sites. This gives us lightweight method inlining without needing AST-level substitution (risky for self-host).
Forward declarations and definitions use a shared method_linkage helper to stay consistent.
75/75 tests pass, 52 benchmarks pass, bootstrap stable.
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- b75f70ce1e5c
- Parent
- bd4f1a5f830a
spinel_codegen.rbmodifiedView file
@@ -8758,6 +8758,23 @@
87588758
end87598759
end87608760
8761+
# 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.8764+
def method_linkage(body_id, has_yield)8765+
if has_yield == 18766+
return "static "8767+
end8768+
if body_id < 08769+
return "static inline "8770+
end8771+
stmts = get_stmts(body_id)8772+
if stmts.length <= 38773+
return "static inline "8774+
end8775+
"static "8776+
end8777+
87618778
def emit_tuple_structs87628779
# Tuple structs are now inserted at end of generate_code87638780
end@@ -8955,7 +8972,7 @@
89558972
if @meth_has_yield[i] == 189568973
yp = yield_params_suffix(i)89578974
end8958-
emit_raw("static " + c_type(@meth_return_types[i]) + " sp_" + sanitize_name(@meth_names[i]) + "(" + method_params_decl(i) + yp + ");")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 + ");")89598976
i = i + 189608977
end89618978
# Class methods@@ -8992,7 +9009,9 @@
89929009
if @cls_is_value_type[i] == 189939010
sp = " self"89949011
end8995-
emit_raw("static " + c_type(rt) + " sp_" + cname + "_" + sanitize_name(mnames[j]) + "(sp_" + cname + sp + method_with_self_params(j, all_params, all_ptypes) + yp + ");")9012+
bids = @cls_meth_bodies[i].split(";")9013+
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 + ");")89969015
end89979016
j = j + 189989017
end@@ -9535,10 +9554,11 @@
95359554
if @in_yield_method == 195369555
yp = yield_params_suffix_cls(ci, midx)95379556
end9557+
cm_linkage = method_linkage(bid, @in_yield_method)95389558
if @cls_is_value_type[ci] == 19539-
emit_raw("static " + c_type(rt) + " sp_" + cname + "_" + sanitize_name(mname) + "(sp_" + cname + " self" + build_params_str(pnames, ptypes) + yp + ") {")9559+
emit_raw(cm_linkage + c_type(rt) + " sp_" + cname + "_" + sanitize_name(mname) + "(sp_" + cname + " self" + build_params_str(pnames, ptypes) + yp + ") {")95409560
else9541-
emit_raw("static " + c_type(rt) + " sp_" + cname + "_" + sanitize_name(mname) + "(sp_" + cname + " *self" + build_params_str(pnames, ptypes) + yp + ") {")9561+
emit_raw(cm_linkage + c_type(rt) + " sp_" + cname + "_" + sanitize_name(mname) + "(sp_" + cname + " *self" + build_params_str(pnames, ptypes) + yp + ") {")95429562
end95439563
95449564
push_scope@@ -9710,11 +9730,11 @@
97109730
else97119731
pdecl = self_ctype + " self"97129732
end9713-
emit_raw("static " + c_type(rt) + " sp_" + sanitize_name(mfullname) + "(" + pdecl + ") {")9733+
emit_raw(method_linkage(@meth_body_ids[mi], @meth_has_yield[mi]) + c_type(rt) + " sp_" + sanitize_name(mfullname) + "(" + pdecl + ") {")97149734
push_scope97159735
declare_var("__self_type", oc_type)97169736
else9717-
emit_raw("static " + c_type(@meth_return_types[mi]) + " sp_" + sanitize_name(mfullname) + "(" + method_params_decl(mi) + yp + ") {")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 + ") {")97189738
push_scope97199739
end97209740