-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Conversation
|
||
def_node_matcher :slow_caller?, <<-PATTERN | ||
{ | ||
(send nil :caller) | ||
(send nil :caller int) | ||
(send nil {:caller :caller_locations}) |
There was a problem hiding this comment.
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})
? 🤔
There was a problem hiding this comment.
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 😢
There was a problem hiding this comment.
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] |
There was a problem hiding this comment.
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. 😊
Great change! It looks like the two node matchers can be combined? Because the result of |
`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) ```
@Drenmi I updated. Thanks! |
caller
andcaller_locations
are almost the same. They have difference in a type of return value.caller
returns anArray<String>
, butcaller_locations
returns anArray<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
Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).rake spec
) are passing.rake internal_investigation
.and description in grammatically correct, complete sentences.
rake generate_cops_documentation
(required only when you've added a new cop or changed the configuration/documentation of an existing cop).