Let reporters opt into fast path in RunContext::assertionStarting

The fast path allows `RunContext` to skip disabling output redirect,
and notifying the reporters, turning `RunContext::notifyAssertionStarted`
into a no-op. This improves the overall performance of assertion handling
significantly, and also prepares ground for future changes around
assertion handling and thread safety.

For simple 10M assertion run, this improves the running time by ~30%
in Debug build and ~40% in Release build.

For backwards-compatibility reasons, the fast path is disabled
by default. However, none of the first party reporters use the
`assertionStarting` event, so all first party reporters are opted-in.
This commit is contained in:
Martin Hořeňovský 2025-07-17 13:13:08 +02:00
parent ae33e5b99a
commit c22096846c
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A
15 changed files with 41 additions and 7 deletions

View File

@ -115,6 +115,11 @@ namespace Catch {
//! Catch2 should call `Reporter::assertionEnded` even for passing //! Catch2 should call `Reporter::assertionEnded` even for passing
//! assertions //! assertions
bool shouldReportAllAssertions = false; bool shouldReportAllAssertions = false;
//! Catch2 should call `Reporter::assertionStarting` for all assertions
// Defaults to true for backwards compatibility, but none of our current
// reporters actually want this, and it enables a fast path in assertion
// handling.
bool shouldReportAllAssertionStarts = true;
}; };
/** /**

View File

@ -172,6 +172,7 @@ namespace Catch {
m_lastKnownLineInfo("DummyLocation", static_cast<size_t>(-1)), m_lastKnownLineInfo("DummyLocation", static_cast<size_t>(-1)),
m_outputRedirect( makeOutputRedirect( m_reporter->getPreferences().shouldRedirectStdOut ) ), m_outputRedirect( makeOutputRedirect( m_reporter->getPreferences().shouldRedirectStdOut ) ),
m_abortAfterXFailedAssertions( m_config->abortAfter() ), m_abortAfterXFailedAssertions( m_config->abortAfter() ),
m_reportAssertionStarting( m_reporter->getPreferences().shouldReportAllAssertionStarts ),
m_includeSuccessfulResults( m_config->includeSuccessfulResults() || m_reporter->getPreferences().shouldReportAllAssertions ), m_includeSuccessfulResults( m_config->includeSuccessfulResults() || m_reporter->getPreferences().shouldReportAllAssertions ),
m_shouldDebugBreak( m_config->shouldDebugBreak() ) m_shouldDebugBreak( m_config->shouldDebugBreak() )
{ {
@ -309,8 +310,10 @@ namespace Catch {
} }
void RunContext::notifyAssertionStarted( AssertionInfo const& info ) { void RunContext::notifyAssertionStarted( AssertionInfo const& info ) {
auto _ = scopedDeactivate( *m_outputRedirect ); if (m_reportAssertionStarting) {
m_reporter->assertionStarting( info ); auto _ = scopedDeactivate( *m_outputRedirect );
m_reporter->assertionStarting( info );
}
} }
bool RunContext::sectionStarted( StringRef sectionName, bool RunContext::sectionStarted( StringRef sectionName,

View File

@ -159,6 +159,9 @@ namespace Catch {
size_t m_abortAfterXFailedAssertions; size_t m_abortAfterXFailedAssertions;
bool m_lastAssertionPassed = false; bool m_lastAssertionPassed = false;
bool m_shouldReportUnexpected = true; bool m_shouldReportUnexpected = true;
// Caches whether `assertionStarting` events should be sent to the reporter.
bool m_reportAssertionStarting;
// Caches whether `assertionEnded` events for successful assertions should be sent to the reporter
bool m_includeSuccessfulResults; bool m_includeSuccessfulResults;
// Caches m_config->shouldDebugBreak() to avoid vptr calls/allow inlining // Caches m_config->shouldDebugBreak() to avoid vptr calls/allow inlining
bool m_shouldDebugBreak; bool m_shouldDebugBreak;

View File

@ -19,9 +19,11 @@ namespace Catch {
public: public:
// GCC5 compat: we cannot use inherited constructor, because it // GCC5 compat: we cannot use inherited constructor, because it
// doesn't implement backport of P0136 // doesn't implement backport of P0136
AutomakeReporter(ReporterConfig&& _config): AutomakeReporter( ReporterConfig&& _config ):
StreamingReporterBase(CATCH_MOVE(_config)) StreamingReporterBase( CATCH_MOVE( _config ) ) {
{} m_preferences.shouldReportAllAssertionStarts = false;
}
~AutomakeReporter() override; ~AutomakeReporter() override;
static std::string getDescription() { static std::string getDescription() {

View File

@ -16,7 +16,10 @@ namespace Catch {
class CompactReporter final : public StreamingReporterBase { class CompactReporter final : public StreamingReporterBase {
public: public:
using StreamingReporterBase::StreamingReporterBase; CompactReporter( ReporterConfig&& _config ):
StreamingReporterBase( CATCH_MOVE( _config ) ) {
m_preferences.shouldReportAllAssertionStarts = false;
}
~CompactReporter() override; ~CompactReporter() override;

View File

@ -401,7 +401,10 @@ ConsoleReporter::ConsoleReporter(ReporterConfig&& config):
{ "est run time high mean high std dev", 14, Justification::Right } { "est run time high mean high std dev", 14, Justification::Right }
}; };
} }
}())) {} }())) {
m_preferences.shouldReportAllAssertionStarts = false;
}
ConsoleReporter::~ConsoleReporter() = default; ConsoleReporter::~ConsoleReporter() = default;
std::string ConsoleReporter::getDescription() { std::string ConsoleReporter::getDescription() {

View File

@ -51,6 +51,8 @@ namespace Catch {
// not, but for machine-parseable reporters I think the answer // not, but for machine-parseable reporters I think the answer
// should be yes. // should be yes.
m_preferences.shouldReportAllAssertions = true; m_preferences.shouldReportAllAssertions = true;
// We only handle assertions when they end
m_preferences.shouldReportAllAssertionStarts = false;
m_objectWriters.emplace( m_stream ); m_objectWriters.emplace( m_stream );
m_writers.emplace( Writer::Object ); m_writers.emplace( Writer::Object );

View File

@ -89,6 +89,7 @@ namespace Catch {
{ {
m_preferences.shouldRedirectStdOut = true; m_preferences.shouldRedirectStdOut = true;
m_preferences.shouldReportAllAssertions = false; m_preferences.shouldReportAllAssertions = false;
m_preferences.shouldReportAllAssertionStarts = false;
m_shouldStoreSuccesfulAssertions = false; m_shouldStoreSuccesfulAssertions = false;
} }

View File

@ -19,6 +19,8 @@ namespace Catch {
reporterish.getPreferences().shouldRedirectStdOut; reporterish.getPreferences().shouldRedirectStdOut;
m_preferences.shouldReportAllAssertions |= m_preferences.shouldReportAllAssertions |=
reporterish.getPreferences().shouldReportAllAssertions; reporterish.getPreferences().shouldReportAllAssertions;
m_preferences.shouldReportAllAssertionStarts |=
reporterish.getPreferences().shouldReportAllAssertionStarts;
} }
void MultiReporter::addListener( IEventListenerPtr&& listener ) { void MultiReporter::addListener( IEventListenerPtr&& listener ) {

View File

@ -29,6 +29,11 @@ namespace Catch {
void updatePreferences(IEventListener const& reporterish); void updatePreferences(IEventListener const& reporterish);
public: public:
MultiReporter( IConfig const* config ):
IEventListener( config ) {
m_preferences.shouldReportAllAssertionStarts = false;
}
using IEventListener::IEventListener; using IEventListener::IEventListener;
void addListener( IEventListenerPtr&& listener ); void addListener( IEventListenerPtr&& listener );

View File

@ -22,6 +22,7 @@ namespace Catch {
, xml(m_stream) { , xml(m_stream) {
m_preferences.shouldRedirectStdOut = true; m_preferences.shouldRedirectStdOut = true;
m_preferences.shouldReportAllAssertions = false; m_preferences.shouldReportAllAssertions = false;
m_preferences.shouldReportAllAssertionStarts = false;
m_shouldStoreSuccesfulAssertions = false; m_shouldStoreSuccesfulAssertions = false;
} }

View File

@ -18,6 +18,7 @@ namespace Catch {
TAPReporter( ReporterConfig&& config ): TAPReporter( ReporterConfig&& config ):
StreamingReporterBase( CATCH_MOVE(config) ) { StreamingReporterBase( CATCH_MOVE(config) ) {
m_preferences.shouldReportAllAssertions = true; m_preferences.shouldReportAllAssertions = true;
m_preferences.shouldReportAllAssertionStarts = false;
} }
static std::string getDescription() { static std::string getDescription() {

View File

@ -26,6 +26,7 @@ namespace Catch {
: StreamingReporterBase( CATCH_MOVE(_config) ) : StreamingReporterBase( CATCH_MOVE(_config) )
{ {
m_preferences.shouldRedirectStdOut = true; m_preferences.shouldRedirectStdOut = true;
m_preferences.shouldReportAllAssertionStarts = false;
} }
~TeamCityReporter() override; ~TeamCityReporter() override;

View File

@ -30,6 +30,7 @@ namespace Catch {
{ {
m_preferences.shouldRedirectStdOut = true; m_preferences.shouldRedirectStdOut = true;
m_preferences.shouldReportAllAssertions = true; m_preferences.shouldReportAllAssertions = true;
m_preferences.shouldReportAllAssertionStarts = false;
} }
XmlReporter::~XmlReporter() = default; XmlReporter::~XmlReporter() = default;

View File

@ -54,6 +54,7 @@ public:
ValidatingTestListener(Catch::IConfig const* config) : ValidatingTestListener(Catch::IConfig const* config) :
EventListenerBase(config) { EventListenerBase(config) {
m_preferences.shouldReportAllAssertions = true; m_preferences.shouldReportAllAssertions = true;
m_preferences.shouldReportAllAssertionStarts = true;
} }
void testRunStarting( Catch::TestRunInfo const& ) override { void testRunStarting( Catch::TestRunInfo const& ) override {