Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Allow a caller to pass a skip_frames arg to first_non_rspec_line. #155

Merged
merged 1 commit into from
Jan 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions benchmarks/skip_frames_for_caller_filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'rspec/support/caller_filter'

eval <<-EOS, binding, "/lib/rspec/core/some_file.rb", 1 # make it think this code is in rspec-core
class Recurser
def self.recurse(times, *args)
return RSpec::CallerFilter.first_non_rspec_line(*args) if times.zero?
recurse(times - 1, *args)
end
end
EOS

# Ensure the correct first_non_rspec_line is found in these cases.
puts Recurser.recurse(20, 19)
puts Recurser.recurse(20)

require 'benchmark/ips'

Benchmark.ips do |x|
x.report("(no args)") { Recurser.recurse(20) }
x.report("(19)") { Recurser.recurse(20, 19) }
x.report("(19, 2)") { Recurser.recurse(20, 19, 2) }
end

__END__
(no args) 13.789k (± 7.8%) i/s - 69.377k
(19) 55.410k (± 8.4%) i/s - 275.123k
(19, 2) 61.076k (± 8.6%) i/s - 303.637k
32 changes: 23 additions & 9 deletions lib/rspec/support/caller_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,48 @@ class CallerFilter
IGNORE_REGEX = Regexp.union(LIB_REGEX, "rubygems/core_ext/kernel_require.rb")

if RUBY_VERSION >= '2.0.0'
def self.first_non_rspec_line
# This supports args because it's more efficient when the caller specifies
# these. It allows us to skip frames the caller knows are part of RSpec,
# and to decrease the increment size if the caller is confident the line will
# be found in a small number of stack frames from `skip_frames`.
#
# Note that there is a risk to passing a `skip_frames` value that is too high:
# If it skippped the first non-rspec line, then this method would return the
# 2nd or 3rd (or whatever) non-rspec line. Thus, you generally shouldn't pass
# values for these parameters, particularly since most places that use this are
# not hot spots (generally it gets used for deprecation warnings). However,
# if you do have a hot spot that calls this, passing `skip_frames` can make
# a significant difference. Just make sure that that particular use is tested
# so that if the provided `skip_frames` changes to no longer be accurate in
# such a way that would return the wrong stack frame, a test will fail to tell you.
#
# See benchmarks/skip_frames_for_caller_filter.rb for measurements.
def self.first_non_rspec_line(skip_frames=1, increment=5)
# `caller` is an expensive method that scales linearly with the size of
# the stack. The performance hit for fetching it in chunks is small,
# and since the target line is probably near the top of the stack, the
# overall improvement of a chunked search like this is significant.
#
# See benchmarks/caller.rb for measurements.

# Initial value here is mostly arbitrary, but is chosen to give good
# performance on the common case of creating a double.
increment = 5
i = 1
# The default increment of 5 for this method are mostly arbitrary, but
# is chosen to give good performance on the common case of creating a double.

loop do
stack = caller_locations(i, increment)
stack = caller_locations(skip_frames, increment)
raise "No non-lib lines in stack" unless stack

line = stack.find { |l| l.path !~ IGNORE_REGEX }
return line.to_s if line

i += increment
increment *= 2 # The choice of two here is arbitrary.
skip_frames += increment
increment *= 2 # The choice of two here is arbitrary.
end
end
else
# Earlier rubies do not support the two argument form of `caller`. This
# fallback is logically the same, but slower.
def self.first_non_rspec_line
def self.first_non_rspec_line(*)
caller.find { |line| line !~ IGNORE_REGEX }
end
end
Expand Down
4 changes: 4 additions & 0 deletions spec/rspec/support/caller_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ def in_rspec_support_lib(name)
}.to change { $_caller_filter }.to(include "#{__FILE__}:#{__LINE__ - 1}")
end
end

it 'can receive skip_frames and increment arguments' do
expect(RSpec::CallerFilter.first_non_rspec_line(1, 5)).to include("#{__FILE__}:#{__LINE__}")
end
end
end
end