Skip to content

Commit

Permalink
🔥 Speed up compilation times by not using std::regex
Browse files Browse the repository at this point in the history
Problem:
- std::regex include takes multiple seconds to compile.

Solution:
- Implement utility::regex_match instead.
  • Loading branch information
kris-jusiak committed Oct 5, 2023
1 parent ef80412 commit cce6120
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
19 changes: 15 additions & 4 deletions include/boost/ut.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ export import std;
#include <iostream>
#include <memory>
#include <optional>
#include <regex>
#include <sstream>
#include <stack>
#include <string_view>
Expand Down Expand Up @@ -231,6 +230,18 @@ template <class T = std::string_view, class TDelim>
}
return output;
}
constexpr auto regex_match(const char *str, const char *pattern) -> bool {
if (*pattern == '\0' && *str == '\0') return true;
if (*pattern == '\0' && *str != '\0') return false;
if (*str == '\0' && *pattern != '\0') return false;
if (*pattern == '.') {
return regex_match(str+1, pattern+1);
}
if (*pattern == *str) {
return regex_match(str+1, pattern+1);
}
return false;
}
} // namespace utility

namespace reflection {
Expand Down Expand Up @@ -2031,10 +2042,10 @@ class runner {
}

if (!detail::cfg::query_pattern.empty()) {
const static std::regex regex(detail::cfg::query_regex_pattern);
bool matches = std::regex_match(test.name.data(), regex);
const static auto regex = detail::cfg::query_regex_pattern;
bool matches = utility::regex_match(test.name.data(), regex.c_str());
for (const auto& tag2 : test.tag) {
matches |= std::regex_match(tag2.data(), regex);
matches |= utility::regex_match(tag2.data(), regex.c_str());
}
if (matches) {
execute = !detail::cfg::invert_query_pattern;
Expand Down
16 changes: 15 additions & 1 deletion test/ut/ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,21 @@ int main() {
static_assert("fake_cfg" == reflection::type_name<fake_cfg>());
#endif
}


{
static_assert(utility::regex_match("", ""));
static_assert(utility::regex_match("hello", "hello"));
static_assert(utility::regex_match("hello", "h.llo"));
static_assert(utility::regex_match("hello", "he..o"));
static_assert(not utility::regex_match("hello", "hella"));
static_assert(not utility::regex_match("hello", "helao"));
static_assert(not utility::regex_match("hello", "hlllo"));
static_assert(not utility::regex_match("hello", ""));
static_assert(not utility::regex_match("", "hello"));
static_assert(not utility::regex_match("hi", "hello"));
static_assert(not utility::regex_match("hello there", "hello"));
}

{
test_assert(utility::is_match("", ""));
test_assert(utility::is_match("", "*"));
Expand Down

0 comments on commit cce6120

Please sign in to comment.