Lazily load erb and ostruct - #435
Merged
Merged
Conversation
`erb` costs ~7ms to require and pulls in strscan, cgi/escape and the rest of the erb tree (11 files). It and `ostruct` are only touched inside Generator::Base.get_script when a template actually needs rendering, so load them there rather than at require time. Also fixes the guard on the erb require: the constant is `ERB`, not `Erb`, so `unless defined?(Erb)` never matched and the guard never did anything. Measured on Ruby 4.0.6 (min of 15 fresh subprocesses): before 56.85 ms 240 loaded features after 49.11 ms 229 loaded features Generated output is unchanged: 176 SHA256 digests covering install.sh, install.ps1, the platform detection scripts, every product matrix entry and the ScriptGenerator variants are byte-identical to main. Signed-off-by: Tim Smith <tsmith84@proton.me>
tpowell-progress
approved these changes
Aug 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
erbcosts ~7 ms to require and pulls instrscan,cgi/escapeand the rest of the erb tree — 11 files. It andostructare only touched insideGenerator::Base.get_script, and only when a template actually needs rendering, so this loads them there rather than at require time.Drive-by fix
The existing guard never worked:
The constant is
ERB, notErb, sodefined?(Erb)was alwaysniland the guard never suppressed anything. Corrected todefined?(ERB)as part of the move.Measurements
Ruby 4.0.6, minimum of 15 fresh subprocesses:
7.74 ms and 11 files saved on every consumer's boot.
Verification
chefstyleclean.ERBandOpenStructare undefined afterrequire "mixlib/install"and defined after the first render.install.sh,install.ps1, both platform-detection scripts, every product matrix entry and theScriptGeneratorvariants are byte-identical tomain. This matters here because CI feeds the generated scripts to ShellCheck and PSScriptAnalyzer.