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

Make Performance/Caller aware of caller_locations #4551

Merged
merged 3 commits into from
Jun 26, 2017

Conversation

pocke
Copy link
Collaborator

@pocke pocke commented Jun 25, 2017

caller and caller_locations are almost the same. They have difference in a type of return value. caller returns an Array<String>, but caller_locations returns an Array<Thread::Backtrace::Location>.
The difference is not problematic for this cop. So, I'll make this cop aware of caller_locations by this change.

Benchmark

require 'benchmark'

def method_for_backtrace
  tap do
    tap do
      yield
    end
  end
end

def assert(a, b)
  raise "#{a} != #{b}" if a != b
end

method_for_backtrace do
  assert(caller, caller_locations.map(&:to_s))
  assert(caller_locations.first.to_s, caller_locations(1..1).first.to_s)
  assert(caller_locations[1].to_s, caller_locations(2..2).first.to_s)

  Benchmark.bm(30) do |x|
    x.report('caller_locations.first')      {1000000.times{caller_locations.first}}
    x.report('caller_locations(1..1).first'){1000000.times{caller_locations(1..1).first}}

    x.report('caller_locations[1]')         {1000000.times{caller_locations[1]}}
    x.report('caller_locations(2..2).first'){1000000.times{caller_locations(2..2).first}}
  end
end
                                     user     system      total        real
caller_locations.first           1.660000   0.000000   1.660000 (  1.656021)
caller_locations(1..1).first     0.390000   0.000000   0.390000 (  0.392523)
caller_locations[1]              1.660000   0.000000   1.660000 (  1.661755)
caller_locations(2..2).first     0.460000   0.000000   0.460000 (  0.462888)

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(rake spec) are passing.
  • The new code doesn't generate RuboCop offenses that are checked by rake internal_investigation.
  • 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).


def_node_matcher :slow_caller?, <<-PATTERN
{
(send nil :caller)
(send nil :caller int)
(send nil {:caller :caller_locations})
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would it work with:

(send nil {:caller :caller_locations} {int nil})

? 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hmm, it does not work. Because the matcher checks children size. https://github.com/bbatsov/rubocop/blob/master/lib/rubocop/node_pattern.rb#L201

I think if ? operator like regular expression is added to the matcher, it is handy.
For example:

# int is optional.
(send nil {:caller :caller_locations} int?)

However, this syntax is conflict with existing one 😢

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah. We'd need to come up with something that is available. Will look into this later. 🙂

@@ -44,15 +48,16 @@ def on_send(node)
private

def message(node)
method_name = node.receiver.method_name
caller_arg = node.receiver.arguments[0]
Copy link
Collaborator

@Drenmi Drenmi Jun 25, 2017

Choose a reason for hiding this comment

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

There's a #first_argument method on parameterized nodes, that could be used here, and on L:51. 😊

@Drenmi
Copy link
Collaborator

Drenmi commented Jun 25, 2017

Great change! It looks like the two node matchers can be combined? Because the result of #caller_with_scope_method? is just passed on to #slow_caller?. WDYT? 🙂

@pocke pocke force-pushed the caller_locations branch from 093983b to ecb6abc Compare June 25, 2017 13:24
`caller` and `caller_locations` are almost the same. They have difference in a type of return value. `caller` returns an `Array<String>`, but `caller_locations` returns an `Array<Thread::Backtrace::Location>`.
The difference is not problematic for this cop. So, I'll make this cop aware of `caller_locations` by this change.

Benchmark
============

```ruby
require 'benchmark'

def method_for_backtrace
  tap do
    tap do
      yield
    end
  end
end

def assert(a, b)
  raise "#{a} != #{b}" if a != b
end

method_for_backtrace do
  assert(caller, caller_locations.map(&:to_s))
  assert(caller_locations.first.to_s, caller_locations(1..1).first.to_s)
  assert(caller_locations[1].to_s, caller_locations(2..2).first.to_s)

  Benchmark.bm(30) do |x|
    x.report('caller_locations.first')      {1000000.times{caller_locations.first}}
    x.report('caller_locations(1..1).first'){1000000.times{caller_locations(1..1).first}}

    x.report('caller_locations[1]')         {1000000.times{caller_locations[1]}}
    x.report('caller_locations(2..2).first'){1000000.times{caller_locations(2..2).first}}
  end
end
```

```
                                     user     system      total        real
caller_locations.first           1.660000   0.000000   1.660000 (  1.656021)
caller_locations(1..1).first     0.390000   0.000000   0.390000 (  0.392523)
caller_locations[1]              1.660000   0.000000   1.660000 (  1.661755)
caller_locations(2..2).first     0.460000   0.000000   0.460000 (  0.462888)
```
@pocke
Copy link
Collaborator Author

pocke commented Jun 25, 2017

@Drenmi I updated. Thanks!

@bbatsov bbatsov merged commit e5617ec into rubocop:master Jun 26, 2017
@pocke pocke deleted the caller_locations branch July 4, 2017 11:19
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.

3 participants