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_icoerce) - 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
# Sinatra-mini: tiny HTTP-style router + handler dispatch. Adds a# bench 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)## This one exercises: routing table (hash by path), per-route# handler with different params shapes, params extraction from a# query-string-shaped hash, response body construction. Shape is# closer to a real web framework's hot path than the others, and# the request/response cycle composition is a frequent source of# poly-param widening in real codebases.## Routes are explicit `if path == "..."` matches (Sinatra's# regex/glob routing would require runtime pattern matching that's# outside Spinel's subset). Handlers take the params hash and# return a response body string.class Request attr_accessor :method_name attr_accessor :path attr_accessor :params def initialize(method_name, path, params) @method_name = method_name @path = path @params = params endendclass Response attr_accessor :status attr_accessor :body def initialize(status, body) @status = status @body = body endenddef handle_index(req) Response.new(200, "Hello, world")enddef handle_greet(req) name = req.params["name"] if name == "" name = "anonymous" end Response.new(200, "Hello, " + name)enddef handle_echo(req) msg = req.params["msg"] count_str = req.params["count"] count = count_str.to_i if count <= 0 count = 1 end out = "" i = 0 while i < count if i > 0 out = out + " " end out = out + msg i = i + 1 end Response.new(200, out)enddef handle_not_found(req) Response.new(404, "Not Found: " + req.path)endclass Router def initialize @log = [] end def dispatch(req) @log.push(req.method_name + " " + req.path) if req.path == "/" return handle_index(req) end if req.path == "/greet" return handle_greet(req) end if req.path == "/echo" return handle_echo(req) end handle_not_found(req) end def entries @log endenddef render(res) res.status.to_s + " " + res.bodyendrouter = Router.newreqs = [ Request.new("GET", "/", { "name" => "" }), Request.new("GET", "/greet", { "name" => "Matz" }), Request.new("GET", "/greet", { "name" => "" }), Request.new("GET", "/echo", { "msg" => "hi", "count" => "3" }), Request.new("GET", "/echo", { "msg" => "x", "count" => "0" }), Request.new("GET", "/missing", { "name" => "" })]i = 0while i < reqs.length res = router.dispatch(reqs[i]) puts render(res) i = i + 1endputs "---log---"entries = router.entriesj = 0while j < entries.length puts entries[j] j = j + 1end