Commit b90c30c3

b90c30c3b0d9Merged

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

Description

analyze: SP_POLY_REPORT envvar gate for poly-slot statistics

Add an opt-in counter that, after all analyze passes finish, walks every param / LV / ivar / return slot and reports how many ended up at poly (sp_RbVal) / poly_array / *_poly_hash. Gated by SP_POLY_REPORT=1 (summary only) or =2 (per-slot breakdown), so production builds stay noise-free.

Discovery measurement ran across four corpora:

Corpusparams poly%LVsivarsreturns
self-host (analyze)0/569 0%4/26880/2700/370
self-host (codegen)0/794 0%0/34430/2530/477
bench (52 files agg)0/280 0%0/6000/2080/208
optcarrot7/237 3%15/2529/2559/352
real-blog88/288 31%20/27330/5237/361

Result: self-host + bench are already optimally narrowed (the existing iterative inference pins concrete types). optcarrot has a few % poly. real-blog clusters polyvariance in Active Record patterns (Comment / Article / CommentRow / ArticleRow setters receive int / string / poly from heterogeneous callers — DB column getters, hash lookups, kwarg fallbacks).

Caller-unanimity narrowing won't help the real-blog cluster because the polyvariance is genuine (not spurious widening). The proper next step is procedure cloning or an extension of the per-(class, cmeth) scope specialization to instance methods.

The tool is harmless when unset — no allocations / I/O.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
b90c30c3b0d9
Parent
06697296bb38

spinel_analyze.rbmodifiedView file

