Move SectionEndInfo into sectionEnded call in SECTION's destructor

When running `./tests/SelfTest -o /dev/null`, this saves 1272
allocations (437439 -> 437167). In general, this saves multiple
allocations per end of an entered `SECTION`, if the section name
was too long for SSO, because `RunContext::sectionEnded` can then
move the section's name further down the callstack.
This commit is contained in:
Martin Hořeňovský 2023-01-28 13:00:24 +01:00
parent d58491c85a
commit 2403f5620e
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A
7 changed files with 24 additions and 21 deletions

View File

@ -46,8 +46,8 @@ namespace Catch {
virtual bool sectionStarted( SectionInfo const& sectionInfo, virtual bool sectionStarted( SectionInfo const& sectionInfo,
Counts& assertions ) = 0; Counts& assertions ) = 0;
virtual void sectionEnded( SectionEndInfo const& endInfo ) = 0; virtual void sectionEnded( SectionEndInfo&& endInfo ) = 0;
virtual void sectionEndedEarly( SectionEndInfo const& endInfo ) = 0; virtual void sectionEndedEarly( SectionEndInfo&& endInfo ) = 0;
virtual IGeneratorTracker* virtual IGeneratorTracker*
acquireGeneratorTracker( StringRef generatorName, acquireGeneratorTracker( StringRef generatorName,

View File

@ -67,11 +67,11 @@ namespace Catch {
} }
} }
SectionStats::SectionStats( SectionInfo const& _sectionInfo, SectionStats::SectionStats( SectionInfo&& _sectionInfo,
Counts const& _assertions, Counts const& _assertions,
double _durationInSeconds, double _durationInSeconds,
bool _missingAssertions ) bool _missingAssertions )
: sectionInfo( _sectionInfo ), : sectionInfo( CATCH_MOVE(_sectionInfo) ),
assertions( _assertions ), assertions( _assertions ),
durationInSeconds( _durationInSeconds ), durationInSeconds( _durationInSeconds ),
missingAssertions( _missingAssertions ) missingAssertions( _missingAssertions )

View File

@ -78,7 +78,7 @@ namespace Catch {
}; };
struct SectionStats { struct SectionStats {
SectionStats( SectionInfo const& _sectionInfo, SectionStats( SectionInfo&& _sectionInfo,
Counts const& _assertions, Counts const& _assertions,
double _durationInSeconds, double _durationInSeconds,
bool _missingAssertions ); bool _missingAssertions );

View File

@ -356,7 +356,7 @@ namespace Catch {
return true; return true;
} }
void RunContext::sectionEnded(SectionEndInfo const & endInfo) { void RunContext::sectionEnded(SectionEndInfo&& endInfo) {
Counts assertions = m_totals.assertions - endInfo.prevAssertions; Counts assertions = m_totals.assertions - endInfo.prevAssertions;
bool missingAssertions = testForMissingAssertions(assertions); bool missingAssertions = testForMissingAssertions(assertions);
@ -365,19 +365,20 @@ namespace Catch {
m_activeSections.pop_back(); m_activeSections.pop_back();
} }
m_reporter->sectionEnded(SectionStats(endInfo.sectionInfo, assertions, endInfo.durationInSeconds, missingAssertions)); m_reporter->sectionEnded(SectionStats(CATCH_MOVE(endInfo.sectionInfo), assertions, endInfo.durationInSeconds, missingAssertions));
m_messages.clear(); m_messages.clear();
m_messageScopes.clear(); m_messageScopes.clear();
} }
void RunContext::sectionEndedEarly(SectionEndInfo const & endInfo) { void RunContext::sectionEndedEarly(SectionEndInfo&& endInfo) {
if (m_unfinishedSections.empty()) if ( m_unfinishedSections.empty() ) {
m_activeSections.back()->fail(); m_activeSections.back()->fail();
else } else {
m_activeSections.back()->close(); m_activeSections.back()->close();
}
m_activeSections.pop_back(); m_activeSections.pop_back();
m_unfinishedSections.push_back(endInfo); m_unfinishedSections.push_back(CATCH_MOVE(endInfo));
} }
void RunContext::benchmarkPreparing( StringRef name ) { void RunContext::benchmarkPreparing( StringRef name ) {
@ -439,7 +440,7 @@ namespace Catch {
Counts assertions; Counts assertions;
assertions.failed = 1; assertions.failed = 1;
SectionStats testCaseSectionStats(testCaseSection, assertions, 0, false); SectionStats testCaseSectionStats(CATCH_MOVE(testCaseSection), assertions, 0, false);
m_reporter->sectionEnded(testCaseSectionStats); m_reporter->sectionEnded(testCaseSectionStats);
auto const& testInfo = m_activeTestCase->getTestCaseInfo(); auto const& testInfo = m_activeTestCase->getTestCaseInfo();
@ -518,7 +519,7 @@ namespace Catch {
m_messages.clear(); m_messages.clear();
m_messageScopes.clear(); m_messageScopes.clear();
SectionStats testCaseSectionStats(testCaseSection, assertions, duration, missingAssertions); SectionStats testCaseSectionStats(CATCH_MOVE(testCaseSection), assertions, duration, missingAssertions);
m_reporter->sectionEnded(testCaseSectionStats); m_reporter->sectionEnded(testCaseSectionStats);
} }
@ -542,7 +543,7 @@ namespace Catch {
itEnd = m_unfinishedSections.rend(); itEnd = m_unfinishedSections.rend();
it != itEnd; it != itEnd;
++it) ++it)
sectionEnded(*it); sectionEnded(CATCH_MOVE(*it));
m_unfinishedSections.clear(); m_unfinishedSections.clear();
} }

