-
-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…er 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 ```
- Loading branch information
Showing
3 changed files
with
106 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#482](https://github.com/rubocop/rubocop-performance/issues/482): Change `Performance/CollectionLiteralInLoop` to not register offenses for `Array#include?` that are optimized directly in Ruby. ([@earlopain][]) |
This file contains 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
This file contains 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