From 31f7c8009e2d6bf09328f2597633ee8e2cfa68de Mon Sep 17 00:00:00 2001 From: ewewukek Date: Sun, 25 Feb 2024 04:04:28 +0300 Subject: Implement simple caching for compiled regular expressions --- src/stringutils.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/stringutils.cpp b/src/stringutils.cpp index fc80017..e7c8b5b 100644 --- a/src/stringutils.cpp +++ b/src/stringutils.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include @@ -709,16 +710,20 @@ void secureChatCommand(std::string &str) bool isMatch(const std::string &str, const std::string &exp) { - std::regex regExp(exp); - return std::regex_match(str, regExp); + static std::unordered_map re_cache; + if (re_cache.find(exp) == re_cache.end()) + re_cache.emplace(exp, exp); + return std::regex_match(str, re_cache[exp]); } bool isMatch(const std::string &str, const std::string &exp, std::smatch &m) { - std::regex regExp(exp); - return std::regex_match(str, m, regExp); + static std::unordered_map re_cache; + if (re_cache.find(exp) == re_cache.end()) + re_cache.emplace(exp, exp); + return std::regex_match(str, m, re_cache[exp]); } bool fileExists(const std::string &name) -- cgit v1.2.3-60-g2f50