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

Add new `Performance/SortWithBlock` cop #3500

Merged
merged 3 commits into from Sep 16, 2016

Conversation

Projects
None yet
2 participants
@koic
Member

koic commented Sep 16, 2016

Feature

This cop identifies places where sort { |a, b| a.foo <=> b.foo } can be replaced by sort_by(&:foo).

% cat test.rb
# frozen_string_literal: true

array = [1, 2, 3]

array.sort { |a, b| a.to_s <=> b.to_s }
% rubocop test.rb
Inspecting 1 file
C

Offenses:

test.rb:5:7: C: Use sort_by(&:to_s) instead of sort { |a, b| a.to_s <=> b.to_s }.
array.sort { |a, b| a.to_s <=> b.to_s }
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected

Target Problem

Enumerable#sort_by { ... } faster than Enumerable#sort { ... }.

Show a benchmark below.

require 'benchmark'

array = (1..100000).map { rand(100000) }

Benchmark.bm(10) do |b|
  b.report("sort")    { array.sort { |a, b| a.to_s <=> b.to_s } }
  b.report("sort_by") { array.sort_by(&:to_s) }
end

produces:

                 user     system      total        real
sort         0.570000   0.000000   0.570000 (  0.571723)
sort_by      0.130000   0.000000   0.130000 (  0.129793)

Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Used the same coding conventions as the rest of the project.
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • All tests are passing.
  • The new code doesn't generate RuboCop offenses.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
Show outdated Hide outdated lib/rubocop/cop/performance/sort_with_block.rb Outdated
describe RuboCop::Cop::Performance::SortWithBlock do
subject(:cop) { described_class.new }
it 'autocorrects array.sort { |a, b| a.foo <=> b.foo }' do

This comment has been minimized.

@bbatsov

bbatsov Sep 16, 2016

Collaborator

Typically we also have some standalone tests testing simply that offenses are being registered/not registered. I'd definitely add some negative test cases.

@bbatsov

bbatsov Sep 16, 2016

Collaborator

Typically we also have some standalone tests testing simply that offenses are being registered/not registered. I'd definitely add some negative test cases.

koic added some commits Sep 15, 2016

Add new `Performance/SortWithBlock` cop
This cop identifies places where `sort { |a, b| a.foo <=> b.foo }`
can be replaced by `sort_by(&:foo)`.
@koic

This comment has been minimized.

Show comment
Hide comment
@koic

koic Sep 16, 2016

Member

Thanks for your review. I fixed that item that was pointed out. How does that look?

Member

koic commented Sep 16, 2016

Thanks for your review. I fixed that item that was pointed out. How does that look?

@bbatsov bbatsov merged commit caaf0e2 into rubocop-hq:master Sep 16, 2016

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
@bbatsov

This comment has been minimized.

Show comment
Hide comment
@bbatsov

bbatsov Sep 16, 2016

Collaborator

It's in a decent shape. Thanks!

Collaborator

bbatsov commented Sep 16, 2016

It's in a decent shape. Thanks!

@koic

This comment has been minimized.

Show comment
Hide comment
@koic

koic Sep 16, 2016

Member

Thanks too!

Member

koic commented Sep 16, 2016

Thanks too!

@koic koic deleted the koic:sort_with_block branch Sep 16, 2016

Neodelf added a commit to Neodelf/rubocop that referenced this pull request Oct 15, 2016