@@ -16158,6 +16158,308 @@
1615816158 @analysis_frozen = 1
1615916159 precompute_all_scope_decls
1616016160 annotate_all_node_types
16161+ if ENV["SP_POLY_REPORT"] == "1"
16162+ emit_poly_report
16163+ end
16164+ if ENV["SP_POLY_REPORT"] == "2"
16165+ emit_poly_report
16166+ emit_poly_breakdown
16167+ end
16168+ end
16169+
16170+ # B1 discovery (STALIN.md §11): count slot types that landed on
16171+ # poly / nullable-poly / *_poly_hash / poly_array. Output to
16172+ # stderr after all analyze passes finish, gated by envvar to
16173+ # keep production builds noise-free. The breakdown drives the
16174+ # priority of STALIN.md Step 2 (param narrowing) / Step 3 (is_a?
16175+ # narrowing) — high poly counts mean polyvariance work has ROI,
16176+ # low counts mean the inference is already pinning concrete
16177+ # types and the effort is elsewhere.
16178+ def emit_poly_report
16179+ param_total = 0
16180+ param_poly = 0
16181+ param_poly_array = 0
16182+ param_poly_hash = 0
16183+ param_nullable_poly = 0
16184+ lv_total = 0
16185+ lv_poly = 0
16186+ lv_poly_array = 0
16187+ lv_poly_hash = 0
16188+ lv_nullable_poly = 0
16189+ ivar_total = 0
16190+ ivar_poly = 0
16191+ ivar_poly_array = 0
16192+ ivar_poly_hash = 0
16193+ ret_total = 0
16194+ ret_poly = 0
16195+
16196+ # Top-level method params + returns.
16197+ rmi = 0
16198+ while rmi < @meth_names.length
16199+ rpt = @meth_param_types[rmi].split(",")
16200+ rpi = 0
16201+ while rpi < rpt.length
16202+ if rpt[rpi] != ""
16203+ param_total = param_total + 1
16204+ rb = base_type(rpt[rpi])
16205+ if rb == "poly"
16206+ param_poly = param_poly + 1
16207+ if rpt[rpi].end_with?("?")
16208+ param_nullable_poly = param_nullable_poly + 1
16209+ end
16210+ elsif rb == "poly_array"
16211+ param_poly_array = param_poly_array + 1
16212+ elsif rpt[rpi].include?("poly_hash")
16213+ param_poly_hash = param_poly_hash + 1
16214+ end
16215+ end
16216+ rpi = rpi + 1
16217+ end
16218+ ret_total = ret_total + 1
16219+ if rmi < @meth_return_types.length
16220+ rb_ret = base_type(@meth_return_types[rmi])
16221+ if rb_ret == "poly"
16222+ ret_poly = ret_poly + 1
16223+ end
16224+ end
16225+ rmi = rmi + 1
16226+ end
16227+
16228+ # Class instance method + cmeth params + returns.
16229+ rci = 0
16230+ while rci < @cls_names.length
16231+ r_im_pall = @cls_meth_ptypes[rci].split("|")
16232+ r_im_ret = @cls_meth_returns[rci].split(";")
16233+ r_imj = 0
16234+ while r_imj < r_im_pall.length
16235+ r_im_pt = r_im_pall[r_imj].split(",")
16236+ r_imk = 0
16237+ while r_imk < r_im_pt.length
16238+ if r_im_pt[r_imk] != ""
16239+ param_total = param_total + 1
16240+ r_imb = base_type(r_im_pt[r_imk])
16241+ if r_imb == "poly"
16242+ param_poly = param_poly + 1
16243+ if r_im_pt[r_imk].end_with?("?")
16244+ param_nullable_poly = param_nullable_poly + 1
16245+ end
16246+ elsif r_imb == "poly_array"
16247+ param_poly_array = param_poly_array + 1
16248+ elsif r_im_pt[r_imk].include?("poly_hash")
16249+ param_poly_hash = param_poly_hash + 1
16250+ end
16251+ end
16252+ r_imk = r_imk + 1
16253+ end
16254+ r_imj = r_imj + 1
16255+ end
16256+ r_imrj = 0
16257+ while r_imrj < r_im_ret.length
16258+ if r_im_ret[r_imrj] != ""
16259+ ret_total = ret_total + 1
16260+ r_imrb = base_type(r_im_ret[r_imrj])
16261+ if r_imrb == "poly"
16262+ ret_poly = ret_poly + 1
16263+ end
16264+ end
16265+ r_imrj = r_imrj + 1
16266+ end
16267+ r_cmpall = @cls_cmeth_ptypes[rci].split("|")
16268+ r_cmret = @cls_cmeth_returns[rci].split(";")
16269+ r_cmj = 0
16270+ while r_cmj < r_cmpall.length
16271+ r_cmpt = r_cmpall[r_cmj].split(",")
16272+ r_cmk = 0
16273+ while r_cmk < r_cmpt.length
16274+ if r_cmpt[r_cmk] != ""
16275+ param_total = param_total + 1
16276+ r_cmb = base_type(r_cmpt[r_cmk])
16277+ if r_cmb == "poly"
16278+ param_poly = param_poly + 1
16279+ if r_cmpt[r_cmk].end_with?("?")
16280+ param_nullable_poly = param_nullable_poly + 1
16281+ end
16282+ elsif r_cmb == "poly_array"
16283+ param_poly_array = param_poly_array + 1
16284+ elsif r_cmpt[r_cmk].include?("poly_hash")
16285+ param_poly_hash = param_poly_hash + 1
16286+ end
16287+ end
16288+ r_cmk = r_cmk + 1
16289+ end
16290+ r_cmj = r_cmj + 1
16291+ end
16292+ r_cmrj = 0
16293+ while r_cmrj < r_cmret.length
16294+ if r_cmret[r_cmrj] != ""
16295+ ret_total = ret_total + 1
16296+ r_cmrb = base_type(r_cmret[r_cmrj])
16297+ if r_cmrb == "poly"
16298+ ret_poly = ret_poly + 1
16299+ end
16300+ end
16301+ r_cmrj = r_cmrj + 1
16302+ end
16303+ # ivars
16304+ r_ivt = @cls_ivar_types[rci].split(";")
16305+ r_ivj = 0
16306+ while r_ivj < r_ivt.length
16307+ if r_ivt[r_ivj] != ""
16308+ ivar_total = ivar_total + 1
16309+ r_ivb = base_type(r_ivt[r_ivj])
16310+ if r_ivb == "poly"
16311+ ivar_poly = ivar_poly + 1
16312+ elsif r_ivb == "poly_array"
16313+ ivar_poly_array = ivar_poly_array + 1
16314+ elsif r_ivt[r_ivj].include?("poly_hash")
16315+ ivar_poly_hash = ivar_poly_hash + 1
16316+ end
16317+ end
16318+ r_ivj = r_ivj + 1
16319+ end
16320+ rci = rci + 1
16321+ end
16322+
16323+ # LVs from @nd_scope_types — covers all bodies (top-level,
16324+ # instance methods, cmeths, main).
16325+ r_nd = 0
16326+ while r_nd < @nd_scope_types.length
16327+ r_st = @nd_scope_types[r_nd]
16328+ if r_st != ""
16329+ r_pieces = r_st.split("|")
16330+ r_pp = 0
16331+ while r_pp < r_pieces.length
16332+ if r_pieces[r_pp] != ""
16333+ lv_total = lv_total + 1
16334+ r_lb = base_type(r_pieces[r_pp])
16335+ if r_lb == "poly"
16336+ lv_poly = lv_poly + 1
16337+ if r_pieces[r_pp].end_with?("?")
16338+ lv_nullable_poly = lv_nullable_poly + 1
16339+ end
16340+ elsif r_lb == "poly_array"
16341+ lv_poly_array = lv_poly_array + 1
16342+ elsif r_pieces[r_pp].include?("poly_hash")
16343+ lv_poly_hash = lv_poly_hash + 1
16344+ end
16345+ end
16346+ r_pp = r_pp + 1
16347+ end
16348+ end
16349+ r_nd = r_nd + 1
16350+ end
16351+
16352+ $stderr.puts "=== SP_POLY_REPORT ==="
16353+ $stderr.puts "params: total=" + param_total.to_s + " poly=" + param_poly.to_s + " (nullable=" + param_nullable_poly.to_s + ") poly_array=" + param_poly_array.to_s + " poly_hash=" + param_poly_hash.to_s
16354+ $stderr.puts "locals: total=" + lv_total.to_s + " poly=" + lv_poly.to_s + " (nullable=" + lv_nullable_poly.to_s + ") poly_array=" + lv_poly_array.to_s + " poly_hash=" + lv_poly_hash.to_s
16355+ $stderr.puts "ivars: total=" + ivar_total.to_s + " poly=" + ivar_poly.to_s + " poly_array=" + ivar_poly_array.to_s + " poly_hash=" + ivar_poly_hash.to_s
16356+ $stderr.puts "returns: total=" + ret_total.to_s + " poly=" + ret_poly.to_s
16357+ $stderr.puts "======================"
16358+ end
16359+
16360+ # Detailed breakdown (SP_POLY_REPORT=2). Dump location names
16361+ # of every poly slot so the operator can see which classes /
16362+ # methods cluster the poly. Used to scope STALIN.md Step 2
16363+ # (param narrowing) — high concentration in a few methods
16364+ # means a targeted narrowing pass yields the most.
16365+ def emit_poly_breakdown
16366+ $stderr.puts "--- BREAKDOWN ---"
16367+ # Top-level + module class methods params.
16368+ bmi = 0
16369+ while bmi < @meth_names.length
16370+ bpn = @meth_param_names[bmi].split(",")
16371+ bpt = @meth_param_types[bmi].split(",")
16372+ bj = 0
16373+ while bj < bpn.length
16374+ if bj < bpt.length
16375+ bb = base_type(bpt[bj])
16376+ if bb == "poly" || bb == "poly_array" || bpt[bj].include?("poly_hash")
16377+ $stderr.puts "param: " + @meth_names[bmi] + "(" + bpn[bj] + ") : " + bpt[bj]
16378+ end
16379+ end
16380+ bj = bj + 1
16381+ end
16382+ if bmi < @meth_return_types.length
16383+ b_rb = base_type(@meth_return_types[bmi])
16384+ if b_rb == "poly"
16385+ $stderr.puts "return: " + @meth_names[bmi] + " : " + @meth_return_types[bmi]
16386+ end
16387+ end
16388+ bmi = bmi + 1
16389+ end
16390+ # Class instance methods + cmeths params.
16391+ bci = 0
16392+ while bci < @cls_names.length
16393+ bcn = @cls_names[bci]
16394+ b_im_pn = @cls_meth_params[bci].split("|")
16395+ b_im_pt = @cls_meth_ptypes[bci].split("|")
16396+ b_im_mn = @cls_meth_names[bci].split(";")
16397+ b_im_rn = @cls_meth_returns[bci].split(";")
16398+ b_imj = 0
16399+ while b_imj < b_im_pn.length
16400+ b_im_n = b_im_pn[b_imj].split(",")
16401+ b_im_t = b_im_pt[b_imj].split(",")
16402+ b_im_mname = b_imj < b_im_mn.length ? b_im_mn[b_imj] : "?"
16403+ b_imk = 0
16404+ while b_imk < b_im_n.length
16405+ if b_imk < b_im_t.length
16406+ b_im_b = base_type(b_im_t[b_imk])
16407+ if b_im_b == "poly" || b_im_b == "poly_array" || b_im_t[b_imk].include?("poly_hash")
16408+ $stderr.puts "param: " + bcn + "#" + b_im_mname + "(" + b_im_n[b_imk] + ") : " + b_im_t[b_imk]
16409+ end
16410+ end
16411+ b_imk = b_imk + 1
16412+ end
16413+ if b_imj < b_im_rn.length && b_im_rn[b_imj] != ""
16414+ b_im_rb = base_type(b_im_rn[b_imj])
16415+ if b_im_rb == "poly"
16416+ $stderr.puts "return: " + bcn + "#" + b_im_mname + " : " + b_im_rn[b_imj]
16417+ end
16418+ end
16419+ b_imj = b_imj + 1
16420+ end
16421+ b_cm_pn = @cls_cmeth_params[bci].split("|")
16422+ b_cm_pt = @cls_cmeth_ptypes[bci].split("|")
16423+ b_cm_mn = @cls_cmeth_names[bci].split(";")
16424+ b_cm_rn = @cls_cmeth_returns[bci].split(";")
16425+ b_cmj = 0
16426+ while b_cmj < b_cm_pn.length
16427+ b_cm_n = b_cm_pn[b_cmj].split(",")
16428+ b_cm_t = b_cm_pt[b_cmj].split(",")
16429+ b_cm_mname = b_cmj < b_cm_mn.length ? b_cm_mn[b_cmj] : "?"
16430+ b_cmk = 0
16431+ while b_cmk < b_cm_n.length
16432+ if b_cmk < b_cm_t.length
16433+ b_cm_b = base_type(b_cm_t[b_cmk])
16434+ if b_cm_b == "poly" || b_cm_b == "poly_array" || b_cm_t[b_cmk].include?("poly_hash")
16435+ $stderr.puts "param: " + bcn + "." + b_cm_mname + "(" + b_cm_n[b_cmk] + ") : " + b_cm_t[b_cmk]
16436+ end
16437+ end
16438+ b_cmk = b_cmk + 1
16439+ end
16440+ if b_cmj < b_cm_rn.length && b_cm_rn[b_cmj] != ""
16441+ b_cm_rb = base_type(b_cm_rn[b_cmj])
16442+ if b_cm_rb == "poly"
16443+ $stderr.puts "return: " + bcn + "." + b_cm_mname + " : " + b_cm_rn[b_cmj]
16444+ end
16445+ end
16446+ b_cmj = b_cmj + 1
16447+ end
16448+ b_ivn = @cls_ivar_names[bci].split(";")
16449+ b_ivt = @cls_ivar_types[bci].split(";")
16450+ b_ivj = 0
16451+ while b_ivj < b_ivn.length
16452+ if b_ivj < b_ivt.length
16453+ b_iv_b = base_type(b_ivt[b_ivj])
16454+ if b_iv_b == "poly" || b_iv_b == "poly_array" || b_ivt[b_ivj].include?("poly_hash")
16455+ $stderr.puts "ivar: " + bcn + "." + b_ivn[b_ivj] + " : " + b_ivt[b_ivj]
16456+ end
16457+ end
16458+ b_ivj = b_ivj + 1
16459+ end
16460+ bci = bci + 1
16461+ end
16462+ $stderr.puts "-----------------"
1616116463 end
1616216464
1616316465