From 442ad5eb0fca2135afbdd4dabca5daaa24d7f79a Mon Sep 17 00:00:00 2001 From: Neal Coombes Date: Wed, 21 Jun 2017 13:34:58 -0500 Subject: [PATCH] Performance improvement in AssertionInfo. By using char const * instead of std::string we avoid significant copying per assertion. In a simple loop with 10000000 CHECKS on my system, this reduces the run time from 9.8s to 5.8s. This change is at the expense of no longer capturing the second arg, which no currently existing test notices. --- include/internal/catch_assertionresult.h | 8 ++++---- include/internal/catch_assertionresult.hpp | 10 +++++----- include/internal/catch_result_builder.hpp | 2 +- include/internal/catch_run_context.hpp | 8 ++++---- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/include/internal/catch_assertionresult.h b/include/internal/catch_assertionresult.h index 22d1355e..ca828502 100644 --- a/include/internal/catch_assertionresult.h +++ b/include/internal/catch_assertionresult.h @@ -40,14 +40,14 @@ namespace Catch { struct AssertionInfo { AssertionInfo() {} - AssertionInfo( std::string const& _macroName, + AssertionInfo( char const * _macroName, SourceLineInfo const& _lineInfo, - std::string const& _capturedExpression, + char const * _capturedExpression, ResultDisposition::Flags _resultDisposition ); - std::string macroName; + char const * macroName; SourceLineInfo lineInfo; - std::string capturedExpression; + char const * capturedExpression; ResultDisposition::Flags resultDisposition; }; diff --git a/include/internal/catch_assertionresult.hpp b/include/internal/catch_assertionresult.hpp index 9b637028..bb8502f2 100644 --- a/include/internal/catch_assertionresult.hpp +++ b/include/internal/catch_assertionresult.hpp @@ -13,9 +13,9 @@ namespace Catch { - AssertionInfo::AssertionInfo( std::string const& _macroName, + AssertionInfo::AssertionInfo( char const * _macroName, SourceLineInfo const& _lineInfo, - std::string const& _capturedExpression, + char const * _capturedExpression, ResultDisposition::Flags _resultDisposition ) : macroName( _macroName ), lineInfo( _lineInfo ), @@ -47,7 +47,7 @@ namespace Catch { } bool AssertionResult::hasExpression() const { - return !m_info.capturedExpression.empty(); + return m_info.capturedExpression[0] != 0; } bool AssertionResult::hasMessage() const { @@ -61,10 +61,10 @@ namespace Catch { return m_info.capturedExpression; } std::string AssertionResult::getExpressionInMacro() const { - if( m_info.macroName.empty() ) + if( m_info.macroName[0] == 0 ) return m_info.capturedExpression; else - return m_info.macroName + "( " + m_info.capturedExpression + " )"; + return std::string(m_info.macroName) + "( " + m_info.capturedExpression + " )"; } bool AssertionResult::hasExpandedExpression() const { diff --git a/include/internal/catch_result_builder.hpp b/include/internal/catch_result_builder.hpp index 2874c568..55d20cb1 100644 --- a/include/internal/catch_result_builder.hpp +++ b/include/internal/catch_result_builder.hpp @@ -28,7 +28,7 @@ namespace Catch { char const* capturedExpression, ResultDisposition::Flags resultDisposition, char const* secondArg ) - : m_assertionInfo( macroName, lineInfo, capturedExpressionWithSecondArgument( capturedExpression, secondArg ), resultDisposition ), + : m_assertionInfo( macroName, lineInfo, capturedExpression, resultDisposition ), m_shouldDebugBreak( false ), m_shouldThrow( false ), m_guardException( false ) diff --git a/include/internal/catch_run_context.hpp b/include/internal/catch_run_context.hpp index 572b54d1..b3dee17f 100644 --- a/include/internal/catch_run_context.hpp +++ b/include/internal/catch_run_context.hpp @@ -150,7 +150,7 @@ namespace Catch { static_cast(m_reporter->assertionEnded(AssertionStats(result, m_messages, m_totals))); // Reset working state - m_lastAssertionInfo = AssertionInfo( std::string(), m_lastAssertionInfo.lineInfo, "{Unknown expression after the reported line}" , m_lastAssertionInfo.resultDisposition ); + m_lastAssertionInfo = AssertionInfo( "", m_lastAssertionInfo.lineInfo, "{Unknown expression after the reported line}" , m_lastAssertionInfo.resultDisposition ); m_lastResult = result; } @@ -280,7 +280,7 @@ namespace Catch { double duration = 0; m_shouldReportUnexpected = true; try { - m_lastAssertionInfo = AssertionInfo( "TEST_CASE", testCaseInfo.lineInfo, std::string(), ResultDisposition::Normal ); + m_lastAssertionInfo = AssertionInfo( "TEST_CASE", testCaseInfo.lineInfo, "", ResultDisposition::Normal ); seedRng( *m_config ); @@ -332,9 +332,9 @@ namespace Catch { private: ResultBuilder makeUnexpectedResultBuilder() const { - return ResultBuilder( m_lastAssertionInfo.macroName.c_str(), + return ResultBuilder( m_lastAssertionInfo.macroName, m_lastAssertionInfo.lineInfo, - m_lastAssertionInfo.capturedExpression.c_str(), + m_lastAssertionInfo.capturedExpression, m_lastAssertionInfo.resultDisposition ); }