Skip to content

Commit

Permalink
Make RegexCache thread-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
edolstra committed Jun 4, 2024
1 parent b36aa04 commit fbbca59
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/libexpr/primops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4062,17 +4062,23 @@ static RegisterPrimOp primop_convertHash({

struct RegexCache
{
// TODO use C++20 transparent comparison when available
std::unordered_map<std::string_view, std::regex> cache;
std::list<std::string> keys;
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;
}
};

Expand Down

0 comments on commit fbbca59

Please sign in to comment.