Introduced ReusableStringStream and removed all uses of std::ostringstream from the main path

ReusableStringStream holds a std::ostringstream internally, but only exposes the ostream interface.
It caches a pool of ostringstreams in a vector which is currently global, but will be made thread-local.

Altogether this should enable both runtime and compile-time benefits. although more work is needed to realise the compile time opportunities.
This commit is contained in:
Phil Nash
2017-11-07 18:01:10 +00:00
parent 868e125d49
commit 56e1075613
26 changed files with 202 additions and 114 deletions

View File

@@ -8,16 +8,15 @@
#ifndef TWOBLUECUBES_CATCH_REPORTER_BASES_HPP_INCLUDED
#define TWOBLUECUBES_CATCH_REPORTER_BASES_HPP_INCLUDED
#include "../internal/catch_enforce.h"
#include "../internal/catch_interfaces_reporter.h"
#include <algorithm>
#include <cstring>
#include <cfloat>
#include <cstdio>
#include <assert.h>
#include <memory>
#include <ostream>
namespace Catch {
void prepareExpandedExpression(AssertionResult& result);
@@ -33,7 +32,8 @@ namespace Catch {
stream( _config.stream() )
{
m_reporterPrefs.shouldRedirectStdOut = false;
CATCH_ENFORCE( DerivedT::getSupportedVerbosities().count( m_config->verbosity() ), "Verbosity level not supported by this reporter" );
if( !DerivedT::getSupportedVerbosities().count( m_config->verbosity() ) )
throw std::domain_error( "Verbosity level not supported by this reporter" );
}
ReporterPreferences getPreferences() const override {
@@ -147,7 +147,8 @@ namespace Catch {
stream( _config.stream() )
{
m_reporterPrefs.shouldRedirectStdOut = false;
CATCH_ENFORCE( DerivedT::getSupportedVerbosities().count( m_config->verbosity() ), "Verbosity level not supported by this reporter" );
if( !DerivedT::getSupportedVerbosities().count( m_config->verbosity() ) )
throw std::domain_error( "Verbosity level not supported by this reporter" );
}
~CumulativeReporterBase() override = default;

View File

@@ -543,9 +543,9 @@ namespace Catch {
colour( _colour )
{}
SummaryColumn addRow( std::size_t count ) {
std::ostringstream oss;
oss << count;
std::string row = oss.str();
ReusableStringStream rss;
rss << count;
std::string row = rss.str();
for( auto& oldRow : rows ) {
while( oldRow.size() < row.size() )
oldRow = ' ' + oldRow;

View File

@@ -14,7 +14,7 @@
#include "../internal/catch_timer.h"
#include <assert.h>
#include <sstream>
#include <ctime>
#include <algorithm>
@@ -230,15 +230,15 @@ namespace Catch {
xml.writeAttribute( "message", result.getExpandedExpression() );
xml.writeAttribute( "type", result.getTestMacroName() );
std::ostringstream oss;
ReusableStringStream rss;
if( !result.getMessage().empty() )
oss << result.getMessage() << '\n';
rss << result.getMessage() << '\n';
for( auto const& msg : stats.infoMessages )
if( msg.type == ResultWas::Info )
oss << msg.message << '\n';
rss << msg.message << '\n';
oss << "at " << result.getSourceInfo();
xml.writeText( oss.str(), false );
rss << "at " << result.getSourceInfo();
xml.writeText( rss.str(), false );
}
}

View File

@@ -14,6 +14,8 @@
// file can be distributed as a single header that works with the main
// Catch single header.
#include "../internal/catch_enforce.h"
#include <cstring>
#ifdef __clang__
@@ -69,9 +71,9 @@ namespace Catch {
AssertionResult const& result = assertionStats.assertionResult;
if( !result.isOk() ) {
std::ostringstream msg;
ReusableStringStream msg;
if( !m_headerPrintedForThisSection )
printSectionHeader( msg );
printSectionHeader( msg.get() );
m_headerPrintedForThisSection = true;
msg << result.getSourceInfo() << "\n";