Skip to content

Commit

Permalink
Configure Style/NumericPredicate for performance
Browse files Browse the repository at this point in the history
Replace predicate methods like `v.zero?` with direct comparison `v == 0`
for better performance.

While the current implementation may not show visible improvements,
frequently called methods using `.zero?` instead of `== 0` can lead to
significant performance losses.

Benchmarks across Ruby versions 2.5 to 3.3 consistently show comparisons 
outperforming predicates:

```
Comparison (Ruby 2.6 to 3.2, v = 0):
              v == 0: 21479329.3 i/s
             v.zero?: 17979885.4 i/s - 1.19x  (± 0.00) slower

Comparison (Ruby 2.5 and 3.3, v = 0):
              v == 0: 23652215.7 i/s
             v.zero?: 21843174.0 i/s - 1.08x  (± 0.00) slower

Comparison (Ruby 2.5 to 3.3, v = 1):
              v == 0: 23227474.2 i/s
             v.zero?: 21675200.9 i/s - 1.07x  (± 0.00) slower
```
  • Loading branch information
tagliala committed Sep 26, 2024
1 parent ef52a1a commit a46e568
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ Style/DocumentDynamicEvalDefinition:

Style/MultipleComparison:
Enabled: false

Style/NumericPredicate:
EnforcedStyle: comparison
2 changes: 1 addition & 1 deletion lib/memo_wise/internal_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def self.create_memo_wise_state!(obj)
# - :double_splat (examples: `def foo(a: 1)`, `def foo(a:, **b)`)
# - :splat_and_double_splat (examples: `def foo(a=1, b: 2)`, `def foo(a=1, **b)`, `def foo(*a, **b)`)
def self.method_arguments(method)
return NONE if method.arity.zero?
return NONE if method.arity == 0

parameters = method.parameters.map(&:first)

Expand Down

0 comments on commit a46e568

Please sign in to comment.