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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make it faster馃殌 #24
The head ref may contain hidden characters: "speedup\u{1F680}"
Make it faster馃殌 #24
Conversation
|
||
tracer = TracePoint.new(:call, :return) do |trace_point| | ||
next if match && !trace_point.path.to_s.match?(/#{Array(match).join('|')}/) | ||
next if ignore && trace_point.path.to_s.match?(/#{Array(ignore).join('|')}/) | ||
|
||
id += 1 | ||
caller_path, caller_lineno = trace_point.binding.of_caller(2).source_location | ||
caller_loc = caller_locations(2, 1)[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.
We can use caller_locations
instead of binding.of_caller
. It is faster, and no dependency.
location_cache_key = "#{caller_path}:#{caller_lineno}" | ||
|
||
mes = extract_method_from(trace_point) | ||
next if mes.source_location[0] == '<internal:prelude>' | ||
|
||
method_source = method_source_cache[mes] ||= remove_indent(mes.source) |
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.
UnboundMethod#source
is really heavy. So caching them improves performance dramatically.
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.
Pretty awesome!!
This pull request improves performance.
I've confirmed it was about 6x faster in a micro benchmark and a real rails application.
Benchmark scripts
Micro benchmark
With rails application
Benchmark results
Micro benchmark
first - 3462bd7
Do not use binding_of_caller - 7cf5769
Simplify remove_indent - febf7b0
Cache method source - 900202c
With rails application
I only execute the benchmark with the base commit and the latest commit for Rails app.
first - 3462bd7
Cache method source - 900202c
Note
This pull request contains #23 commit to avoid conflicting.