Memoize merged enum hashes to fix superclass-lookup performance regression - #60
Merged
Conversation
Danger ReportWarnings
MarkdownsHere's an example of a CHANGELOG.md entry: * [#60](https://github.com/dblock/ruby-enum/pull/60): Memoize merged enum hashes to fix superclass-lookup performance regression - [@dblock](https://github.com/dblock). |
Fix a performance regression introduced when keys, key?, value?, key, value, to_h, parse and each were made to walk the superclass chain: every call was recomputing the merged hash from scratch instead of caching it, making these methods significantly slower on subclasses. - Memoize _enum_hash/_enums_by_value, invalidating on define. - Disable Metrics/ModuleLength rubocop cop (module grew past the default 100-line limit due to the memoization). - Add benchmarks/inheritance.rb covering base/subclass/sub-subclass .value lookups plus .keys, .key?, .to_h and .each on a subclass, and a rake benchmark:inheritance task. - Fix README documenting the case benchmark task as 'rake benchmarks:case' instead of the actual 'rake benchmark:case'. Co-authored-by: Copilot <[email protected]>
dblock
force-pushed
the
memoize-enum-hash
branch
from
August 15, 2026 01:54
7bd68a9 to
6c22990
Compare
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.
What
Benchmarking the superclass-inheritance work from #59 surfaced a performance regression:
keys,key?,value?,key,value,to_h,parse,each,each_key, andeach_valuenow walk and merge the entire superclass chain on every call, with no caching. On a 1-level subclass this made.value~5x slower and.keys~3x slower than the base class.Fix
_enum_hash/_enums_by_value, invalidating the cache instore_new_instance(i.e. ondefine) so newly defined enums are always reflected.Metrics/ModuleLength(module grew past the default 100-line limit purely due to the extra memoization/invalidation lines).benchmarks/inheritance.rb+rake benchmark:inheritancetask to track this going forward.rake benchmarks:case(doesn't exist) instead of the actualrake benchmark:case.Before / After (1,000,000 iterations, 1-level subclass)
SubColors.valueSubColors.keysTesting
bundle exec rake(rubocop + rspec): 69 examples, 0 failures, rubocop clean (24 files)spec_i18nsuite: 3 examples, 0 failuresrake benchmark:inheritancerun manually to confirm the fix