Add new `Performance/SortWithBlock` cop (rubocop-hq#3500)
This cop identifies places where `sort { |a, b| a.foo <=> b.foo }`
can be replaced by `sort_by(&:foo)`.

pocke added a commit to pocke/rubocop that referenced this pull request Nov 29, 2016

Checks `min` and `max` call by `Perfomance/CompareWithBlock` cop
Ref: rubocop-hq#3500

Problem
======

`SortWithBlock` cop checks slow `sort` method call.
`min` and `max` method have same performance problem.
However, the methods is not checked by this cop.

- https://ruby-doc.org/core-2.3.3/Enumerable.html#method-i-max
- https://ruby-doc.org/core-2.3.3/Enumerable.html#method-i-min

Benchmark
=======

```ruby
require 'benchmark'

array = (1..1000000).map { rand(1000000) }

Benchmark.bm(10) do |b|
  b.report("sort")    { array.sort { |a, b| a.to_s <=> b.to_s } }
  b.report("sort_by") { array.sort_by(&:to_s) }

  b.report("max")    { array.max { |a, b| a.to_s <=> b.to_s } }
  b.report("max_by") { array.max_by(&:to_s) }

  b.report("min")    { array.min { |a, b| a.to_s <=> b.to_s } }
  b.report("min_by") { array.min_by(&:to_s) }
end
```

```
$ ruby --version
ruby 2.3.2p217 (2016-11-15 revision 56796) [x86_64-linux]
$ ruby test.rb
                 user     system      total        real
sort         6.660000   0.010000   6.670000 (  6.664807)
sort_by      1.910000   0.010000   1.920000 (  1.928268)
max          0.390000   0.000000   0.390000 (  0.385470)
max_by       0.200000   0.000000   0.200000 (  0.203039)
min          0.290000   0.000000   0.290000 (  0.288846)
min_by       0.210000   0.000000   0.210000 (  0.210002)
```

Goal
=======

I've added a check for `max` and `min` methods by `CompareWithBlock` cop.

- [x] Rename `Perfomance/SortWithBlock` to `Perfomance/CompareWithBlock`
- [x] Add test

pocke added a commit to pocke/rubocop that referenced this pull request Nov 29, 2016

Checks `min` and `max` call by `Perfomance/CompareWithBlock` cop
Ref: rubocop-hq#3500

Problem
======

`SortWithBlock` cop checks slow `sort` method call.
`min` and `max` method have same performance problem.
However, the methods is not checked by this cop.

- https://ruby-doc.org/core-2.3.3/Enumerable.html#method-i-max
- https://ruby-doc.org/core-2.3.3/Enumerable.html#method-i-min

Benchmark
=======

```ruby
require 'benchmark'

array = (1..1000000).map { rand(1000000) }

Benchmark.bm(10) do |b|
  b.report("sort")    { array.sort { |a, b| a.to_s <=> b.to_s } }
  b.report("sort_by") { array.sort_by(&:to_s) }

  b.report("max")    { array.max { |a, b| a.to_s <=> b.to_s } }
  b.report("max_by") { array.max_by(&:to_s) }

  b.report("min")    { array.min { |a, b| a.to_s <=> b.to_s } }
  b.report("min_by") { array.min_by(&:to_s) }
end
```

```
$ ruby --version
ruby 2.3.2p217 (2016-11-15 revision 56796) [x86_64-linux]
$ ruby test.rb
                 user     system      total        real
sort         6.660000   0.010000   6.670000 (  6.664807)
sort_by      1.910000   0.010000   1.920000 (  1.928268)
max          0.390000   0.000000   0.390000 (  0.385470)
max_by       0.200000   0.000000   0.200000 (  0.203039)
min          0.290000   0.000000   0.290000 (  0.288846)
min_by       0.210000   0.000000   0.210000 (  0.210002)
```

Goal
=======

I've added a check for `max` and `min` methods by `CompareWithBlock` cop.

- [x] Rename `Perfomance/SortWithBlock` to `Perfomance/CompareWithBlock`
- [x] Add test

bbatsov added a commit that referenced this pull request Nov 29, 2016

Checks `min` and `max` call by `Perfomance/CompareWithBlock` cop
Ref: #3500

Problem
======

`SortWithBlock` cop checks slow `sort` method call.
`min` and `max` method have same performance problem.
However, the methods is not checked by this cop.

- https://ruby-doc.org/core-2.3.3/Enumerable.html#method-i-max
- https://ruby-doc.org/core-2.3.3/Enumerable.html#method-i-min

Benchmark
=======

```ruby
require 'benchmark'

array = (1..1000000).map { rand(1000000) }

Benchmark.bm(10) do |b|
  b.report("sort")    { array.sort { |a, b| a.to_s <=> b.to_s } }
  b.report("sort_by") { array.sort_by(&:to_s) }

  b.report("max")    { array.max { |a, b| a.to_s <=> b.to_s } }
  b.report("max_by") { array.max_by(&:to_s) }

  b.report("min")    { array.min { |a, b| a.to_s <=> b.to_s } }
  b.report("min_by") { array.min_by(&:to_s) }
end
```

```
$ ruby --version
ruby 2.3.2p217 (2016-11-15 revision 56796) [x86_64-linux]
$ ruby test.rb
                 user     system      total        real
sort         6.660000   0.010000   6.670000 (  6.664807)
sort_by      1.910000   0.010000   1.920000 (  1.928268)
max          0.390000   0.000000   0.390000 (  0.385470)
max_by       0.200000   0.000000   0.200000 (  0.203039)
min          0.290000   0.000000   0.290000 (  0.288846)
min_by       0.210000   0.000000   0.210000 (  0.210002)
```

Goal
=======

I've added a check for `max` and `min` methods by `CompareWithBlock` cop.

- [x] Rename `Perfomance/SortWithBlock` to `Perfomance/CompareWithBlock`
- [x] Add test

koic added a commit to koic/rubocop-performance that referenced this pull request Oct 12, 2018

Checks `min` and `max` call by `Perfomance/CompareWithBlock` cop
Ref: rubocop-hq/rubocop#3500

Problem
======

`SortWithBlock` cop checks slow `sort` method call.
`min` and `max` method have same performance problem.
However, the methods is not checked by this cop.

- https://ruby-doc.org/core-2.3.3/Enumerable.html#method-i-max
- https://ruby-doc.org/core-2.3.3/Enumerable.html#method-i-min

Benchmark
=======

```ruby
require 'benchmark'

array = (1..1000000).map { rand(1000000) }

Benchmark.bm(10) do |b|
  b.report("sort")    { array.sort { |a, b| a.to_s <=> b.to_s } }
  b.report("sort_by") { array.sort_by(&:to_s) }

  b.report("max")    { array.max { |a, b| a.to_s <=> b.to_s } }
  b.report("max_by") { array.max_by(&:to_s) }

  b.report("min")    { array.min { |a, b| a.to_s <=> b.to_s } }
  b.report("min_by") { array.min_by(&:to_s) }
end
```

```
$ ruby --version
ruby 2.3.2p217 (2016-11-15 revision 56796) [x86_64-linux]
$ ruby test.rb
                 user     system      total        real
sort         6.660000   0.010000   6.670000 (  6.664807)
sort_by      1.910000   0.010000   1.920000 (  1.928268)
max          0.390000   0.000000   0.390000 (  0.385470)
max_by       0.200000   0.000000   0.200000 (  0.203039)
min          0.290000   0.000000   0.290000 (  0.288846)
min_by       0.210000   0.000000   0.210000 (  0.210002)
```

Goal
=======

I've added a check for `max` and `min` methods by `CompareWithBlock` cop.

- [x] Rename `Perfomance/SortWithBlock` to `Perfomance/CompareWithBlock`
- [x] Add test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment