Stop rebuilding the defaults Hash on every option read - #438
Merged
Conversation
Every one of the 13 generated option readers ran: options[option] || options[option.to_s] || default_options[option] so each read that was not explicitly set allocated a String from `to_s` and then built a fresh 5 entry Hash (including an ENV lookup) just to pull one key out of it. `Options#for_ps1?` reads two options, so it did that twice. Freeze the String key once when the reader is defined, and look up a single default instead of materializing the whole Hash. `license_id` still reads ENV["CHEF_LICENSE_KEY"] on every access, so changes to the environment are picked up exactly as before. The `||` chain is kept rather than switched to nil checks so that an explicitly `false` option still falls through to its default, as it does today. Measured on Ruby 4.0.6: #shell_type 445 ns -> 180 ns 2.5x 3.02 -> 0.02 allocations #product_version 450 ns -> 140 ns 3.2x 3 -> 0 allocations #for_ps1? 907 ns -> 327 ns 2.8x 7 -> 1 allocation #latest_version? 483 ns -> 174 ns 2.8x .new 4094 ns -> 2632 ns 1.6x 30 -> 16 allocations validate_options! 1193 ns -> 867 ns 1.4x Output is unchanged: 576 unit examples pass and all 176 golden SHA256 digests match main. Signed-off-by: Tim Smith <tsmith84@proton.me>
tas50
force-pushed
the
perf/options-accessor
branch
from
August 28, 2026 03:58
18dc925 to
bc8c7d2
Compare
tpowell-progress
approved these changes
Aug 31, 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
Every one of the 13 generated option readers ran:
So each read that was not explicitly set allocated a
Stringfromto_sand then built a fresh 5-entryHash— including anENVlookup — just to pull one key back out of it.Options#for_ps1?reads two options, so it did that twice per call, andfor_ps1?is on the hot path for both script generation and artifact filtering.This freezes the String key once when the reader is defined, and looks up a single default instead of materializing the whole Hash.
license_idstill readsENV["CHEF_LICENSE_KEY"]on every access, so environment changes are picked up exactly as before.The
||chain is deliberately kept rather than switched tonil?checks, so an explicitlyfalseoption still falls through to its default exactly as it does today.Measurements
Ruby 4.0.6:
#shell_type(default)#product_version(default)#for_ps1?#latest_version?.new#validate_options!#product_nameand other explicitly-set reads are unchanged at ~78 ns, as expected — they never reached the defaults branch.Verification
chefstyleclean.niloptions and theCHEF_LICENSE_KEYenvironment fallback all resolve identically, including picking up an environment change between twoOptions.newcalls.main.