Merge pull request #10854 from DeterminateSystems/thread-safe-RegexCache

Make RegexCache thread-safe
This commit is contained in:
Robert Hensing 2024-06-08 18:07:35 +02:00 committed by GitHub
commit 0ab9369572
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -4062,17 +4062,23 @@ static RegisterPrimOp primop_convertHash({
struct RegexCache
{
struct State
{
// TODO use C++20 transparent comparison when available
std::unordered_map<std::string_view, std::regex> cache;
std::list<std::string> keys;
};
Sync<State> state_;
std::regex get(std::string_view re)
{
auto it = cache.find(re);
if (it != cache.end())
auto state(state_.lock());
auto it = state->cache.find(re);
if (it != state->cache.end())
return it->second;
keys.emplace_back(re);
return cache.emplace(keys.back(), std::regex(keys.back(), std::regex::extended)).first->second;
state->keys.emplace_back(re);
return state->cache.emplace(state->keys.back(), std::regex(state->keys.back(), std::regex::extended)).first->second;
}
};