Clean up extraneous copies in Messages

This removes 109 allocations from running `tests/SelfTest`
(437057 -> 436948).
This commit is contained in:
Martin Hořeňovský 2023-01-28 22:01:34 +01:00
parent 356dfc1439
commit 906552f8c8
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A
6 changed files with 11 additions and 13 deletions

View File

@ -19,8 +19,8 @@ namespace Catch {
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
ScopedMessage::ScopedMessage( MessageBuilder const& builder ): ScopedMessage::ScopedMessage( MessageBuilder&& builder ):
m_info( builder.m_info ) { m_info( CATCH_MOVE(builder.m_info) ) {
m_info.message = builder.m_stream.str(); m_info.message = builder.m_stream.str();
getResultCapture().pushScopedMessage( m_info ); getResultCapture().pushScopedMessage( m_info );
} }

View File

@ -39,11 +39,10 @@ namespace Catch {
ResultWas::OfType type ): ResultWas::OfType type ):
m_info(macroName, lineInfo, type) {} m_info(macroName, lineInfo, type) {}
template<typename T> template<typename T>
MessageBuilder& operator << ( T const& value ) { MessageBuilder&& operator << ( T const& value ) && {
m_stream << value; m_stream << value;
return *this; return CATCH_MOVE(*this);
} }
MessageInfo m_info; MessageInfo m_info;
@ -51,7 +50,7 @@ namespace Catch {
class ScopedMessage { class ScopedMessage {
public: public:
explicit ScopedMessage( MessageBuilder const& builder ); explicit ScopedMessage( MessageBuilder&& builder );
ScopedMessage( ScopedMessage& duplicate ) = delete; ScopedMessage( ScopedMessage& duplicate ) = delete;
ScopedMessage( ScopedMessage&& old ) noexcept; ScopedMessage( ScopedMessage&& old ) noexcept;
~ScopedMessage(); ~ScopedMessage();

View File

@ -65,7 +65,7 @@ namespace Catch {
virtual void pushScopedMessage( MessageInfo const& message ) = 0; virtual void pushScopedMessage( MessageInfo const& message ) = 0;
virtual void popScopedMessage( MessageInfo const& message ) = 0; virtual void popScopedMessage( MessageInfo const& message ) = 0;
virtual void emplaceUnscopedMessage( MessageBuilder const& builder ) = 0; virtual void emplaceUnscopedMessage( MessageBuilder&& builder ) = 0;
virtual void handleFatalErrorCondition( StringRef message ) = 0; virtual void handleFatalErrorCondition( StringRef message ) = 0;

View File

@ -60,10 +60,9 @@ namespace Catch {
// Copy message into messages list. // Copy message into messages list.
// !TBD This should have been done earlier, somewhere // !TBD This should have been done earlier, somewhere
MessageBuilder builder( assertionResult.getTestMacroName(), assertionResult.getSourceInfo(), assertionResult.getResultType() ); MessageBuilder builder( assertionResult.getTestMacroName(), assertionResult.getSourceInfo(), assertionResult.getResultType() );
builder << assertionResult.getMessage(); builder.m_info.message = static_cast<std::string>(assertionResult.getMessage());
builder.m_info.message = builder.m_stream.str();
infoMessages.push_back( builder.m_info ); infoMessages.push_back( CATCH_MOVE(builder.m_info) );
} }
} }

View File

@ -402,8 +402,8 @@ namespace Catch {
m_messages.erase(std::remove(m_messages.begin(), m_messages.end(), message), m_messages.end()); m_messages.erase(std::remove(m_messages.begin(), m_messages.end(), message), m_messages.end());
} }
void RunContext::emplaceUnscopedMessage( MessageBuilder const& builder ) { void RunContext::emplaceUnscopedMessage( MessageBuilder&& builder ) {
m_messageScopes.emplace_back( builder ); m_messageScopes.emplace_back( CATCH_MOVE(builder) );
} }
std::string RunContext::getCurrentTestName() const { std::string RunContext::getCurrentTestName() const {

View File

@ -90,7 +90,7 @@ namespace Catch {
void pushScopedMessage( MessageInfo const& message ) override; void pushScopedMessage( MessageInfo const& message ) override;
void popScopedMessage( MessageInfo const& message ) override; void popScopedMessage( MessageInfo const& message ) override;
void emplaceUnscopedMessage( MessageBuilder const& builder ) override; void emplaceUnscopedMessage( MessageBuilder&& builder ) override;
std::string getCurrentTestName() const override; std::string getCurrentTestName() const override;