Skip to content

Document performance overhead in README - #61

Merged
dblock merged 1 commit into
masterfrom
document-performance
Aug 15, 2026
Merged

Document performance overhead in README#61
dblock merged 1 commit into
masterfrom
document-performance

Conversation

@dblock

@dblock dblock commented Aug 15, 2026

Copy link
Copy Markdown
Owner

What

Adds a new Performance subsection under Benchmarks in README.md, documenting the actual overhead of Ruby::Enum's basic operations vs. plain Ruby equivalents, backed by a new benchmark script.

Benchmarks added

benchmarks/basic.rb (rake benchmark:basic) compares, over 1,000,000 iterations:

  • Constant access: Colors::RED vs. a plain Ruby constant
  • Key → value lookup: Colors.value(:RED) vs. Hash#[]
  • Value → key lookup: Colors.key('red') vs. Hash#key
  • Existence checks: Colors.key?(:RED) vs. Hash#key?
  • Enumerating: Colors.keys/Colors.values vs. Hash#keys/Hash#values

Sample results (may vary by machine)

--- Constant access ---
Ruby::Enum constant (Colors::RED): 0.0167
plain Ruby constant (PlainConstants::RED): 0.0165

--- Key to value lookup ---
Ruby::Enum (Colors.value(:RED)): 0.0633
plain Hash (PLAIN_HASH[:RED]): 0.0211

--- Comparison ---
Ruby::Enum constant access is 1.01x plain Ruby constant access
Ruby::Enum .value is 3.0x plain Hash#[]

README

The new Performance section summarizes:

  • Constant access has no measurable overhead.
  • Hash-backed lookups (value, key, key?, value?, keys, values) carry roughly 3-5x overhead vs. a plain Hash, due to method dispatch/object wrapping.
  • This overhead does not grow with subclass depth, thanks to the memoization added in Memoize merged enum hashes to fix superclass-lookup performance regression #60 (see rake benchmark:inheritance).
  • Ruby::Enum::Case is the one notable outlier, at 50-100x slower than a native case statement (see rake benchmark:case), since it's the only feature involving lambda construction/evaluation at call time.

Testing

  • bundle exec rake (rubocop + rspec): 69 examples, 0 failures, rubocop clean (25 files)
  • rake benchmark:basic run manually to generate the numbers above

@github-actions

github-actions Bot commented Aug 15, 2026

Copy link
Copy Markdown

Danger Report

No issues found.

View run

Add benchmarks/basic.rb comparing basic Ruby::Enum operations
(constant access, value, key, key?, keys, values) against their
plain Ruby equivalents (constants, Hash), and a rake benchmark:basic
task to run it.

Add a new 'Performance' subsection under Benchmarks in README.md
documenting, with real numbers, that constant access has no
measurable overhead while hash-backed lookups carry roughly 3-5x
overhead vs. a plain Hash, that this overhead does not grow with
subclass depth (see rake benchmark:inheritance), and that
Ruby::Enum::Case is the one notable outlier at 50-100x slower than
a native case statement (see rake benchmark:case).

Co-authored-by: Copilot <[email protected]>
@dblock
dblock force-pushed the document-performance branch from 67f6c30 to bf5bfb1 Compare August 15, 2026 02:00
@dblock
dblock merged commit 4c2bfb8 into master Aug 15, 2026
22 checks passed
@dblock
dblock deleted the document-performance branch August 15, 2026 02:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant