From 2eb6e502a31c22bac83611ada5dcfb88f84cb1dd Mon Sep 17 00:00:00 2001 From: Maxime Lapointe Date: Sat, 27 Jun 2020 16:02:39 -0400 Subject: [PATCH] Memoize exception_lines in ExceptionPresenter 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. --- lib/rspec/core/formatters/exception_presenter.rb | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/rspec/core/formatters/exception_presenter.rb b/lib/rspec/core/formatters/exception_presenter.rb index db612e8858..4deecda228 100644 --- a/lib/rspec/core/formatters/exception_presenter.rb +++ b/lib/rspec/core/formatters/exception_presenter.rb @@ -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