Commit 6b098f97

6b098f972cd6Merged

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

Description

bench: add Sinatra-mini router + handler dispatch

Fourth bench in the corpus, shape distinct from the existing three: bm_jekyll_lite — string-heavy pipeline (0 poly) bm_poly_cells — synthetic polyvariance exerciser bm_micro_lisp — saturated polyvariance (Lisp eval) bm_sinatra_mini — HTTP-style router + handler dispatch

Exercises:

  • per-route handler functions returning Response objects
  • params hash extraction (req.params["name"] / .to_i coerce)
  • Request / Response domain objects with attr_accessor fields
  • mutable Router state (logging visited paths)
  • if-chain routing (Sinatra's regex/glob routing isn't in Spinel's subset; explicit if path == "..." is the workable shape)

Baseline SP_POLY_REPORT lands at 0 poly across every category — the shape is cleanly handled today. Useful as a regression checkpoint for future inference changes and as a coverage target for the router/handler dispatch pattern. CRuby diff is exact across 6 request/response pairs.

One implementation note recorded inline: def log; @log; end collides with infer_call_type's Math.log special case (returns float regardless of recv kind), miscompiling the response-side router.log[i] read as float-index. Renamed to entries to sidestep; the underlying bug is filed for future cleanup.

55 benchmarks before, 57 after (bm_sinatra_mini + the bm_micro_lisp -> bm_jekyll_lite rounding I missed last time makes 57). 4-way bootstrap fixpoint OK.

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

Details

Commit
6b098f972cd6
Parent
eabafb833016

benchmark/bm_sinatra_mini.rbaddedView file

1+# Sinatra-mini: tiny HTTP-style router + handler dispatch. Adds a
2+# bench shape distinct from the existing three:
3+# bm_jekyll_lite — string-heavy pipeline, 0 poly
4+# bm_poly_cells — synthetic polyvariance exerciser
5+# bm_micro_lisp — saturated polyvariance (Lisp eval)
6+#
7+# This one exercises: routing table (hash by path), per-route
8+# handler with different params shapes, params extraction from a
9+# query-string-shaped hash, response body construction. Shape is
10+# closer to a real web framework's hot path than the others, and
11+# the request/response cycle composition is a frequent source of
12+# poly-param widening in real codebases.
13+#
14+# Routes are explicit `if path == "..."` matches (Sinatra's
15+# regex/glob routing would require runtime pattern matching that's
16+# outside Spinel's subset). Handlers take the params hash and
17+# return a response body string.
18+
19+class Request
20+ attr_accessor :method_name
21+ attr_accessor :path
22+ attr_accessor :params
23+ def initialize(method_name, path, params)
24+ @method_name = method_name
25+ @path = path
26+ @params = params
27+ end
28+end
29+
30+class Response
31+ attr_accessor :status
32+ attr_accessor :body
33+ def initialize(status, body)
34+ @status = status
35+ @body = body
36+ end
37+end
38+
39+def handle_index(req)
40+ Response.new(200, "Hello, world")
41+end
42+
43+def handle_greet(req)
44+ name = req.params["name"]
45+ if name == ""
46+ name = "anonymous"
47+ end
48+ Response.new(200, "Hello, " + name)
49+end
50+
51+def handle_echo(req)
52+ msg = req.params["msg"]
53+ count_str = req.params["count"]
54+ count = count_str.to_i
55+ if count <= 0
56+ count = 1
57+ end
58+ out = ""
59+ i = 0
60+ while i < count
61+ if i > 0
62+ out = out + " "
63+ end
64+ out = out + msg
65+ i = i + 1
66+ end
67+ Response.new(200, out)
68+end
69+
70+def handle_not_found(req)
71+ Response.new(404, "Not Found: " + req.path)
72+end
73+
74+class Router
75+ def initialize
76+ @log = []
77+ end
78+
79+ def dispatch(req)
80+ @log.push(req.method_name + " " + req.path)
81+ if req.path == "/"
82+ return handle_index(req)
83+ end
84+ if req.path == "/greet"
85+ return handle_greet(req)
86+ end
87+ if req.path == "/echo"
88+ return handle_echo(req)
89+ end
90+ handle_not_found(req)
91+ end
92+
93+ def entries
94+ @log
95+ end
96+end
97+
98+def render(res)
99+ res.status.to_s + " " + res.body
100+end
101+
102+router = Router.new
103+
104+reqs = [
105+ Request.new("GET", "/", { "name" => "" }),
106+ Request.new("GET", "/greet", { "name" => "Matz" }),
107+ Request.new("GET", "/greet", { "name" => "" }),
108+ Request.new("GET", "/echo", { "msg" => "hi", "count" => "3" }),
109+ Request.new("GET", "/echo", { "msg" => "x", "count" => "0" }),
110+ Request.new("GET", "/missing", { "name" => "" })
111+]
112+
113+i = 0
114+while i < reqs.length
115+ res = router.dispatch(reqs[i])
116+ puts render(res)
117+ i = i + 1
118+end
119+
120+puts "---log---"
121+entries = router.entries
122+j = 0
123+while j < entries.length
124+ puts entries[j]
125+ j = j + 1
126+end