Cleanup unneeded allocations from reporters

The CompactReporter changes save 21 (430764 -> 430743) allocations
when running the SelfTest binary in default configuration. They
save about 500 allocations when running the binary with `-s`.
This commit is contained in:
Martin Hořeňovský 2023-01-30 12:35:44 +01:00
parent 2ab20a0e00
commit 388f7e1737
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A
4 changed files with 39 additions and 36 deletions

View File

@ -192,7 +192,7 @@ private:
private: private:
std::ostream& stream; std::ostream& stream;
AssertionResult const& result; AssertionResult const& result;
std::vector<MessageInfo> messages; std::vector<MessageInfo> const& messages;
std::vector<MessageInfo>::const_iterator itMessage; std::vector<MessageInfo>::const_iterator itMessage;
bool printInfoMessages; bool printInfoMessages;
ColourImpl* colourImpl; ColourImpl* colourImpl;

View File

@ -51,7 +51,6 @@ public:
stats(_stats), stats(_stats),
result(_stats.assertionResult), result(_stats.assertionResult),
colour(Colour::None), colour(Colour::None),
message(result.getMessage()),
messages(_stats.infoMessages), messages(_stats.infoMessages),
colourImpl(colourImpl_), colourImpl(colourImpl_),
printInfoMessages(_printInfoMessages) { printInfoMessages(_printInfoMessages) {
@ -60,10 +59,10 @@ public:
colour = Colour::Success; colour = Colour::Success;
passOrFail = "PASSED"_sr; passOrFail = "PASSED"_sr;
//if( result.hasMessage() ) //if( result.hasMessage() )
if (_stats.infoMessages.size() == 1) if (messages.size() == 1)
messageLabel = "with message"; messageLabel = "with message"_sr;
if (_stats.infoMessages.size() > 1) if (messages.size() > 1)
messageLabel = "with messages"; messageLabel = "with messages"_sr;
break; break;
case ResultWas::ExpressionFailed: case ResultWas::ExpressionFailed:
if (result.isOk()) { if (result.isOk()) {
@ -73,51 +72,57 @@ public:
colour = Colour::Error; colour = Colour::Error;
passOrFail = "FAILED"_sr; passOrFail = "FAILED"_sr;
} }
if (_stats.infoMessages.size() == 1) if (messages.size() == 1)
messageLabel = "with message"; messageLabel = "with message"_sr;
if (_stats.infoMessages.size() > 1) if (messages.size() > 1)
messageLabel = "with messages"; messageLabel = "with messages"_sr;
break; break;
case ResultWas::ThrewException: case ResultWas::ThrewException:
colour = Colour::Error; colour = Colour::Error;
passOrFail = "FAILED"_sr; passOrFail = "FAILED"_sr;
messageLabel = "due to unexpected exception with "; // todo switch
if (_stats.infoMessages.size() == 1) switch (messages.size()) { case 0:
messageLabel += "message"; messageLabel = "due to unexpected exception with "_sr;
if (_stats.infoMessages.size() > 1) break;
messageLabel += "messages"; case 1:
messageLabel = "due to unexpected exception with message"_sr;
break;
default:
messageLabel = "due to unexpected exception with messages"_sr;
break;
}
break; break;
case ResultWas::FatalErrorCondition: case ResultWas::FatalErrorCondition:
colour = Colour::Error; colour = Colour::Error;
passOrFail = "FAILED"_sr; passOrFail = "FAILED"_sr;
messageLabel = "due to a fatal error condition"; messageLabel = "due to a fatal error condition"_sr;
break; break;
case ResultWas::DidntThrowException: case ResultWas::DidntThrowException:
colour = Colour::Error; colour = Colour::Error;
passOrFail = "FAILED"_sr; passOrFail = "FAILED"_sr;
messageLabel = "because no exception was thrown where one was expected"; messageLabel = "because no exception was thrown where one was expected"_sr;
break; break;
case ResultWas::Info: case ResultWas::Info:
messageLabel = "info"; messageLabel = "info"_sr;
break; break;
case ResultWas::Warning: case ResultWas::Warning:
messageLabel = "warning"; messageLabel = "warning"_sr;
break; break;
case ResultWas::ExplicitFailure: case ResultWas::ExplicitFailure:
passOrFail = "FAILED"_sr; passOrFail = "FAILED"_sr;
colour = Colour::Error; colour = Colour::Error;
if (_stats.infoMessages.size() == 1) if (messages.size() == 1)
messageLabel = "explicitly with message"; messageLabel = "explicitly with message"_sr;
if (_stats.infoMessages.size() > 1) if (messages.size() > 1)
messageLabel = "explicitly with messages"; messageLabel = "explicitly with messages"_sr;
break; break;
case ResultWas::ExplicitSkip: case ResultWas::ExplicitSkip:
colour = Colour::Skip; colour = Colour::Skip;
passOrFail = "SKIPPED"_sr; passOrFail = "SKIPPED"_sr;
if (_stats.infoMessages.size() == 1) if (messages.size() == 1)
messageLabel = "explicitly with message"; messageLabel = "explicitly with message"_sr;
if (_stats.infoMessages.size() > 1) if (messages.size() > 1)
messageLabel = "explicitly with messages"; messageLabel = "explicitly with messages"_sr;
break; break;
// These cases are here to prevent compiler warnings // These cases are here to prevent compiler warnings
case ResultWas::Unknown: case ResultWas::Unknown:
@ -181,9 +186,8 @@ private:
AssertionResult const& result; AssertionResult const& result;
Colour::Code colour; Colour::Code colour;
StringRef passOrFail; StringRef passOrFail;
std::string messageLabel; StringRef messageLabel;
std::string message; std::vector<MessageInfo> const& messages;
std::vector<MessageInfo> messages;
ColourImpl* colourImpl; ColourImpl* colourImpl;
bool printInfoMessages; bool printInfoMessages;
}; };

