Commit 40a82ad9

40a82ad99bceMerged

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

Description

bench: add Micro Lisp interpreter

A genuinely polyvariance-rich benchmark, distinct from bm_poly_cells in shape and from bm_jekyll_lite in poly counts. Lisp values are separate classes (IntV / SymV / PairV / LambdaV); a recursive eval_form returns each of them at different call sites, so its return widens to poly. Pair head / tail and env values store any kind, so those ivars are poly too. Test program is (define fact (lambda (n) (if (< n 2) 1 (* n (fact (- n 1)))))) (fact 6) evaluating to 720 — one line of stdout for trivial bench-harness diff against CRuby.

Baseline SP_POLY_REPORT (a measurement target for future backward-inference work): params: 26 / 8 poly (31%, matches real-blog) locals: 31 / 12 poly (39%) ivars: 16 / 3 poly + 1 poly_array returns: 20 / 5 poly

Two design notes recorded inline:

  1. attr_accessor (vs attr_reader) on every value class. spinel's value-type detection otherwise turns IntV / SymV into in-register structs that can't be stored uniformly in a poly array — sp_box_obj rejects the struct value at the call site.

  2. LambdaV stores the lambda param list as the original PairV form instead of as a precomputed str_array. The str_array approach widened to poly_array via a chain that probably marks future backward-inference work: collect_param_names' element type inferred poly because the is_a?(SymV) narrow on cur.head doesn't reach the .s attr-read at the push site; @params then became poly_array, pnames[i] in bind dispatched polyly, and Env.define's n widened to poly even though both call sites pass strings. Walking the PairV in lockstep with args keeps the local read of hh.s under a tight enough is_a?(SymV) narrow that does resolve to string.

Output is deterministic and matches CRuby. 55/55 benchmarks pass.

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

Details

Commit
40a82ad99bce
Parent
0b192292e54c

benchmark/bm_micro_lisp.rbaddedView file

