Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Performance/CollectionLiteralInLoop to not register offenses for Array#include? that are optimized directly in Ruby. #488

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Earlopain
Copy link
Contributor

Fix #482. Since include? on arrays are the only instances I ever run across this cop, I think it makes sense to add special handling for this.

On Ruby 3.4 this does no array allocations:

require "memory_profiler"

class Foo
  def bar
    Bar.new
  end
end

class Bar
  def baz
  end
end

foo = Foo.new
report = MemoryProfiler.report do
  [1,2].include?(foo.bar.baz)
end.pretty_print

Also, this code is simply slower on Ruby 3.4:

require "benchmark/ips"

local = [301, 302]
val = 301
Benchmark.ips do |x|
  x.report('local') do
    local.include?(val)
  end

  x.report('literal') do
    [301, 302].include?(val)
  end
  x.compare!
end
$ ruby test.rb
ruby 3.4.1 (2024-12-25 revision 48d4efcb85) +PRISM [x86_64-linux]
Warming up --------------------------------------
               local     1.822M i/100ms
             literal     2.296M i/100ms
Calculating -------------------------------------
               local     18.235M (± 1.6%) i/s   (54.84 ns/i) -     92.906M in   5.096277s
             literal     22.807M (± 1.5%) i/s   (43.85 ns/i) -    114.789M in   5.034289s

Comparison:
             literal: 22806932.0 i/s
               local: 18235340.7 i/s - 1.25x  slower

Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

… register offenses for `Array#include?` that are optimized directly in Ruby.

Since `include?` on arrays are the only instances I ever run across this cop, I think it makes sense to add special handling for this.

On Ruby 3.4 this does no array allocations:
```
require "memory_profiler"

class Foo
  def bar
    Bar.new
  end
end

class Bar
  def baz
  end
end

foo = Foo.new
report = MemoryProfiler.report do
  [1,2].include?(foo.bar.baz)
end.pretty_print
```

Also, this code is simply slower on Ruby 3.4:

```rb
require "benchmark/ips"

local = [301, 302]
val = 301
Benchmark.ips do |x|
  x.report('local') do
    local.include?(val)
  end

  x.report('literal') do
    [301, 302].include?(val)
  end
  x.compare!
end
```

```
$ ruby test.rb
ruby 3.4.1 (2024-12-25 revision 48d4efcb85) +PRISM [x86_64-linux]
Warming up --------------------------------------
               local     1.822M i/100ms
             literal     2.296M i/100ms
Calculating -------------------------------------
               local     18.235M (± 1.6%) i/s   (54.84 ns/i) -     92.906M in   5.096277s
             literal     22.807M (± 1.5%) i/s   (43.85 ns/i) -    114.789M in   5.034289s

Comparison:
             literal: 22806932.0 i/s
               local: 18235340.7 i/s - 1.25x  slower
```
@Earlopain
Copy link
Contributor Author

🤔 how do I get the oldest rubocop version tests to pass when it doesn't know about ruby34?

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.

Investigate Performance/CollectionLiteralInLoop for Ruby 3.4
1 participant