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

Check slow hash accessing in Array#sort by Performance/CompareWithBlock cop #4314

Merged
merged 1 commit into from
Apr 26, 2017

Conversation

pocke
Copy link
Collaborator

@pocke pocke commented Apr 25, 2017

Currently, Performance/CompareWithBlock cop accepts the following code.

array.sort { |a, b| a[:foo] <=> b[:foo] }

But the code has same issue as a.foo <=> b.foo.
So, by this change, the cop registers an offense for this code.

Benchmark

require 'benchmark'

array10000 = 10000.times.map do |i|
  {value: i}
end.shuffle

array100 = 100.times.map do |i|
  {value: i}
end.shuffle

array10 = 10.times.map do |i|
  {value: i}
end.shuffle

Benchmark.bm(15) do |x|
  x.report('sort_by 10000'){500.times{array10000.sort_by{|a| a[:value]}}}
  x.report('sort    10000'){500.times{array10000.sort{|a, b| a[:value] <=> b[:value]}}}

  x.report('sort_by 100'){50000.times{array100.sort_by{|a| a[:value]}}}
  x.report('sort    100'){50000.times{array100.sort{|a, b| a[:value] <=> b[:value]}}}

  x.report('sort_by 10'){500000.times{array10.sort_by{|a| a[:value]}}}
  x.report('sort    10'){500000.times{array10.sort{|a, b| a[:value] <=> b[:value]}}}
end
$ ruby test.rb
                      user     system      total        real
sort_by 10000     2.470000   0.000000   2.470000 (  2.480009)
sort    10000     4.790000   0.010000   4.800000 (  4.797943)
sort_by 100       1.140000   0.000000   1.140000 (  1.146861)
sort    100       1.910000   0.000000   1.910000 (  1.910717)
sort_by 10        0.880000   0.000000   0.880000 (  0.871086)
sort    10        1.000000   0.000000   1.000000 (  1.002856)

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).

@pocke pocke force-pushed the Performance/CompareWithBlock branch from 151ed26 to 805ae8c Compare April 25, 2017 07:13
expect(cop.offenses.size).to eq(1)
end

it 'highlights compare emthod' do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

emthod -> method

…Block`

Currently, `Performance/CompareWithBlock` cop accepts the following code.

```ruby
array.sort { |a, b| a[:foo] <=> b[:foo] }
```

But the code has same issue as `a.foo <=> b.foo`.
So, by this change, the cop registers an offense for this code.

Benchmark
========

```ruby
require 'benchmark'

array10000 = 10000.times.map do |i|
  {value: i}
end.shuffle

array100 = 100.times.map do |i|
  {value: i}
end.shuffle

array10 = 10.times.map do |i|
  {value: i}
end.shuffle

Benchmark.bm(15) do |x|
  x.report('sort_by 10000'){500.times{array10000.sort_by{|a| a[:value]}}}
  x.report('sort    10000'){500.times{array10000.sort{|a, b| a[:value] <=> b[:value]}}}

  x.report('sort_by 100'){50000.times{array100.sort_by{|a| a[:value]}}}
  x.report('sort    100'){50000.times{array100.sort{|a, b| a[:value] <=> b[:value]}}}

  x.report('sort_by 10'){500000.times{array10.sort_by{|a| a[:value]}}}
  x.report('sort    10'){500000.times{array10.sort{|a, b| a[:value] <=> b[:value]}}}
end
```

```bash
$ ruby test.rb
                      user     system      total        real
sort_by 10000     2.470000   0.000000   2.470000 (  2.480009)
sort    10000     4.790000   0.010000   4.800000 (  4.797943)
sort_by 100       1.140000   0.000000   1.140000 (  1.146861)
sort    100       1.910000   0.000000   1.910000 (  1.910717)
sort_by 10        0.880000   0.000000   0.880000 (  0.871086)
sort    10        1.000000   0.000000   1.000000 (  1.002856)
```
@pocke pocke force-pushed the Performance/CompareWithBlock branch from 805ae8c to 9e2eec1 Compare April 26, 2017 04:41
@pocke
Copy link
Collaborator Author

pocke commented Apr 26, 2017

@bbatsov Thanks. I fixed and rebaesd.

@bbatsov bbatsov merged commit 8ba6928 into rubocop:master Apr 26, 2017
@pocke pocke deleted the Performance/CompareWithBlock branch April 26, 2017 07:00
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.

2 participants