From 050fceac1bf02058b527ecc21f5a3620fba99b60 Mon Sep 17 00:00:00 2001 From: firewave Date: Tue, 14 Nov 2023 16:03:45 +0100 Subject: [PATCH] fixed performance regression in `endsWith()` --- simplecpp.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/simplecpp.cpp b/simplecpp.cpp index 47bb78f..fd61cf6 100755 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -141,7 +141,10 @@ static unsigned long long stringToULL(const std::string &s) static bool endsWith(const std::string &s, const std::string &e) { - return (s.size() >= e.size()) && std::equal(e.rbegin(), e.rend(), s.rbegin()); + // TODO: std::equal() is much faster than std::string::compare() in a benchmark + // but in our case it leads to a big performance regression + //return (s.size() >= e.size()) && std::equal(e.rbegin(), e.rend(), s.rbegin()); + return (s.size() >= e.size() && s.compare(s.size() - e.size(), e.size(), e) == 0); } static bool sameline(const simplecpp::Token *tok1, const simplecpp::Token *tok2)