Commit 0b192292

0b192292e54cMerged

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

Description

bench: add poly-cells exerciser to surface poly-param counts

bm_jekyll_lite landed clean at 0 poly across every category, which makes it a useless before/after target for backward-inference work. bm_poly_cells deliberately stresses the patterns that survive after analyze's current type inference:

  • Cell#set(v) / Sheet#put(v) called from 3 sites with String, Int, and Array — params widen to poly via genuine polyvariance, the same shape Active Record setters hit in real-blog.
  • @value ivar in Cell takes all three types, widens to poly.
  • render_one(v) dispatches by is_a?(String/Integer/Array). Each branch returns string-typed, but return v in the is_a?(String) arm doesn't carry the narrow to the function's return-type unification, so the function ends up poly.

Baseline SP_POLY_REPORT for measurement: params: 5 / 3 poly ivars: 5 / 1 poly returns: 9 / 2 poly locals: 4 / 0 poly

The 3 poly params are genuine polyvariance (multiple call sites with incompatible types). The 1 poly ivar is the same shape one level down. The 2 poly returns are the analyze gap that backward-inference shape additions should be able to close — return v inside is_a?(C) should narrow before contributing to the return unify.

Output is deterministic and matches CRuby. Bench suite now 54/54 pass.

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

Details

Commit
0b192292e54c
Parent
1d5291554707

benchmark/bm_poly_cells.rbaddedView file

1+# Polyvariance-rich exerciser. A `Cell.set(v)` accepts mixed-type
2+# `v` across call sites (string / int / array), so `@value` is a
3+# genuinely poly ivar and `set`'s param is a genuinely poly param.
4+# `render_one(v)` dispatches by is_a? — another poly param whose
5+# return narrows by branch but spinel currently keeps the
6+# function-level return as poly. Shape mirrors real-blog's
7+# Active Record setter pattern in ~80 lines, so SP_POLY_REPORT
8+# counts move visibly with each backward-inference refinement.
9+
10+class Cell
11+ attr_accessor :value
12+
13+ def initialize
14+ @value = ""
15+ end
16+
17+ def set(v)
18+ @value = v
19+ end
20+
21+ def get
22+ @value
23+ end
24+end
25+
26+class Sheet
27+ def initialize
28+ @cells = []
29+ @count = 0
30+ end
31+
32+ def put(v)
33+ c = Cell.new
34+ c.set(v)
35+ @cells.push(c)
36+ @count = @count + 1
37+ end
38+
39+ def count
40+ @count
41+ end
42+
43+ def cells
44+ @cells
45+ end
46+end
47+
48+def render_one(v)
49+ if v.is_a?(String)
50+ return v
51+ end
52+ if v.is_a?(Integer)
53+ return v.to_s
54+ end
55+ if v.is_a?(Array)
56+ return "[" + v.length.to_s + " items]"
57+ end
58+ "?"
59+end
60+
61+s = Sheet.new
62+s.put("hello")
63+s.put(42)
64+s.put([1, 2, 3])
65+s.put("world")
66+s.put(99)
67+
68+puts "count: " + s.count.to_s
69+
70+arr = s.cells
71+i = 0
72+while i < arr.length
73+ puts render_one(arr[i].get)
74+ i = i + 1
75+end