Skip to content

Commit

Permalink
Memoize exception_lines in ExceptionPresenter
Browse files Browse the repository at this point in the history
The method failure_lines uses exception_lines 3 times, which uses exception.message.
In some situation, exception.message can be slow (when inspect is called on a big object for example).
Without caching, this delay which can become considerable will be amplified by 3x.
  • Loading branch information
MaxLap committed Jun 29, 2020
1 parent 77455dd commit 2eb6e50
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions lib/rspec/core/formatters/exception_presenter.rb
Expand Up @@ -175,12 +175,14 @@ def failure_slash_error_lines
end

def exception_lines
lines = []
lines << "#{exception_class_name}:" unless exception_class_name =~ /RSpec/
encoded_string(exception.message.to_s).split("\n").each do |line|
lines << (line.empty? ? line : " #{line}")
@exception_lines ||= begin
lines = []
lines << "#{exception_class_name}:" unless exception_class_name =~ /RSpec/
encoded_string(exception.message.to_s).split("\n").each do |line|
lines << (line.empty? ? line : " #{line}")
end
lines
end
lines
end

def extra_failure_lines
Expand Down

0 comments on commit 2eb6e50

Please sign in to comment.