Progmasoft Git

catch3

Progmasoft Catch3: an independently maintained C++17 test framework with a parallel runner, property checks, snapshots and structured results.

Clonegit clone https://git.progmasoft.com/catch3

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.
Commit
56e107561362147dc13f63294a9a5b90b855697b
Author
Phil Nash ·
Parents
868e125d49ca

Files · Download .tar.gz

Changes

 include/internal/catch_approx.cpp                  |  6 +-
 include/internal/catch_approx.h                    | 21 ++++---
 include/internal/catch_assertionresult.cpp         |  7 +--
 include/internal/catch_console_colour.cpp          |  2 +
 include/internal/catch_enforce.h                   |  5 +-
 include/internal/catch_list.cpp                    |  9 +--
 include/internal/catch_message.h                   |  5 +-
 include/internal/catch_run_context.cpp             |  7 ++-
 include/internal/catch_run_context.h               |  5 +-
 include/internal/catch_stream.cpp                  | 72 +++++++++++++++++++---
 include/internal/catch_stream.h                    | 19 ++++++
 include/internal/catch_tag_alias_registry.cpp      |  3 +-
 include/internal/catch_test_case_info.cpp          |  1 +
 include/internal/catch_test_case_registry_impl.cpp |  6 +-
 include/internal/catch_test_case_tracker.cpp       |  1 +
 include/internal/catch_tostring.cpp                | 30 ++++-----
 include/internal/catch_tostring.h                  | 66 ++++++++++----------
 include/internal/catch_wildcard_pattern.cpp        |  1 +
 include/internal/catch_xmlwriter.h                 |  9 +--
 include/reporters/catch_reporter_bases.hpp         |  9 +--
 include/reporters/catch_reporter_console.cpp       |  6 +-
 include/reporters/catch_reporter_junit.cpp         | 12 ++--
 include/reporters/catch_reporter_teamcity.hpp      |  6 +-
 projects/SelfTest/MatchersTests.cpp                |  2 +
 projects/SelfTest/MiscTests.cpp                    |  1 +
 projects/SelfTest/TrickyTests.cpp                  |  5 +-
 26 files changed, 202 insertions(+), 114 deletions(-)