1+# Micro Lisp: tokens -> ast -> eval over a Scheme-shaped subset
2+# (numbers, symbols, pairs, lambdas, define, if, recursive calls).
3+# Polymorphism-rich by construction: every value kind is its own
4+# class (IntV / SymV / PairV / LambdaV), and the recursive walker
5+# eval(form, env) returns each of them at different call sites, so
6+# eval's return widens to poly. Pair head/tail and env values store
7+# any kind, so those ivars are poly too. Each is_a? dispatch is a
8+# place where backward-inference (narrowing the return inside a
9+# branch where the recv is known) could in principle pin a tighter
10+# type.
11+#
12+# Test program is `factorial 6` -> 720. Output is one line so the
13+# bench harness diffs trivially against CRuby.
14+#
15+# attr_accessor (not attr_reader) on every value class is
16+# deliberate: spinel's value-type detection otherwise turns
17+# IntV / SymV into in-register structs that can't be stored
18+# uniformly in a poly array.
19+
20+class IntV
21+ attr_accessor :n
22+ def initialize(n); @n = n; end
23+end
24+
25+class SymV
26+ attr_accessor :s
27+ def initialize(s); @s = s; end
28+end
29+
30+class PairV
31+ attr_accessor :head
32+ attr_accessor :tail
33+ def initialize(h, t); @head = h; @tail = t; end
34+end
35+
36+class LambdaV
37+ # Store the param list as the original PairV form rather than as
38+ # a precomputed str_array. The str_array approach widened to poly
39+ # via a chain: collect_param_names' element type infers as poly
40+ # because the is_a?(SymV) narrow on cur.head doesn't reach the .s
41+ # at the push site, so @params becomes poly_array, pnames[i] in
42+ # bind dispatches polyly, and Env.define's `n` widens to poly.
43+ # Walking the PairV directly keeps the local read of h.s under a
44+ # tight is_a?(SymV) narrow that does resolve to string.
45+ attr_accessor :params_form
46+ attr_accessor :body
47+ attr_accessor :env
48+ def initialize(p, b, e); @params_form = p; @body = b; @env = e; end
49+
50+ def bind(args)
51+ new_env = Env.new(@env)
52+ p_cur = @params_form
53+ a_cur = args
54+ while p_cur != nil && p_cur.is_a?(PairV) && a_cur != nil && a_cur.is_a?(PairV)
55+ hh = p_cur.head
56+ if hh.is_a?(SymV)
57+ new_env.define(hh.s, a_cur.head)
58+ end
59+ p_cur = p_cur.tail
60+ a_cur = a_cur.tail
61+ end
62+ new_env
63+ end
64+end
65+
66+class Env
67+ attr_accessor :parent
68+ def initialize(p)
69+ @parent = p
70+ @names = []
71+ @vals = []
72+ end
73+ def define(n, v)
74+ @names.push(n)
75+ @vals.push(v)
76+ end
77+ def lookup(n)
78+ i = 0
79+ while i < @names.length
80+ if @names[i] == n
81+ return @vals[i]
82+ end
83+ i = i + 1
84+ end
85+ if @parent != nil
86+ return @parent.lookup(n)
87+ end
88+ nil
89+ end
90+end
91+
92+class Reader
93+ # Tokenizes + parses in one class so the tokens array stays an
94+ # ivar of a single type. Splitting Tokenizer and Parser widened
95+ # Parser#@toks to int_array because constructor-arg inference
96+ # didn't carry the str_array shape across the new (toks) call.
97+ def initialize(src)
98+ @src = src
99+ @sp = 0
100+ @toks = []
101+ @tp = 0
102+ end
103+ def tokenize
104+ while @sp < @src.length
105+ c = @src[@sp]
106+ if c == " " || c == "\n" || c == "\t"
107+ @sp = @sp + 1
108+ elsif c == "("
109+ @toks.push("(")
110+ @sp = @sp + 1
111+ elsif c == ")"
112+ @toks.push(")")
113+ @sp = @sp + 1
114+ elsif c >= "0" && c <= "9"
115+ s = @sp
116+ while @sp < @src.length && @src[@sp] >= "0" && @src[@sp] <= "9"
117+ @sp = @sp + 1
118+ end
119+ @toks.push(@src[s, @sp - s])
120+ else
121+ s = @sp
122+ while @sp < @src.length && @src[@sp] != " " && @src[@sp] != "\n" && @src[@sp] != "\t" && @src[@sp] != "(" && @src[@sp] != ")"
123+ @sp = @sp + 1
124+ end
125+ @toks.push(@src[s, @sp - s])
126+ end
127+ end
128+ end
129+ def more?
130+ @tp < @toks.length
131+ end
132+ def parse
133+ t = @toks[@tp]
134+ @tp = @tp + 1
135+ if t == "("
136+ return parse_list
137+ end
138+ if t[0] >= "0" && t[0] <= "9"
139+ return IntV.new(t.to_i)
140+ end
141+ SymV.new(t)
142+ end
143+ def parse_list
144+ if @toks[@tp] == ")"
145+ @tp = @tp + 1
146+ return nil
147+ end
148+ h = parse
149+ t = parse_list
150+ PairV.new(h, t)
151+ end
152+end
153+
154+def is_truthy(v)
155+ if v == nil
156+ return false
157+ end
158+ true
159+end
160+
161+def int_of(v)
162+ if v.is_a?(IntV)
163+ return v.n
164+ end
165+ 0
166+end
167+
168+def list_get(lst, i)
169+ cur = lst
170+ k = 0
171+ while cur != nil && cur.is_a?(PairV) && k < i
172+ cur = cur.tail
173+ k = k + 1
174+ end
175+ if cur != nil && cur.is_a?(PairV)
176+ return cur.head
177+ end
178+ nil
179+end
180+
181+def eval_form(form, env)
182+ if form == nil
183+ return nil
184+ end
185+ if form.is_a?(IntV)
186+ return form
187+ end
188+ if form.is_a?(SymV)
189+ return env.lookup(form.s)
190+ end
191+ if form.is_a?(PairV)
192+ h = form.head
193+ if h.is_a?(SymV)
194+ n = h.s
195+ if n == "quote"
196+ return form.tail.head
197+ end
198+ if n == "if"
199+ cond = eval_form(list_get(form, 1), env)
200+ if is_truthy(cond)
201+ return eval_form(list_get(form, 2), env)
202+ end
203+ return eval_form(list_get(form, 3), env)
204+ end
205+ if n == "define"
206+ name_sym = list_get(form, 1)
207+ val = eval_form(list_get(form, 2), env)
208+ # Hoist .s into a typed local. Without this hoist, spinel widens
209+ # Env.define's `n` param to poly because the is_a?(SymV) narrow
210+ # on name_sym doesn't propagate through the .s read at the call-
211+ # site arg position — name_sym.s on a poly recv reads as poly.
212+ if name_sym.is_a?(SymV)
213+ name_str = name_sym.s
214+ env.define(name_str, val)
215+ end
216+ return nil
217+ end
218+ if n == "lambda"
219+ params_form = list_get(form, 1)
220+ body = list_get(form, 2)
221+ return LambdaV.new(params_form, body, env)
222+ end
223+ end
224+ return apply_form(h, form.tail, env)
225+ end
226+ form
227+end
228+
229+def eval_args(args_form, env)
230+ if args_form == nil
231+ return nil
232+ end
233+ if args_form.is_a?(PairV)
234+ hv = eval_form(args_form.head, env)
235+ tv = eval_args(args_form.tail, env)
236+ return PairV.new(hv, tv)
237+ end
238+ nil
239+end
240+
241+def apply_form(head_form, args_form, env)
242+ evald_args = eval_args(args_form, env)
243+ if head_form.is_a?(SymV)
244+ bn = head_form.s
245+ if bn == "+"
246+ return IntV.new(int_of(list_get(evald_args, 0)) + int_of(list_get(evald_args, 1)))
247+ end
248+ if bn == "-"
249+ return IntV.new(int_of(list_get(evald_args, 0)) - int_of(list_get(evald_args, 1)))
250+ end
251+ if bn == "*"
252+ return IntV.new(int_of(list_get(evald_args, 0)) * int_of(list_get(evald_args, 1)))
253+ end
254+ if bn == "<"
255+ a = int_of(list_get(evald_args, 0))
256+ b = int_of(list_get(evald_args, 1))
257+ if a < b
258+ return IntV.new(1)
259+ end
260+ return nil
261+ end
262+ end
263+ fn = eval_form(head_form, env)
264+ if fn.is_a?(LambdaV)
265+ new_env = fn.bind(evald_args)
266+ return eval_form(fn.body, new_env)
267+ end
268+ nil
269+end
270+
271+SRC = "(define fact (lambda (n) (if (< n 2) 1 (* n (fact (- n 1)))))) (fact 6)"
272+
273+reader = Reader.new(SRC)
274+reader.tokenize
275+env = Env.new(nil)
276+result = nil
277+while reader.more?
278+ result = eval_form(reader.parse, env)
279+end
280+if result.is_a?(IntV)
281+ puts result.n
282+end