Skip to content

Commit 5aa8d11

Browse files
committed
Clear unscoped messages lazily
This improves the fast path performance for successful assertions by about 7%, at the cost of potentially keeping around the message allocation longer.
1 parent 9c5c21d commit 5aa8d11

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

src/catch2/internal/catch_run_context.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,10 @@ namespace Catch {
296296
}
297297

298298
{
299+
if ( m_clearMessageScopes ) {
300+
m_messageScopes.clear();
301+
m_clearMessageScopes = false;
302+
}
299303
auto _ = scopedDeactivate( *m_outputRedirect );
300304
m_reporter->assertionEnded( AssertionStats( result, m_messages, m_totals ) );
301305
}
@@ -524,9 +528,9 @@ namespace Catch {
524528

525529
void RunContext::assertionPassedFastPath(SourceLineInfo lineInfo) {
526530
m_lastKnownLineInfo = lineInfo;
527-
m_lastAssertionPassed = true;
528531
++m_totals.assertions.passed;
529-
m_messageScopes.clear();
532+
m_lastAssertionPassed = true;
533+
m_clearMessageScopes = true;
530534
}
531535

532536
bool RunContext::aborting() const {

src/catch2/internal/catch_run_context.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ namespace Catch {
148148
Totals m_totals;
149149
IEventListenerPtr m_reporter;
150150
std::vector<MessageInfo> m_messages;
151-
std::vector<ScopedMessage> m_messageScopes; /* Keeps owners of so-called unscoped messages. */
151+
// Owners for the UNSCOPED_X information macro
152+
std::vector<ScopedMessage> m_messageScopes;
152153
SourceLineInfo m_lastKnownLineInfo;
153154
std::vector<SectionEndInfo> m_unfinishedSections;
154155
std::vector<ITracker*> m_activeSections;
@@ -158,6 +159,9 @@ namespace Catch {
158159
// Caches m_config->abortAfter() to avoid vptr calls/allow inlining
159160
size_t m_abortAfterXFailedAssertions;
160161
bool m_lastAssertionPassed = false;
162+
// Should we clear message scopes before sending off the messages to reporter?
163+
// Set in `assertionPassedFastPath` to avoid doing the full clear there.
164+
bool m_clearMessageScopes = false;
161165
bool m_shouldReportUnexpected = true;
162166
// Caches whether `assertionStarting` events should be sent to the reporter.
163167
bool m_reportAssertionStarting;

0 commit comments

Comments
 (0)