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

Optimize SourceLineInfo::operator< with short-circuiting

In case of 2 instances of SourceLineInfo constructed in the same
file, they will have the same `file` pointer (even at O0). Thus, we
can check if they are equal before calling potentially pointless
`strcmp`.
Commit
c9de7dd12d2971c63f9d32ce5459eb98f2fec13d
Author
Martin Hořeňovský ·
Parents
52cbb507abdc

Files · Download .tar.gz

Changes

 include/internal/catch_common.cpp | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/include/internal/catch_common.cpp b/include/internal/catch_common.cpp
index c271146d..b2fca4c4 100644
--- a/include/internal/catch_common.cpp
+++ b/include/internal/catch_common.cpp
@@ -22,7 +22,9 @@ namespace Catch {
         return line == other.line && (file == other.file || std::strcmp(file, other.file) == 0);
     }
     bool SourceLineInfo::operator < ( SourceLineInfo const& other ) const noexcept {
-        return line < other.line || ( line == other.line && (std::strcmp(file, other.file) < 0));
+        // We can assume that the same file will usually have the same pointer.
+        // Thus, if the pointers are the same, there is no point in calling the strcmp
+        return line < other.line || ( line == other.line && file != other.file && (std::strcmp(file, other.file) < 0));
     }
 
     std::ostream& operator << ( std::ostream& os, SourceLineInfo const& info ) {