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

Checks min and max call by Perfomance/CompareWithBlock cop #3742

Merged
merged 1 commit into from Nov 29, 2016

Conversation

pocke
Copy link
Collaborator

@pocke pocke commented Nov 29, 2016

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.

Benchmark

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.

  • Rename Perfomance/SortWithBlock to Perfomance/CompareWithBlock
  • Add test

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.
  • Updated cop documentation with rake generate_cops_documentation (required only when you've added a new cop or changed the configuration/documentation of an existing cop).

Ref: 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
@bbatsov bbatsov merged commit 5e5c573 into rubocop:master Nov 29, 2016
@pocke pocke deleted the compare-with-block branch November 29, 2016 23:32
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.

None yet

2 participants