View File

@ -184,7 +184,7 @@ namespace Catch {
private: private:
std::ostream& stream; std::ostream& stream;
AssertionResult const& result; AssertionResult const& result;
std::vector<MessageInfo> messages; std::vector<MessageInfo> const& messages;
std::vector<MessageInfo>::const_iterator itMessage; std::vector<MessageInfo>::const_iterator itMessage;
bool printInfoMessages; bool printInfoMessages;
std::size_t counter; std::size_t counter;

View File

@ -66,7 +66,7 @@ namespace Catch {
void XmlReporter::testCaseStarting( TestCaseInfo const& testInfo ) { void XmlReporter::testCaseStarting( TestCaseInfo const& testInfo ) {
StreamingReporterBase::testCaseStarting(testInfo); StreamingReporterBase::testCaseStarting(testInfo);
m_xml.startElement( "TestCase" ) m_xml.startElement( "TestCase" )
.writeAttribute( "name"_sr, trim( testInfo.name ) ) .writeAttribute( "name"_sr, trim( StringRef(testInfo.name) ) )
.writeAttribute( "tags"_sr, testInfo.tagsAsString() ); .writeAttribute( "tags"_sr, testInfo.tagsAsString() );
writeSourceInfo( testInfo.lineInfo ); writeSourceInfo( testInfo.lineInfo );
@ -80,7 +80,7 @@ namespace Catch {
StreamingReporterBase::sectionStarting( sectionInfo ); StreamingReporterBase::sectionStarting( sectionInfo );
if( m_sectionDepth++ > 0 ) { if( m_sectionDepth++ > 0 ) {
m_xml.startElement( "Section" ) m_xml.startElement( "Section" )
.writeAttribute( "name"_sr, trim( sectionInfo.name ) ); .writeAttribute( "name"_sr, trim( StringRef(sectionInfo.name) ) );
writeSourceInfo( sectionInfo.lineInfo ); writeSourceInfo( sectionInfo.lineInfo );
m_xml.ensureTagClosed(); m_xml.ensureTagClosed();
} }
@ -194,11 +194,10 @@ namespace Catch {
if ( m_config->showDurations() == ShowDurations::Always ) if ( m_config->showDurations() == ShowDurations::Always )
e.writeAttribute( "durationInSeconds"_sr, m_testCaseTimer.getElapsedSeconds() ); e.writeAttribute( "durationInSeconds"_sr, m_testCaseTimer.getElapsedSeconds() );
if( !testCaseStats.stdOut.empty() ) if( !testCaseStats.stdOut.empty() )
m_xml.scopedElement( "StdOut" ).writeText( trim( testCaseStats.stdOut ), XmlFormatting::Newline ); m_xml.scopedElement( "StdOut" ).writeText( trim( StringRef(testCaseStats.stdOut) ), XmlFormatting::Newline );
if( !testCaseStats.stdErr.empty() ) if( !testCaseStats.stdErr.empty() )
m_xml.scopedElement( "StdErr" ).writeText( trim( testCaseStats.stdErr ), XmlFormatting::Newline ); m_xml.scopedElement( "StdErr" ).writeText( trim( StringRef(testCaseStats.stdErr) ), XmlFormatting::Newline );
m_xml.endElement(); m_xml.endElement();
} }