diff --git a/include/internal/catch_approx.cpp b/include/internal/catch_approx.cpp
index 82c6ff12..20f7b165 100644
--- a/include/internal/catch_approx.cpp
+++ b/include/internal/catch_approx.cpp
@@ -36,9 +36,9 @@ namespace Detail {
     }
 
     std::string Approx::toString() const {
-        std::ostringstream oss;
-        oss << "Approx( " << ::Catch::Detail::stringify( m_value ) << " )";
-        return oss.str();
+        ReusableStringStream rss;
+        rss << "Approx( " << ::Catch::Detail::stringify( m_value ) << " )";
+        return rss.str();
     }
 
     bool Approx::equalityComparisonImpl(const double other) const {
diff --git a/include/internal/catch_approx.h b/include/internal/catch_approx.h
index 64f92db7..27068872 100644
--- a/include/internal/catch_approx.h
+++ b/include/internal/catch_approx.h
@@ -8,10 +8,10 @@
 #ifndef TWOBLUECUBES_CATCH_APPROX_HPP_INCLUDED
 #define TWOBLUECUBES_CATCH_APPROX_HPP_INCLUDED
 
-#include "catch_enforce.h"
 #include "catch_tostring.h"
 
 #include <type_traits>
+#include <stdexcept>
 
 namespace Catch {
 namespace Detail {
@@ -83,9 +83,12 @@ namespace Detail {
         template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type>
         Approx& epsilon( T const& newEpsilon ) {
             double epsilonAsDouble = static_cast<double>(newEpsilon);
-            CATCH_ENFORCE(epsilonAsDouble >= 0 && epsilonAsDouble <= 1.0,
-                          "Invalid Approx::epsilon: " << epsilonAsDouble
-                          << ", Approx::epsilon has to be between 0 and 1");
+            if( epsilonAsDouble < 0 || epsilonAsDouble > 1.0 ) {
+                throw std::domain_error
+                    (   "Invalid Approx::epsilon: " +
+                        Catch::Detail::stringify( epsilonAsDouble ) +
+                        ", Approx::epsilon has to be between 0 and 1" );
+            }
             m_epsilon = epsilonAsDouble;
             return *this;
         }
@@ -93,9 +96,13 @@ namespace Detail {
         template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type>
         Approx& margin( T const& newMargin ) {
             double marginAsDouble = static_cast<double>(newMargin);
-            CATCH_ENFORCE(marginAsDouble >= 0,
-                          "Invalid Approx::margin: " << marginAsDouble
-                          << ", Approx::Margin has to be non-negative.");
+            if( marginAsDouble < 0 ) {
+                throw std::domain_error
+                    (   "Invalid Approx::margin: " +
+                         Catch::Detail::stringify( marginAsDouble ) +
+                         ", Approx::Margin has to be non-negative." );
+
+            }
             m_margin = marginAsDouble;
             return *this;
         }
diff --git a/include/internal/catch_assertionresult.cpp b/include/internal/catch_assertionresult.cpp
index 3af40dfc..e58a441f 100644
--- a/include/internal/catch_assertionresult.cpp
+++ b/include/internal/catch_assertionresult.cpp
@@ -17,10 +17,9 @@ namespace Catch {
 
         if( reconstructedExpression.empty() ) {
             if( lazyExpression ) {
-                // !TBD Use stringstream for now, but rework above to pass stream in
-                std::ostringstream oss;
-                oss << lazyExpression;
-                reconstructedExpression = oss.str();
+                ReusableStringStream rss;
+                rss << lazyExpression;
+                reconstructedExpression = rss.str();
             }
         }
         return reconstructedExpression;
diff --git a/include/internal/catch_console_colour.cpp b/include/internal/catch_console_colour.cpp
index 8b055b33..25949f7f 100644
--- a/include/internal/catch_console_colour.cpp
+++ b/include/internal/catch_console_colour.cpp
@@ -23,6 +23,8 @@
 #include "catch_debugger.h"
 #include "catch_windows_h_proxy.h"
 
+#include <sstream>
+
 namespace Catch {
     namespace {
 
diff --git a/include/internal/catch_enforce.h b/include/internal/catch_enforce.h
index e489cf7e..7dc3e3d0 100644
--- a/include/internal/catch_enforce.h
+++ b/include/internal/catch_enforce.h
@@ -8,12 +8,13 @@
 #define TWOBLUECUBES_CATCH_ENFORCE_H_INCLUDED
 
 #include "catch_common.h"
+#include "catch_stream.h"
 
-#include <sstream>
 #include <stdexcept>
+#include <iosfwd>
 
 #define CATCH_PREPARE_EXCEPTION( type, msg ) \
-    type( static_cast<std::ostringstream&&>( std::ostringstream() << msg ).str() )
+    type( static_cast<std::ostringstream&&>( Catch::ReusableStringStream().get() << msg ).str() )
 #define CATCH_INTERNAL_ERROR( msg ) \
     throw CATCH_PREPARE_EXCEPTION( std::logic_error, CATCH_INTERNAL_LINEINFO << ": Internal Catch error: " << msg);
 #define CATCH_ERROR( msg ) \
diff --git a/include/internal/catch_list.cpp b/include/internal/catch_list.cpp
index 4391c441..3efad0b7 100644
--- a/include/internal/catch_list.cpp
+++ b/include/internal/catch_list.cpp
@@ -115,13 +115,14 @@ namespace Catch {
         }
 
         for( auto const& tagCount : tagCounts ) {
-            std::ostringstream oss;
-            oss << "  " << std::setw(2) << tagCount.second.count << "  ";
+            ReusableStringStream rss;
+            rss << "  " << std::setw(2) << tagCount.second.count << "  ";
+            auto str = rss.str();
             auto wrapper = Column( tagCount.second.all() )
                                                     .initialIndent( 0 )
-                                                    .indent( oss.str().size() )
+                                                    .indent( str.size() )
                                                     .width( CATCH_CONFIG_CONSOLE_WIDTH-10 );
-            Catch::cout() << oss.str() << wrapper << '\n';
+            Catch::cout() << str << wrapper << '\n';
         }
         Catch::cout() << pluralise( tagCounts.size(), "tag" ) << '\n' << std::endl;
         return tagCounts.size();
diff --git a/include/internal/catch_message.h b/include/internal/catch_message.h
index c90d30d3..9000fbd9 100644
--- a/include/internal/catch_message.h
+++ b/include/internal/catch_message.h
@@ -9,9 +9,9 @@
 #define TWOBLUECUBES_CATCH_MESSAGE_H_INCLUDED
 
 #include <string>
-#include <sstream>
 #include "catch_result_type.h"
 #include "catch_common.h"
+#include "catch_stream.h"
 
 namespace Catch {
 
@@ -40,8 +40,7 @@ namespace Catch {
             return *this;
         }
 
-        // !TBD reuse a global/ thread-local stream
-        std::ostringstream m_stream;
+        ReusableStringStream m_stream;
     };
 
     struct MessageBuilder : MessageStream {
diff --git a/include/internal/catch_run_context.cpp b/include/internal/catch_run_context.cpp
index 11b85b24..005b1af2 100644
--- a/include/internal/catch_run_context.cpp
+++ b/include/internal/catch_run_context.cpp
@@ -6,6 +6,7 @@
 
 #include <cassert>
 #include <algorithm>
+#include <sstream>
 
 namespace Catch {
 
@@ -13,7 +14,7 @@ namespace Catch {
         : m_stream(stream),
         m_prevBuf(stream.rdbuf()),
         m_targetString(targetString) {
-        stream.rdbuf(m_oss.rdbuf());
+        stream.rdbuf(m_oss.get().rdbuf());
     }
 
     StreamRedirect::~StreamRedirect() {
@@ -24,8 +25,8 @@ namespace Catch {
     StdErrRedirect::StdErrRedirect(std::string & targetString)
         :m_cerrBuf(cerr().rdbuf()), m_clogBuf(clog().rdbuf()),
         m_targetString(targetString) {
-        cerr().rdbuf(m_oss.rdbuf());
-        clog().rdbuf(m_oss.rdbuf());
+        cerr().rdbuf(m_oss.get().rdbuf());
+        clog().rdbuf(m_oss.get().rdbuf());
     }
 
     StdErrRedirect::~StdErrRedirect() {
diff --git a/include/internal/catch_run_context.h b/include/internal/catch_run_context.h
index 56e03a7a..0d10a19b 100644
--- a/include/internal/catch_run_context.h
+++ b/include/internal/catch_run_context.h
@@ -32,13 +32,12 @@ namespace Catch {
 
     public:
         StreamRedirect(std::ostream& stream, std::string& targetString);
-
         ~StreamRedirect();
 
     private:
         std::ostream& m_stream;
         std::streambuf* m_prevBuf;
-        std::ostringstream m_oss;
+        ReusableStringStream m_oss;
         std::string& m_targetString;
     };
 
@@ -52,7 +51,7 @@ namespace Catch {
     private:
         std::streambuf* m_cerrBuf;
         std::streambuf* m_clogBuf;
-        std::ostringstream m_oss;
+        ReusableStringStream m_oss;
         std::string& m_targetString;
     };
 
diff --git a/include/internal/catch_stream.cpp b/include/internal/catch_stream.cpp
index 9d4a3de2..1b157bb8 100644
--- a/include/internal/catch_stream.cpp
+++ b/include/internal/catch_stream.cpp
@@ -13,10 +13,16 @@
 #include "catch_debug_console.h"
 #include "catch_stringref.h"
 
-#include <stdexcept>
 #include <cstdio>
 #include <iostream>
 #include <fstream>
+#include <sstream>
+#include <vector>
+
+#if defined(__clang__)
+#    pragma clang diagnostic push
+#    pragma clang diagnostic ignored "-Wexit-time-destructors"
+#endif
 
 namespace Catch {
 
@@ -132,18 +138,64 @@ namespace Catch {
             return new detail::FileStream( filename );
     }
 
-    ///////////////////////////////////////////////////////////////////////////
 
+    // This class encapsulates the idea of a pool of ostringstreams that can be reused.
+    struct StringStreams {
+        std::vector<std::unique_ptr<std::ostringstream>> m_streams;
+        std::vector<std::size_t> m_unused;
+        std::ostringstream m_referenceStream; // Used for copy state/ flags from
 
-#ifndef CATCH_CONFIG_NOSTDOUT // If you #define this you must implement these functions
-    std::ostream& cout() {
-        return std::cout;
-    }
-    std::ostream& cerr() {
-        return std::cerr;
+        auto add() -> std::size_t {
+            if( m_unused.empty() ) {
+                m_streams.push_back( std::unique_ptr<std::ostringstream>( new std::ostringstream ) );
+                return m_streams.size()-1;
+            }
+            else {
+                auto index = m_unused.back();
+                m_unused.pop_back();
+                return index;
+            }
+        }
+
+        void release( std::size_t index ) {
+            m_streams[index]->copyfmt( m_referenceStream ); // Restore initial flags and other state
+            m_unused.push_back(index);
+        }
+
+        // !TBD: put in TLS
+        static auto instance() -> StringStreams& {
+            static StringStreams s_stringStreams;
+            return s_stringStreams;
+        }
+    };
+
+
+    ReusableStringStream::ReusableStringStream()
+    :   m_index( StringStreams::instance().add() ),
+        m_oss( StringStreams::instance().m_streams[m_index].get() )
+    {}
+
+    ReusableStringStream::~ReusableStringStream() {
+        static_cast<std::ostringstream*>( m_oss )->str("");
+        m_oss->clear();
+        StringStreams::instance().release( m_index );
     }
-    std::ostream& clog() {
-        return std::clog;
+
+    auto ReusableStringStream::str() const -> std::string {
+        return static_cast<std::ostringstream*>( m_oss )->str();
     }
+
+
+    ///////////////////////////////////////////////////////////////////////////
+
+
+#ifndef CATCH_CONFIG_NOSTDOUT // If you #define this you must implement these functions
+    std::ostream& cout() { return std::cout; }
+    std::ostream& cerr() { return std::cerr; }
+    std::ostream& clog() { return std::clog; }
 #endif
 }
+
+#if defined(__clang__)
+#    pragma clang diagnostic pop
+#endif
diff --git a/include/internal/catch_stream.h b/include/internal/catch_stream.h
index 92136199..2b41adbd 100644
--- a/include/internal/catch_stream.h
+++ b/include/internal/catch_stream.h
@@ -10,6 +10,8 @@
 #define TWOBLUECUBES_CATCH_STREAM_H_INCLUDED
 
 #include <iosfwd>
+#include <cstddef>
+#include <ostream>
 
 namespace Catch {
 
@@ -25,6 +27,23 @@ namespace Catch {
     };
 
     auto makeStream( StringRef const &filename ) -> IStream const*;
+
+    class ReusableStringStream {
+        std::size_t m_index;
+        std::ostream* m_oss;
+    public:
+        ReusableStringStream();
+        ~ReusableStringStream();
+
+        auto str() const -> std::string;
+
+        template<typename T>
+        auto operator << ( T const& value ) -> ReusableStringStream& {
+            *m_oss << value;
+            return *this;
+        }
+        auto get() -> std::ostream& { return *m_oss; }
+    };
 }
 
 #endif // TWOBLUECUBES_CATCH_STREAM_H_INCLUDED
diff --git a/include/internal/catch_tag_alias_registry.cpp b/include/internal/catch_tag_alias_registry.cpp
index cfc98f17..98aea26a 100644
--- a/include/internal/catch_tag_alias_registry.cpp
+++ b/include/internal/catch_tag_alias_registry.cpp
@@ -10,9 +10,10 @@
 #include "catch_console_colour.h"
 #include "catch_enforce.h"
 #include "catch_interfaces_registry_hub.h"
-#include "catch_stream.h"
 #include "catch_string_manip.h"
 
+#include <sstream>
+
 namespace Catch {
 
     TagAliasRegistry::~TagAliasRegistry() {}
diff --git a/include/internal/catch_test_case_info.cpp b/include/internal/catch_test_case_info.cpp
index 1f56dbf7..4ab97755 100644
--- a/include/internal/catch_test_case_info.cpp
+++ b/include/internal/catch_test_case_info.cpp
@@ -15,6 +15,7 @@
 #include <cctype>
 #include <exception>
 #include <algorithm>
+#include <sstream>
 
 namespace Catch {
 
diff --git a/include/internal/catch_test_case_registry_impl.cpp b/include/internal/catch_test_case_registry_impl.cpp
index b9aae92c..89c72392 100644
--- a/include/internal/catch_test_case_registry_impl.cpp
+++ b/include/internal/catch_test_case_registry_impl.cpp
@@ -66,9 +66,9 @@ namespace Catch {
     void TestRegistry::registerTest( TestCase const& testCase ) {
         std::string name = testCase.getTestCaseInfo().name;
         if( name.empty() ) {
-            std::ostringstream oss;
-            oss << "Anonymous test case " << ++m_unnamedCount;
-            return registerTest( testCase.withName( oss.str() ) );
+            ReusableStringStream rss;
+            rss << "Anonymous test case " << ++m_unnamedCount;
+            return registerTest( testCase.withName( rss.str() ) );
         }
         m_functions.push_back( testCase );
     }
diff --git a/include/internal/catch_test_case_tracker.cpp b/include/internal/catch_test_case_tracker.cpp
index bcb7793f..66da89e3 100644
--- a/include/internal/catch_test_case_tracker.cpp
+++ b/include/internal/catch_test_case_tracker.cpp
@@ -13,6 +13,7 @@
 #include <assert.h>
 #include <stdexcept>
 #include <memory>
+#include <sstream>
 
 #if defined(__clang__)
 #    pragma clang diagnostic push
diff --git a/include/internal/catch_tostring.cpp b/include/internal/catch_tostring.cpp
index 1b47178d..dc6f3af5 100644
--- a/include/internal/catch_tostring.cpp
+++ b/include/internal/catch_tostring.cpp
@@ -52,22 +52,22 @@ namespace Detail {
         }
 
         unsigned char const *bytes = static_cast<unsigned char const *>(object);
-        std::ostringstream os;
-        os << "0x" << std::setfill('0') << std::hex;
+        ReusableStringStream rss;
+        rss << "0x" << std::setfill('0') << std::hex;
         for( ; i != end; i += inc )
-             os << std::setw(2) << static_cast<unsigned>(bytes[i]);
-       return os.str();
+             rss << std::setw(2) << static_cast<unsigned>(bytes[i]);
+       return rss.str();
     }
 }
 
 
 template<typename T>
 std::string fpToString( T value, int precision ) {
-    std::ostringstream oss;
-    oss << std::setprecision( precision )
+    ReusableStringStream rss;
+    rss << std::setprecision( precision )
         << std::fixed
         << value;
-    std::string d = oss.str();
+    std::string d = rss.str();
     std::size_t i = d.find_last_not_of( '0' );
     if( i != std::string::npos && i != d.size()-1 ) {
         if( d[i] == '.' )
@@ -153,12 +153,12 @@ std::string StringMaker<long>::convert(long value) {
     return ::Catch::Detail::stringify(static_cast<long long>(value));
 }
 std::string StringMaker<long long>::convert(long long value) {
-    std::ostringstream oss;
-    oss << value;
+    ReusableStringStream rss;
+    rss << value;
     if (value > Detail::hexThreshold) {
-        oss << " (0x" << std::hex << value << ')';
+        rss << " (0x" << std::hex << value << ')';
     }
-    return oss.str();
+    return rss.str();
 }
 
 std::string StringMaker<unsigned int>::convert(unsigned int value) {
@@ -168,12 +168,12 @@ std::string StringMaker<unsigned long>::convert(unsigned long value) {
     return ::Catch::Detail::stringify(static_cast<unsigned long long>(value));
 }
 std::string StringMaker<unsigned long long>::convert(unsigned long long value) {
-    std::ostringstream oss;
-    oss << value;
+    ReusableStringStream rss;
+    rss << value;
     if (value > Detail::hexThreshold) {
-        oss << " (0x" << std::hex << value << ')';
+        rss << " (0x" << std::hex << value << ')';
     }
-    return oss.str();
+    return rss.str();
 }
 
 
diff --git a/include/internal/catch_tostring.h b/include/internal/catch_tostring.h
index 8348c825..40d61479 100644
--- a/include/internal/catch_tostring.h
+++ b/include/internal/catch_tostring.h
@@ -9,11 +9,11 @@
 #define TWOBLUECUBES_CATCH_TOSTRING_H_INCLUDED
 
 
-#include <sstream>
 #include <vector>
 #include <cstddef>
 #include <type_traits>
 #include <string>
+#include "catch_stream.h"
 
 #ifdef __OBJC__
 #include "catch_objc_arc.hpp"
@@ -66,9 +66,9 @@ namespace Catch {
         static
         typename std::enable_if<::Catch::Detail::IsStreamInsertable<Fake>::value, std::string>::type
             convert(const Fake& t) {
-                std::ostringstream sstr;
-                sstr << t;
-                return sstr.str();
+                ReusableStringStream rss;
+                rss << t;
+                return rss.str();
         }
 
         template <typename Fake = T>
@@ -221,15 +221,15 @@ namespace Catch {
     namespace Detail {
         template<typename InputIterator>
         std::string rangeToString(InputIterator first, InputIterator last) {
-            std::ostringstream oss;
-            oss << "{ ";
+            ReusableStringStream rss;
+            rss << "{ ";
             if (first != last) {
-                oss << ::Catch::Detail::stringify(*first);
+                rss << ::Catch::Detail::stringify(*first);
                 for (++first; first != last; ++first)
-                    oss << ", " << ::Catch::Detail::stringify(*first);
+                    rss << ", " << ::Catch::Detail::stringify(*first);
             }
-            oss << " }";
-            return oss.str();
+            rss << " }";
+            return rss.str();
         }
     }
 
@@ -290,13 +290,13 @@ namespace Catch {
     template<typename T1, typename T2>
     struct StringMaker<std::pair<T1, T2> > {
         static std::string convert(const std::pair<T1, T2>& pair) {
-            std::ostringstream oss;
-            oss << "{ "
+            ReusableStringStream rss;
+            rss << "{ "
                 << ::Catch::Detail::stringify(pair.first)
                 << ", "
                 << ::Catch::Detail::stringify(pair.second)
                 << " }";
-            return oss.str();
+            return rss.str();
         }
     };
 }
@@ -334,11 +334,11 @@ namespace Catch {
     template<typename ...Types>
     struct StringMaker<std::tuple<Types...>> {
         static std::string convert(const std::tuple<Types...>& tuple) {
-            std::ostringstream os;
-            os << '{';
-            Detail::TupleElementPrinter<std::tuple<Types...>>::print(tuple, os);
-            os << " }";
-            return os.str();
+            ReusableStringStream rss;
+            rss << '{';
+            Detail::TupleElementPrinter<std::tuple<Types...>>::print(tuple, rss.get());
+            rss << " }";
+            return rss.str();
         }
     };
 }
@@ -358,10 +358,10 @@ struct ratio_string {
 
 template <class Ratio>
 std::string ratio_string<Ratio>::symbol() {
-    std::ostringstream oss;
-    oss << '[' << Ratio::num << '/'
+    Catch::ReusableStringStream rss;
+    rss << '[' << Ratio::num << '/'
         << Ratio::den << ']';
-    return oss.str();
+    return rss.str();
 }
 template <>
 struct ratio_string<std::atto> {
@@ -394,33 +394,33 @@ namespace Catch {
     template<typename Value, typename Ratio>
     struct StringMaker<std::chrono::duration<Value, Ratio>> {
         static std::string convert(std::chrono::duration<Value, Ratio> const& duration) {
-            std::ostringstream oss;
-            oss << duration.count() << ' ' << ratio_string<Ratio>::symbol() << 's';
-            return oss.str();
+            ReusableStringStream rss;
+            rss << duration.count() << ' ' << ratio_string<Ratio>::symbol() << 's';
+            return rss.str();
         }
     };
     template<typename Value>
     struct StringMaker<std::chrono::duration<Value, std::ratio<1>>> {
         static std::string convert(std::chrono::duration<Value, std::ratio<1>> const& duration) {
-            std::ostringstream oss;
-            oss << duration.count() << " s";
-            return oss.str();
+            ReusableStringStream rss;
+            rss << duration.count() << " s";
+            return rss.str();
         }
     };
     template<typename Value>
     struct StringMaker<std::chrono::duration<Value, std::ratio<60>>> {
         static std::string convert(std::chrono::duration<Value, std::ratio<60>> const& duration) {
-            std::ostringstream oss;
-            oss << duration.count() << " m";
-            return oss.str();
+            ReusableStringStream rss;
+            rss << duration.count() << " m";
+            return rss.str();
         }
     };
     template<typename Value>
     struct StringMaker<std::chrono::duration<Value, std::ratio<3600>>> {
         static std::string convert(std::chrono::duration<Value, std::ratio<3600>> const& duration) {
-            std::ostringstream oss;
-            oss << duration.count() << " h";
-            return oss.str();
+            ReusableStringStream rss;
+            rss << duration.count() << " h";
+            return rss.str();
         }
     };
 
diff --git a/include/internal/catch_wildcard_pattern.cpp b/include/internal/catch_wildcard_pattern.cpp
index f7303568..9bf20f04 100644
--- a/include/internal/catch_wildcard_pattern.cpp
+++ b/include/internal/catch_wildcard_pattern.cpp
@@ -9,6 +9,7 @@
 #include "catch_enforce.h"
 #include "catch_string_manip.h"
 
+#include <sstream>
 
 namespace Catch {
 
diff --git a/include/internal/catch_xmlwriter.h b/include/internal/catch_xmlwriter.h
index 2570ec12..76456f96 100644
--- a/include/internal/catch_xmlwriter.h
+++ b/include/internal/catch_xmlwriter.h
@@ -11,7 +11,6 @@
 #include "catch_stream.h"
 #include "catch_compiler_capabilities.h"
 
-#include <sstream>
 #include <vector>
 
 namespace Catch {
@@ -73,10 +72,9 @@ namespace Catch {
 
         template<typename T>
         XmlWriter& writeAttribute( std::string const& name, T const& attribute ) {
-            m_oss.clear();
-            m_oss.str(std::string());
-            m_oss << attribute;
-            return writeAttribute( name, m_oss.str() );
+            ReusableStringStream rss;
+            rss << attribute;
+            return writeAttribute( name, rss.str() );
         }
 
         XmlWriter& writeText( std::string const& text, bool indent = true );
@@ -100,7 +98,6 @@ namespace Catch {
         std::vector<std::string> m_tags;
         std::string m_indent;
         std::ostream& m_os;
-        std::ostringstream m_oss;
     };
 
 }
diff --git a/include/reporters/catch_reporter_bases.hpp b/include/reporters/catch_reporter_bases.hpp
index 1c7efb64..7b66ea05 100644
--- a/include/reporters/catch_reporter_bases.hpp
+++ b/include/reporters/catch_reporter_bases.hpp
@@ -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;
 
diff --git a/include/reporters/catch_reporter_console.cpp b/include/reporters/catch_reporter_console.cpp
index 68eb3b94..cfeacae3 100644
--- a/include/reporters/catch_reporter_console.cpp
+++ b/include/reporters/catch_reporter_console.cpp
@@ -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;
diff --git a/include/reporters/catch_reporter_junit.cpp b/include/reporters/catch_reporter_junit.cpp
index bb7f6d9c..0b40f0fd 100644
--- a/include/reporters/catch_reporter_junit.cpp
+++ b/include/reporters/catch_reporter_junit.cpp
@@ -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 );
             }
         }
 
diff --git a/include/reporters/catch_reporter_teamcity.hpp b/include/reporters/catch_reporter_teamcity.hpp
index 6c7b3d18..96e35a84 100644
--- a/include/reporters/catch_reporter_teamcity.hpp
+++ b/include/reporters/catch_reporter_teamcity.hpp
@@ -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";
diff --git a/projects/SelfTest/MatchersTests.cpp b/projects/SelfTest/MatchersTests.cpp
index 2a9040a8..8f376292 100644
--- a/projects/SelfTest/MatchersTests.cpp
+++ b/projects/SelfTest/MatchersTests.cpp
@@ -8,6 +8,8 @@
 
 #include "catch.hpp"
 
+#include <sstream>
+
 #ifdef __clang__
 #pragma clang diagnostic push
 #pragma clang diagnostic ignored "-Wweak-vtables"
diff --git a/projects/SelfTest/MiscTests.cpp b/projects/SelfTest/MiscTests.cpp
index ff9a0a10..a7449081 100644
--- a/projects/SelfTest/MiscTests.cpp
+++ b/projects/SelfTest/MiscTests.cpp
@@ -18,6 +18,7 @@
 #include <iostream>
 #include <cerrno>
 #include <limits>
+#include <sstream>
 
 TEST_CASE( "random SECTION tests", "[.][sections][failing]" ) {
     int a = 1;
diff --git a/projects/SelfTest/TrickyTests.cpp b/projects/SelfTest/TrickyTests.cpp
index 820d2fbd..79d2c63c 100644
--- a/projects/SelfTest/TrickyTests.cpp
+++ b/projects/SelfTest/TrickyTests.cpp
@@ -15,10 +15,11 @@
                                  // that is triggered when compiling as Win32|Release
 #endif
 
-#include <stdio.h>
-
 #include "catch.hpp"
 
+#include <stdio.h>
+#include <sstream>
+
 namespace Catch {
     std::string toString( const std::pair<int, int>& value ) {
         std::ostringstream oss;