Commit 345901d6

345901d6deeaMerged

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

Description

perf(codegen): gate sp_sym_array_sort + sp_sym_sort_cmp on .sort usage

emit_sym_runtime emitted sp_sym_array_sort + sp_sym_sort_cmp (4 lines of C) inside the @sym_names guard, so any program that uses any symbol got the sort helpers even when nothing called <sym_array>.sort. The user's puts :hello example landed both helpers despite never invoking sort.

Gate the emit on a new @needs_sym_sort flag, flipped by scan_for_class_hierarchy_usage when it sees a .sort or .sort! CallNode. Conservative -- false positives (.sort on int / str / float arrays) emit the dead helpers, but -O2 DCE removes them when sym_array_sort is uncalled. Missing the trigger would break the link, so the over-emit is the safer side.

Effect: puts :hello C output 22 -> 18 lines (-18%). 382 tests pass, bootstrap fixpoint OK, optcarrot checksum 59662 unchanged.

Details

Commit
345901d6deea
Parent
a512de6fd72b

spinel_codegen.rbmodifiedView file

@@ -433,6 +433,13 @@
433433 # build. Flip this when an actual usage site fires; emit
434434 # the Method struct + helpers only when it's set.
435435 @needs_method = 0
436+ # sp_sym_array_sort + sp_sym_sort_cmp are emitted inside the
437+ # @sym_names guard in emit_sym_runtime; a program that uses
438+ # any symbol gets the sort helpers even when nothing calls
439+ # `<sym_array>.sort`. Pre-scan flips this when a `.sort` /
440+ # `.sort!` CallNode appears so the helpers stay out of
441+ # programs that don't sort.
442+ @needs_sym_sort = 0
436443 @regexp_patterns = "".split(",")
437444 @regexp_flags = "".split(",")
438445 # Dynamic-regex (InterpolatedRegularExpressionNode) call-site cache.
@@ -6766,10 +6773,12 @@
67666773 i = i + 1
67676774 end
67686775 # Sort comparator for sym arrays: lexical by symbol name.
6769- emit_raw("static int sp_sym_sort_cmp(const void*a,const void*b) __attribute__((unused));")
6770- emit_raw("static int sp_sym_sort_cmp(const void*a,const void*b){return strcmp(sp_sym_to_s(*(const sp_sym*)a),sp_sym_to_s(*(const sp_sym*)b));}")
6771- emit_raw("static void sp_sym_array_sort(sp_IntArray*a) __attribute__((unused));")
6772- emit_raw("static void sp_sym_array_sort(sp_IntArray*a){qsort(a->data+a->start,a->len,sizeof(mrb_int),sp_sym_sort_cmp);}")
6776+ if @needs_sym_sort == 1
6777+ emit_raw("static int sp_sym_sort_cmp(const void*a,const void*b) __attribute__((unused));")
6778+ emit_raw("static int sp_sym_sort_cmp(const void*a,const void*b){return strcmp(sp_sym_to_s(*(const sp_sym*)a),sp_sym_to_s(*(const sp_sym*)b));}")
6779+ emit_raw("static void sp_sym_array_sort(sp_IntArray*a) __attribute__((unused));")
6780+ emit_raw("static void sp_sym_array_sort(sp_IntArray*a){qsort(a->data+a->start,a->len,sizeof(mrb_int),sp_sym_sort_cmp);}")
6781+ end
67736782 else
67746783 # No symbols used at all -- emit a 1-line stub.
67756784 # sp_runtime.h's poly_to_s / poly_puts / poly_inspect /
@@ -9298,6 +9307,15 @@
92989307 # chain that typically follows.
92999308 @needs_class_table = 1
93009309 end
9310+ if mname_ch == "sort" || mname_ch == "sort!"
9311+ # `.sort` / `.sort!` on a sym_array compiles via
9312+ # sp_sym_array_sort + sp_sym_sort_cmp. Pre-scan flips
9313+ # @needs_sym_sort conservatively on any .sort / .sort!
9314+ # call -- false positives (sort on int / str / float
9315+ # arrays) cost a 4-line dead helper that -O2 DCE
9316+ # removes; missing the trigger breaks the link.
9317+ @needs_sym_sort = 1
9318+ end
93019319 if mname_ch == "to_s" || mname_ch == "name" || mname_ch == "inspect"
93029320 # Static recv check is too brittle pre-inference; flag
93039321 # @needs_class_table conservatively when these mnames