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

Tiny speedup when listing tags

Noticed that the code was originally concatenating strings just to
then append the result to another string. Now it does not create
temporaries and also preallocates the string buffer.
Commit
cf55cfd76f0088a61088169508cc1d014f323875
Author
Martin Hořeňovský ·
Parents
3701c2e2e645

Files · Download .tar.gz

Changes

 include/internal/catch_list.cpp | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/include/internal/catch_list.cpp b/include/internal/catch_list.cpp
index e1d99222..11e55140 100644
--- a/include/internal/catch_list.cpp
+++ b/include/internal/catch_list.cpp
@@ -84,9 +84,18 @@ namespace Catch {
     }
 
     std::string TagInfo::all() const {
-        std::string out;
-        for( auto const& spelling : spellings )
-            out += "[" + spelling + "]";
+        size_t size = 0;
+        for (auto const& spelling : spellings) {
+            // Add 2 for the brackes
+            size += spelling.size() + 2;
+        }
+
+        std::string out; out.reserve(size);
+        for (auto const& spelling : spellings) {
+            out += '[';
+            out += spelling;
+            out += ']';
+        }
         return out;
     }