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 end
87598759 end
87608760
8761+ # 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.
8764+ def method_linkage(body_id, has_yield)
8765+ if has_yield == 1
8766+ return "static "
8767+ end
8768+ if body_id < 0
8769+ return "static inline "
8770+ end
8771+ stmts = get_stmts(body_id)
8772+ if stmts.length <= 3
8773+ return "static inline "
8774+ end
8775+ "static "
8776+ end
8777+
87618778 def emit_tuple_structs
87628779 # Tuple structs are now inserted at end of generate_code
87638780 end
@@ -8955,7 +8972,7 @@
89558972 if @meth_has_yield[i] == 1
89568973 yp = yield_params_suffix(i)
89578974 end
8958- 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 + 1
89608977 end
89618978 # Class methods
@@ -8992,7 +9009,9 @@
89929009 if @cls_is_value_type[i] == 1
89939010 sp = " self"
89949011 end
8995- 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 : -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 + ");")
89969015 end
89979016 j = j + 1
89989017 end
@@ -9535,10 +9554,11 @@
95359554 if @in_yield_method == 1
95369555 yp = yield_params_suffix_cls(ci, midx)
95379556 end
9557+ cm_linkage = method_linkage(bid, @in_yield_method)
95389558 if @cls_is_value_type[ci] == 1
9539- 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 else
9541- 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 end
95439563
95449564 push_scope
@@ -9710,11 +9730,11 @@
97109730 else
97119731 pdecl = self_ctype + " self"
97129732 end
9713- 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_scope
97159735 declare_var("__self_type", oc_type)
97169736 else
9717- 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_scope
97199739 end
97209740