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 50a5b0a
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 2 deletions.
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 benchmarks/benchmarks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ def positional_splat_keyword_and_double_splat_args(a, *args, b:, **kwargs)
benchmark_json.sort_by! { |json| json["name"] }

# Print headers based on the first benchmark_json
if i.zero?
if i == 0
benchmark_headers = benchmark_json.map do |benchmark_gem|
# Gem name is of the form:
# "memoist (1.1.0): ()"
Expand Down
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 50a5b0a

Please sign in to comment.