Commit 1d529155

1d5291554707Merged

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

Description

bench: add Jekyll-lite static site generator

A new benchmark exercising the frontmatter -> markdown -> html -> template pipeline that real static site generators use. Patterns distinct from real-blog's Active Record polyvariance:

  • Frontmatter parsing (key: value lines into parallel arrays)
  • Markdown tokens carrying kind / text / level fields
  • Inline rendering walking char-by-char with bold / italic
  • Template engine substituting {{key}} against a string hash

Baseline SP_POLY_REPORT for the new shape: params: 14 / 0 poly locals: 37 / 0 poly ivars: 11 / 0 poly returns:15 / 0 poly

Captured one analyze-side limitation in a comment at the hash-literal composition site (Site#build): a method call mixed with a local read inside the same hash literal widens to str_poly_hash, because scan_locals' first pass walks the literal before the locals merge into scope. Hoisting the calls to locals before the literal works around it; a future backward-inference pass should be able to eliminate the workaround.

Output is deterministic so make bench diffs cleanly against CRuby. All 53 benchmarks now pass.

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

Details

Commit
1d5291554707
Parent
fa74f7217fb3

benchmark/bm_jekyll_lite.rbaddedView file

1+# Jekyll-lite: minimal static site generator. Exercises the
2+# frontmatter -> markdown -> html -> template pipeline that real
3+# static site generators use, with polymorphic patterns distinct
4+# from real-blog's Active Record shape (which is the dominant
5+# source of poly params in the current bench corpus).
6+#
7+# Patterns this exposes:
8+# - Frontmatter: hash with mixed-type values (string + int)
9+# - Markdown tokens: kind / text / level fields on a class
10+# - Inline rendering: char-by-char walk with **bold** / *italic*
11+# - Template engine: {{key}} substitution against a hash
12+#
13+# Output is deterministic so make bench can diff against CRuby.
14+
15+class Frontmatter
16+ def initialize
17+ @keys = []
18+ @vals = []
19+ end
20+
21+ def parse(text)
22+ lines = text.split("\n")
23+ i = 0
24+ while i < lines.length
25+ line = lines[i].strip
26+ if line != ""
27+ colon = line.index(":")
28+ if colon != nil && colon > 0
29+ key = line[0, colon].strip
30+ val = line[colon + 1, line.length - colon - 1].strip
31+ @keys.push(key)
32+ @vals.push(val)
33+ end
34+ end
35+ i = i + 1
36+ end
37+ end
38+
39+ def get(key)
40+ i = 0
41+ while i < @keys.length
42+ if @keys[i] == key
43+ return @vals[i]
44+ end
45+ i = i + 1
46+ end
47+ ""
48+ end
49+end
50+
51+class MdToken
52+ attr_reader :kind
53+ attr_reader :text
54+ attr_reader :level
55+
56+ def initialize(kind, text, level)
57+ @kind = kind
58+ @text = text
59+ @level = level
60+ end
61+end
62+
63+class MdParser
64+ def initialize
65+ @tokens = []
66+ end
67+
68+ def parse(md)
69+ lines = md.split("\n")
70+ i = 0
71+ para = ""
72+ while i < lines.length
73+ line = lines[i]
74+ if line.length > 0 && line[0] == "#"
75+ if para.length > 0
76+ @tokens.push(MdToken.new("paragraph", para, 0))
77+ para = ""
78+ end
79+ level = 0
80+ while level < line.length && line[level] == "#"
81+ level = level + 1
82+ end
83+ rest = line[level, line.length - level].strip
84+ @tokens.push(MdToken.new("heading", rest, level))
85+ elsif line.strip == ""
86+ if para.length > 0
87+ @tokens.push(MdToken.new("paragraph", para, 0))
88+ para = ""
89+ end
90+ else
91+ if para.length > 0
92+ para = para + " "
93+ end
94+ para = para + line.strip
95+ end
96+ i = i + 1
97+ end
98+ if para.length > 0
99+ @tokens.push(MdToken.new("paragraph", para, 0))
100+ end
101+ end
102+
103+ def tokens
104+ @tokens
105+ end
106+end
107+
108+class Inliner
109+ def render(text)
110+ out = ""
111+ i = 0
112+ while i < text.length
113+ ch = text[i]
114+ if ch == "*" && i + 1 < text.length && text[i + 1] == "*"
115+ e = i + 2
116+ while e + 1 < text.length
117+ if text[e] == "*" && text[e + 1] == "*"
118+ break
119+ end
120+ e = e + 1
121+ end
122+ if e + 1 < text.length && text[e] == "*"
123+ out = out + "<strong>" + text[i + 2, e - i - 2] + "</strong>"
124+ i = e + 2
125+ else
126+ out = out + ch
127+ i = i + 1
128+ end
129+ elsif ch == "*"
130+ e = i + 1
131+ while e < text.length && text[e] != "*"
132+ e = e + 1
133+ end
134+ if e < text.length
135+ out = out + "<em>" + text[i + 1, e - i - 1] + "</em>"
136+ i = e + 1
137+ else
138+ out = out + ch
139+ i = i + 1
140+ end
141+ else
142+ out = out + ch
143+ i = i + 1
144+ end
145+ end
146+ out
147+ end
148+end
149+
150+class HtmlEmitter
151+ def initialize
152+ @inliner = Inliner.new
153+ end
154+
155+ def emit(tokens)
156+ out = ""
157+ i = 0
158+ while i < tokens.length
159+ t = tokens[i]
160+ if t.kind == "heading"
161+ ls = t.level.to_s
162+ out = out + "<h" + ls + ">" + @inliner.render(t.text) + "</h" + ls + ">\n"
163+ elsif t.kind == "paragraph"
164+ out = out + "<p>" + @inliner.render(t.text) + "</p>\n"
165+ end
166+ i = i + 1
167+ end
168+ out
169+ end
170+end
171+
172+class Template
173+ def initialize(tmpl)
174+ @tmpl = tmpl
175+ end
176+
177+ def render(vars)
178+ out = ""
179+ i = 0
180+ while i < @tmpl.length
181+ if i + 1 < @tmpl.length && @tmpl[i] == "{" && @tmpl[i + 1] == "{"
182+ j = i + 2
183+ while j + 1 < @tmpl.length && !(@tmpl[j] == "}" && @tmpl[j + 1] == "}")
184+ j = j + 1
185+ end
186+ key = @tmpl[i + 2, j - i - 2].strip
187+ if vars.has_key?(key)
188+ out = out + vars[key]
189+ end
190+ i = j + 2
191+ else
192+ out = out + @tmpl[i]
193+ i = i + 1
194+ end
195+ end
196+ out
197+ end
198+end
199+
200+class Site
201+ def initialize(layout)
202+ @layout = layout
203+ end
204+
205+ def build(doc)
206+ fm_text = ""
207+ body = doc
208+ if doc.length > 4 && doc[0, 4] == "---\n"
209+ end_idx = doc.index("\n---\n", 4)
210+ if end_idx != nil
211+ fm_text = doc[4, end_idx - 4]
212+ body = doc[end_idx + 5, doc.length - end_idx - 5]
213+ end
214+ end
215+ fm = Frontmatter.new
216+ fm.parse(fm_text)
217+ parser = MdParser.new
218+ parser.parse(body)
219+ emitter = HtmlEmitter.new
220+ content = emitter.emit(parser.tokens)
221+ tmpl = Template.new(@layout)
222+ # Hoist fm.get(...) into locals before composing the hash literal.
223+ # Inline method calls mixed with local-variable values widen the
224+ # hash to str_poly_hash even when every value is statically
225+ # string-typed: scan_locals' first pass walks the hash literal
226+ # before the local declarations have been merged into scope, so
227+ # the LV reads default to int, the call returns "" (unresolved),
228+ # and the value-type unification picks poly. With the hoist all
229+ # three values are LV reads that pass-2 resolves to string and
230+ # the literal types as str_str_hash (matches Template.render's
231+ # param signature).
232+ title = fm.get("title")
233+ author = fm.get("author")
234+ vars = {
235+ "title" => title,
236+ "author" => author,
237+ "content" => content
238+ }
239+ tmpl.render(vars)
240+ end
241+end
242+
243+LAYOUT = "<html><head><title>{{title}}</title></head><body><h1>{{title}}</h1><p class='by'>by {{author}}</p>\n{{content}}</body></html>\n"
244+
245+site = Site.new(LAYOUT)
246+
247+DOC1 = "---\ntitle: Hello World\nauthor: Matz\n---\n# Welcome\n\nThis is a **bold** intro paragraph.\n\n## Why static\n\nFast, *cacheable*, and **portable**.\n"
248+
249+DOC2 = "---\ntitle: Second Post\nauthor: spinel\n---\n# Second\n\nA shorter article with *one* emphasis.\n"
250+
251+puts site.build(DOC1)
252+puts "---"
253+puts site.build(DOC2)