Commit ce1d1b79

ce1d1b795e25Merged

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

Description

perf(codegen): gate class hierarchy tables on actual usage

emit_class_runtime emitted sp_class_parents, sp_class_superclass, sp_class_ancestors_off/flat, sp_class_le, sp_class_lt, sp_class_ancestors_arr, and sp_class_for_poly unconditionally once any class was declared. -O3 DCE removed the dead code from the final binary, but ~15 lines of tables + helper bodies landed in every .c regardless of whether the program ever reached for .superclass / .ancestors / dynamic is_a? / case-when on Class.

Gate the per-program emit on three pre-scan flags:

  • @needs_class_parents -- .superclass calls.
  • @needs_class_ancestors -- .ancestors / class-const comparators (<, <=, >, >= with ConstantReadNode operand) / dynamic is_a? / case-when on Class const.
  • @needs_class_for_poly -- dynamic is_a? (non-Const arg) / case-when on Class const (poly subject runs through sp_class_le(sp_class_for_poly(...))).

A new scan_for_class_hierarchy_usage walks the AST before emit_class_runtime runs (the compile-side arms set the same flags but run after the emit; the pre-scan is what makes the gate effective). Heuristics:

  • < / <= / > / >= flag @needs_class_ancestors only when an operand is ConstantReadNode / ConstantPathNode -- numeric comparators on locals don't trigger the table emit.
  • case-when with any ConstantReadNode in a WhenNode arm flags @needs_class_for_poly + @needs_class_ancestors conservatively; case-when on primitive literals doesn't trip this.

sp_class_names + sp_class_to_s + sp_class_eq stay always-emitted because sp_runtime.h's sp_poly_to_s forward-declares sp_class_to_s for its SP_TAG_CLASS arm; removing those would break the link for any program that touches sp_poly_to_s.

Concrete effect:

  • puts "hi" C output: 33 lines -> 20 lines (-39% on this file, -68% from the unoptimized 62-line shape).
  • Programs that touch any hierarchy method keep the full emission. Optcarrot uses both .superclass and dynamic is_a?, so checksum 59662 stays unchanged and fps within noise. 382 tests pass; bootstrap fixpoint OK.

Details

Commit
ce1d1b795e25
Parent
2ff1c6074851

spinel_codegen.rbmodifiedView file

