import pathlib
FUNCTION_COUNT = 200
OUT = pathlib.Path(__file__).with_name("large_source.lua")
HEADER = '''-- GENERATED FILE - do not edit by hand.
-- Regenerate with: python3 examples/parse/generate.py
--
-- Probe: parse and codegen time on a large chunk. Unlike every other bench in
-- examples/, the interesting cost here is paid BEFORE _bench() ever runs, so
-- read the harness's parse_us phase rather than warm_avg_us, and expect the
-- standalone wall time to be dominated by parsing rather than execution.
--
-- Shape: {count} top-level function definitions, each with several statements,
-- several call sites, and a nested closure. See examples/parse/generate.py for
-- why this shape and why the functions are globals.
local function mix(a, b)
return a + b * 2
end
local function scale(v)
return v * 3
end
local function tag(v)
return v + 1
end
'''
TEMPLATE = '''function node_{i}_step(alpha, beta)
local acc = mix(alpha, beta)
local scaled = scale(acc)
local label = "node_{i}"
local bump = function(v)
return tag(v) + acc
end
scaled = bump(scaled)
acc = mix(scaled, tag(acc))
local width = scale(tag(acc))
local height = mix(width, scaled)
local depth = tag(mix(height, width))
acc = mix(acc, depth)
if acc > 0 then
acc = scale(acc)
acc = mix(acc, tag(width))
else
acc = tag(acc)
acc = mix(acc, scale(height))
end
local total = mix(acc, mix(width, mix(height, depth)))
total = scale(tag(total))
return total + #label
end
'''
FOOTER = '''function _bench()
local total = 0
for i = 1, 50 do
total = total + node_1_step(i, 2)
total = total + node_100_step(i, 3)
total = total + node_{last}_step(i, 4)
end
return total
end
-- Parsing dominates, so the standalone runner loops only enough to keep the
-- executed portion non-trivial without hiding the parse cost.
for i = 1, 20 do _bench() end
print("parse/large_source: true")
'''
def main():
parts = [HEADER.format(count=FUNCTION_COUNT)]
for i in range(1, FUNCTION_COUNT + 1):
parts.append(TEMPLATE.format(i=i))
parts.append(FOOTER.format(last=FUNCTION_COUNT))
body = "".join(parts)
OUT.write_text(body, encoding="utf-8")
print(f"wrote {OUT} ({body.count(chr(10))} lines)")
if __name__ == "__main__":
main()