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.
This commit is contained in:
Martin Hořeňovský
2025-07-21 19:45:43 +02:00
parent 9c5c21df82
commit 5aa8d11321
2 changed files with 11 additions and 3 deletions

View File

@@ -296,6 +296,10 @@ namespace Catch {
} }
{ {
if ( m_clearMessageScopes ) {
m_messageScopes.clear();
m_clearMessageScopes = false;
}
auto _ = scopedDeactivate( *m_outputRedirect ); auto _ = scopedDeactivate( *m_outputRedirect );
m_reporter->assertionEnded( AssertionStats( result, m_messages, m_totals ) ); m_reporter->assertionEnded( AssertionStats( result, m_messages, m_totals ) );
} }
@@ -524,9 +528,9 @@ namespace Catch {
void RunContext::assertionPassedFastPath(SourceLineInfo lineInfo) { void RunContext::assertionPassedFastPath(SourceLineInfo lineInfo) {
m_lastKnownLineInfo = lineInfo; m_lastKnownLineInfo = lineInfo;
m_lastAssertionPassed = true;
++m_totals.assertions.passed; ++m_totals.assertions.passed;
m_messageScopes.clear(); m_lastAssertionPassed = true;
m_clearMessageScopes = true;
} }
bool RunContext::aborting() const { bool RunContext::aborting() const {

View File

@@ -148,7 +148,8 @@ namespace Catch {
Totals m_totals; Totals m_totals;
IEventListenerPtr m_reporter; IEventListenerPtr m_reporter;
std::vector<MessageInfo> m_messages; std::vector<MessageInfo> m_messages;
std::vector<ScopedMessage> m_messageScopes; /* Keeps owners of so-called unscoped messages. */ // Owners for the UNSCOPED_X information macro
std::vector<ScopedMessage> m_messageScopes;
SourceLineInfo m_lastKnownLineInfo; SourceLineInfo m_lastKnownLineInfo;
std::vector<SectionEndInfo> m_unfinishedSections; std::vector<SectionEndInfo> m_unfinishedSections;
std::vector<ITracker*> m_activeSections; std::vector<ITracker*> m_activeSections;
@@ -158,6 +159,9 @@ namespace Catch {
// Caches m_config->abortAfter() to avoid vptr calls/allow inlining // Caches m_config->abortAfter() to avoid vptr calls/allow inlining
size_t m_abortAfterXFailedAssertions; size_t m_abortAfterXFailedAssertions;
bool m_lastAssertionPassed = false; bool m_lastAssertionPassed = false;
// Should we clear message scopes before sending off the messages to reporter?
// Set in `assertionPassedFastPath` to avoid doing the full clear there.
bool m_clearMessageScopes = false;
bool m_shouldReportUnexpected = true; bool m_shouldReportUnexpected = true;
// Caches whether `assertionStarting` events should be sent to the reporter. // Caches whether `assertionStarting` events should be sent to the reporter.
bool m_reportAssertionStarting; bool m_reportAssertionStarting;