@@ -6101,6 +6101,14 @@
61016101 if @needs_poly_poly_hash == 1
61026102 @needs_method = 1
61036103 end
6104+ # Same shape for the class-hierarchy tables / helpers:
6105+ # @needs_class_parents / @needs_class_ancestors /
6106+ # @needs_class_for_poly are set by compile-side arms too
6107+ # late to land before emit_class_runtime emits the gated
6108+ # sections. Pre-scan covers .superclass / .ancestors /
6109+ # dynamic is_a? / class-const comparators / case-when on
6110+ # Class subjects.
6111+ scan_for_class_hierarchy_usage(@root_id)
61046112 emit_sym_runtime
61056113 emit_ffi_externs
61066114 # Emit program-specific regexp patterns
@@ -6391,14 +6399,15 @@
63916399 emit_raw("static mrb_bool sp_class_eq(sp_Class a, sp_Class b) __attribute__((unused));")
63926400 emit_raw("static mrb_bool sp_class_eq(sp_Class a, sp_Class b){return a.cls_id == b.cls_id;}")
63936401 # Issue #404 Phase 3: parents + flat ancestors tables.
6394- # emit_class_runtime runs before compile_expr toggles the
6395- # use-site flags, so we follow Phase 1/2's policy and emit
6396- # unconditionally when any class exists. The helpers are
6397- # `__attribute__((unused))` and the tables themselves get
6398- # link-time DCE'd when nothing consumes them; the residual
6399- # cost is the array bytes, which is small for typical
6400- # programs and zero for programs that never declare a class.
6401- if unified_class_count > 0
6402+ # Gated on the pre-scan flags (scan_for_class_hierarchy_usage
6403+ # in generate_code) so programs that never reach for
6404+ # .superclass / .ancestors / dynamic is_a? / case-when-on-Class
6405+ # don't pay for the tables + helpers. Programs that do use
6406+ # them keep the full emission unchanged. The basic
6407+ # sp_class_to_s + sp_class_names + sp_class_eq above stay
6408+ # always-emitted because sp_runtime.h's sp_poly_to_s
6409+ # forward-declares sp_class_to_s for its SP_TAG_CLASS arm.
6410+ if @needs_class_parents == 1 && unified_class_count > 0
64026411 parents_line = "static const mrb_int sp_class_parents[" + unified_class_count.to_s + "] __attribute__((unused)) = {"
64036412 # Built-in parents per the design doc table.
64046413 bi_pl = 0
@@ -6440,11 +6449,11 @@
64406449 parents_line = parents_line + "};"
64416450 emit_raw(parents_line)
64426451 end
6443- if unified_class_count > 0
6452+ if @needs_class_parents == 1 && unified_class_count > 0
64446453 emit_raw("static sp_Class sp_class_superclass(sp_Class c) __attribute__((unused));")
64456454 emit_raw("static sp_Class sp_class_superclass(sp_Class c){if(c.cls_id<0||c.cls_id>=SP_CLASS_COUNT)return (sp_Class){-1};return (sp_Class){sp_class_parents[c.cls_id]};}")
64466455 end
6447- if unified_class_count > 0
6456+ if @needs_class_ancestors == 1 && unified_class_count > 0
64486457 # Compute ancestors per class at codegen time (Ruby side),
64496458 # emit as a flat array + per-class offset table. Lookup is
64506459 # `sp_class_ancestors_flat[sp_class_ancestors_off[cid] ..
@@ -6507,6 +6516,8 @@
65076516 emit_raw("static mrb_bool sp_class_lt(sp_Class child, sp_Class anc){if(child.cls_id==anc.cls_id)return FALSE;return sp_class_le(child,anc);}")
65086517 emit_raw("static sp_PolyArray *sp_class_ancestors_arr(sp_Class c) __attribute__((unused));")
65096518 emit_raw("static sp_PolyArray *sp_class_ancestors_arr(sp_Class c){sp_PolyArray*r=sp_PolyArray_new();if(c.cls_id<0||c.cls_id>=SP_CLASS_COUNT)return r;mrb_int s=sp_class_ancestors_off[c.cls_id];mrb_int e=sp_class_ancestors_off[c.cls_id+1];for(mrb_int k=s;k<e;k++){sp_Class el={sp_class_ancestors_flat[k]};sp_PolyArray_push(r,sp_box_class(el));}return r;}")
6519+ end
6520+ if @needs_class_for_poly == 1
65106521 # Tier 4: recover the cls_id of a poly value. Each primitive
65116522 # tag maps to the corresponding built-in cls_id (Integer=9,
65126523 # Float=10, String=11, Symbol=12, NilClass=5, TrueClass=6,
@@ -9225,6 +9236,97 @@
92259236 end
92269237 end
92279238
9239+ # Walk the AST looking for class-hierarchy usage patterns
9240+ # (`<klass>.superclass`, `.ancestors`, `<`, `<=`, dynamic
9241+ # is_a? / kind_of? / instance_of?, case-when on a Class const).
9242+ # Flips @needs_class_parents / @needs_class_ancestors /
9243+ # @needs_class_for_poly so emit_class_runtime can gate the
9244+ # corresponding tables + helpers. The compile-side arms set
9245+ # the same flags but run after emit_class_runtime, so this
9246+ # scan is what makes the gate effective. Programs that never
9247+ # reach for hierarchy queries trim ~15 lines of tables +
9248+ # helpers from their .c (parents/ancestors arrays + le/lt/
9249+ # ancestors_arr/for_poly bodies).
9250+ def scan_for_class_hierarchy_usage(nid)
9251+ if nid < 0
9252+ return
9253+ end
9254+ if @needs_class_parents == 1 && @needs_class_ancestors == 1 && @needs_class_for_poly == 1
9255+ return
9256+ end
9257+ t_ch = @nd_type[nid]
9258+ if t_ch == "CallNode"
9259+ mname_ch = @nd_name[nid]
9260+ if mname_ch == "superclass"
9261+ @needs_class_parents = 1
9262+ end
9263+ if mname_ch == "ancestors"
9264+ @needs_class_ancestors = 1
9265+ end
9266+ if mname_ch == "is_a?" || mname_ch == "kind_of?" || mname_ch == "instance_of?"
9267+ args_id_ch = @nd_arguments[nid]
9268+ if args_id_ch >= 0
9269+ aa_ch = get_args(args_id_ch)
9270+ if aa_ch.length > 0
9271+ arg_t_ch = @nd_type[aa_ch[0]]
9272+ # Dynamic klass (non-Const recv) routes through
9273+ # sp_class_for_poly + sp_class_le at runtime.
9274+ if arg_t_ch != "ConstantReadNode" && arg_t_ch != "ConstantPathNode"
9275+ @needs_class_for_poly = 1
9276+ @needs_class_ancestors = 1
9277+ end
9278+ end
9279+ end
9280+ end
9281+ if mname_ch == "<" || mname_ch == "<=" || mname_ch == ">" || mname_ch == ">="
9282+ # Heuristic: ConstantReadNode / ConstantPathNode operand
9283+ # suggests class compare; numeric `<` etc. on locals
9284+ # doesn't trigger the hierarchy emit.
9285+ recv_ch = @nd_receiver[nid]
9286+ if recv_ch >= 0 && (@nd_type[recv_ch] == "ConstantReadNode" || @nd_type[recv_ch] == "ConstantPathNode")
9287+ @needs_class_ancestors = 1
9288+ end
9289+ args_id_ch2 = @nd_arguments[nid]
9290+ if args_id_ch2 >= 0
9291+ aa_ch2 = get_args(args_id_ch2)
9292+ if aa_ch2.length > 0 && (@nd_type[aa_ch2[0]] == "ConstantReadNode" || @nd_type[aa_ch2[0]] == "ConstantPathNode")
9293+ @needs_class_ancestors = 1
9294+ end
9295+ end
9296+ end
9297+ end
9298+ if t_ch == "CaseNode"
9299+ # case-when with a ConstantReadNode in a WhenNode arm could
9300+ # be `case obj when SomeClass` on a poly subject -- the
9301+ # codegen path routes through sp_class_le(sp_class_for_poly).
9302+ conds_ch = parse_id_list(@nd_conditions[nid])
9303+ kch = 0
9304+ while kch < conds_ch.length
9305+ wid_ch = conds_ch[kch]
9306+ if @nd_type[wid_ch] == "WhenNode"
9307+ wconds_ch = parse_id_list(@nd_conditions[wid_ch])
9308+ wkch = 0
9309+ while wkch < wconds_ch.length
9310+ wt_ch = @nd_type[wconds_ch[wkch]]
9311+ if wt_ch == "ConstantReadNode" || wt_ch == "ConstantPathNode"
9312+ @needs_class_for_poly = 1
9313+ @needs_class_ancestors = 1
9314+ end
9315+ wkch = wkch + 1
9316+ end
9317+ end
9318+ kch = kch + 1
9319+ end
9320+ end
9321+ cs_ch = []
9322+ push_child_ids(nid, cs_ch)
9323+ k_ch = 0
9324+ while k_ch < cs_ch.length
9325+ scan_for_class_hierarchy_usage(cs_ch[k_ch])
9326+ k_ch = k_ch + 1
9327+ end
9328+ end
9329+
92289330 # Walk the AST looking for usage sites that would emit
92299331 # sp_Method-typed code. Flips @needs_method before
92309332 # emit_class_structs / emit_class_methods run, so the Method