Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Perf: simplify calculation of location

- caller is only used to determine file and line number as metadata is
  initialized. There is no need to store it.
- Also, the previous commit reduces the need for analysis of the caller
  stack. We therefore don't need to sling it around within the rspec
  codebase.
- Running the following 5000 times took 2.77 seconds before these last
  two commits, 1:58 after:

    describe "something" do
      it "does something" do
        1.should eq(1)
      end
    end

Please enter the commit message for your changes. Lines starting
  • Loading branch information...
commit f78ff619e5e07dc099646444b08d7654516f9666 1 parent 9c6b6f7
@dchelimsky dchelimsky authored
View
2  lib/rspec/core/example_group.rb
@@ -42,7 +42,6 @@ def self.define_example_method(name, extra_options={})
module_eval(<<-END_RUBY, __FILE__, __LINE__)
def self.#{name}(desc=nil, options={}, &block)
options.update(:pending => true) unless block
- options.update(:caller => caller)
options.update(#{extra_options.inspect})
examples << RSpec::Core::Example.new(self, desc, options, block)
examples.last
@@ -111,7 +110,6 @@ def self.describe(*args, &example_group_block)
@_subclass_count += 1
args << {} unless args.last.is_a?(Hash)
args.last.update(:example_group_block => example_group_block)
- args.last.update(:caller => caller)
# TODO 2010-05-05: Because we don't know if const_set is thread-safe
child = const_set(
View
1  lib/rspec/core/extensions/object.rb
@@ -3,7 +3,6 @@ module Core
module ObjectExtensions
def describe(*args, &example_group_block)
args << {} unless args.last.is_a?(Hash)
- args.last.update :caller => caller(1)
RSpec::Core::ExampleGroup.describe(*args, &example_group_block)
end
end
View
34 lib/rspec/core/metadata.rb
@@ -32,9 +32,7 @@ def process(*args)
self[:example_group][:full_description] = full_description_from(*args)
self[:example_group][:block] = user_metadata.delete(:example_group_block)
- self[:example_group][:caller] = user_metadata.delete(:caller) || caller(1)
- self[:example_group][:file_path] = file_path_from(self[:example_group], user_metadata.delete(:file_path))
- self[:example_group][:line_number] = line_number_from(self[:example_group], user_metadata.delete(:line_number))
+ self[:example_group][:file_path], self[:example_group][:line_number] = file_and_line_number_from(caller)
self[:example_group][:location] = location_from(self[:example_group])
update(user_metadata)
@@ -70,11 +68,7 @@ def configure_for_example(description, options)
store(:description, description.to_s)
store(:full_description, "#{self[:example_group][:full_description]} #{self[:description]}")
store(:execution_result, {})
- store(:caller, options.delete(:caller))
- if self[:caller]
- store(:file_path, file_path_from(self))
- store(:line_number, line_number_from(self))
- end
+ self[:file_path], self[:line_number] = file_and_line_number_from(caller)
self[:location] = location_from(self)
update(options)
end
@@ -153,31 +147,19 @@ def described_class_from(*args)
end
end
- def file_path_from(metadata, given_file_path=nil)
- return given_file_path if given_file_path
- file = file_and_line_number(metadata)[0] if file_and_line_number(metadata)
- file.strip if file
+ def file_and_line_number_from(list)
+ entry = first_caller_from_outside_rspec_from_caller(list)
+ entry =~ /(.+?):(\d+)(|:\d+)/
+ return [$1, $2.to_i]
end
- def line_number_from(metadata, given_line_number=nil)
- return given_line_number if given_line_number
- line_number = file_and_line_number(metadata)[1] if file_and_line_number(metadata)
- line_number && line_number.to_i
+ def first_caller_from_outside_rspec_from_caller(list)
+ list.detect {|l| l !~ /\/lib\/rspec\/core/}
end
def location_from(metadata)
"#{metadata[:file_path]}:#{metadata[:line_number]}"
end
-
- def file_and_line_number(metadata)
- entry = first_caller_from_outside_rspec(metadata)
- entry && entry.match(/(.+?):(\d+)(|:\d+)/)[1..2]
- end
-
- def first_caller_from_outside_rspec(metadata)
- metadata[:caller].detect {|l| l !~ /\/lib\/rspec\/core/}
- end
-
end
end
end
View
6 spec/rspec/core/example_group_spec.rb
@@ -186,12 +186,6 @@ module RSpec::Core
ExampleGroup.describe(Object, nil, 'foo' => 'bar') { }.metadata.should include({ "foo" => 'bar' })
end
- it "adds the caller to metadata" do
- ExampleGroup.describe(Object) { }.metadata[:example_group][:caller].any? {|f|
- f =~ /#{__FILE__}/
- }.should be_true
- end
-
it "adds the the file_path to metadata" do
ExampleGroup.describe(Object) { }.metadata[:example_group][:file_path].should == __FILE__
end
View
22 spec/rspec/core/metadata_spec.rb
@@ -149,22 +149,13 @@ module Core
])
m[:example_group][:file_path].should == __FILE__
end
-
- it "finds the first spec file in the caller array with drive letter" do
- m = Metadata.new
- m.process(:caller => [ "C:/path/file_spec.rb:#{__LINE__}" ])
- m[:example_group][:file_path].should == "C:/path/file_spec.rb"
- end
end
describe "line number" do
it "finds the line number with the first non-rspec lib file in the backtrace" do
m = Metadata.new
- m.process(:caller => [
- "./lib/rspec/core/foo.rb",
- "#{__FILE__}:#{__LINE__}",
- ])
- m[:example_group][:line_number].should == __LINE__ - 2
+ m.process({})
+ m[:example_group][:line_number].should == __LINE__ - 1
end
it "finds the line number with the first spec file with drive letter" do
@@ -181,10 +172,9 @@ module Core
end
describe "metadata for example" do
- let(:caller_for_example) { caller(0) }
- let(:line_number) { __LINE__ - 1 }
let(:metadata) { Metadata.new.process("group description") }
- let(:mfe) { metadata.for_example("example description", {:caller => caller_for_example, :arbitrary => :options}) }
+ let(:mfe) { metadata.for_example("example description", {:arbitrary => :options}) }
+ let(:line_number) { __LINE__ - 1 }
it "stores the description" do
mfe[:description].should == "example description"
@@ -198,10 +188,6 @@ module Core
mfe[:execution_result].should == {}
end
- it "stores the caller" do
- mfe[:caller].should == caller_for_example
- end
-
it "extracts file path from caller" do
mfe[:file_path].should == __FILE__
end

0 comments on commit f78ff61

Please sign in to comment.
Something went wrong with that request. Please try again.