View File

@ -70,8 +70,8 @@ namespace Catch {
bool sectionStarted( SectionInfo const& sectionInfo, Counts& assertions ) override; bool sectionStarted( SectionInfo const& sectionInfo, Counts& assertions ) override;
void sectionEnded( SectionEndInfo const& endInfo ) override; void sectionEnded( SectionEndInfo&& endInfo ) override;
void sectionEndedEarly( SectionEndInfo const& endInfo ) override; void sectionEndedEarly( SectionEndInfo&& endInfo ) override;
IGeneratorTracker* IGeneratorTracker*
acquireGeneratorTracker( StringRef generatorName, acquireGeneratorTracker( StringRef generatorName,

View File

@ -26,10 +26,11 @@ namespace Catch {
Section::~Section() { Section::~Section() {
if( m_sectionIncluded ) { if( m_sectionIncluded ) {
SectionEndInfo endInfo{ CATCH_MOVE(m_info), m_assertions, m_timer.getElapsedSeconds() }; SectionEndInfo endInfo{ CATCH_MOVE(m_info), m_assertions, m_timer.getElapsedSeconds() };
if( uncaught_exceptions() ) if ( uncaught_exceptions() ) {
getResultCapture().sectionEndedEarly( endInfo ); getResultCapture().sectionEndedEarly( CATCH_MOVE(endInfo) );
else } else {
getResultCapture().sectionEnded( endInfo ); getResultCapture().sectionEnded( CATCH_MOVE( endInfo ) );
}
} }
} }

View File

@ -70,7 +70,8 @@ namespace Catch {
void void
CumulativeReporterBase::sectionStarting( SectionInfo const& sectionInfo ) { CumulativeReporterBase::sectionStarting( SectionInfo const& sectionInfo ) {
SectionStats incompleteStats( sectionInfo, Counts(), 0, false ); // We need a copy, because SectionStats expect to take ownership
SectionStats incompleteStats( SectionInfo(sectionInfo), Counts(), 0, false );
SectionNode* node; SectionNode* node;
if ( m_sectionStack.empty() ) { if ( m_sectionStack.empty() ) {
if ( !m_rootSection ) { if ( !m_rootSection ) {