mirror of
https://github.com/privatevoid-net/nix-super.git
synced 2024-11-25 07:16:17 +02:00
Merge pull request #10854 from DeterminateSystems/thread-safe-RegexCache
Make RegexCache thread-safe
This commit is contained in:
commit
0ab9369572
1 changed files with 13 additions and 7 deletions
|
@ -4061,18 +4061,24 @@ static RegisterPrimOp primop_convertHash({
|
||||||
});
|
});
|
||||||
|
|
||||||
struct RegexCache
|
struct RegexCache
|
||||||
|
{
|
||||||
|
struct State
|
||||||
{
|
{
|
||||||
// TODO use C++20 transparent comparison when available
|
// TODO use C++20 transparent comparison when available
|
||||||
std::unordered_map<std::string_view, std::regex> cache;
|
std::unordered_map<std::string_view, std::regex> cache;
|
||||||
std::list<std::string> keys;
|
std::list<std::string> keys;
|
||||||
|
};
|
||||||
|
|
||||||
|
Sync<State> state_;
|
||||||
|
|
||||||
std::regex get(std::string_view re)
|
std::regex get(std::string_view re)
|
||||||
{
|
{
|
||||||
auto it = cache.find(re);
|
auto state(state_.lock());
|
||||||
if (it != cache.end())
|
auto it = state->cache.find(re);
|
||||||
|
if (it != state->cache.end())
|
||||||
return it->second;
|
return it->second;
|
||||||
keys.emplace_back(re);
|
state->keys.emplace_back(re);
|
||||||
return cache.emplace(keys.back(), std::regex(keys.back(), std::regex::extended)).first->second;
|
return state->cache.emplace(state->keys.back(), std::regex(state->keys.back(), std::regex::extended)).first->second;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue