1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-26 15:51:15 +02:00

builtins.cache: Cache regular expressions

The evaluator was spending about 1% of its time compiling a small
number of regexes over and over again.
This commit is contained in:
Eelco Dolstra 2020-02-21 19:25:49 +01:00
parent d700eecea9
commit 401b5bc541
No known key found for this signature in database
GPG key ID: 8170B4726D7198DE
2 changed files with 9 additions and 3 deletions

View file

@ -1811,19 +1811,21 @@ static void prim_hashString(EvalState & state, const Pos & pos, Value * * args,
/* Match a regular expression against a string and return either
null or a list containing substring matches. */
static void prim_match(EvalState & state, const Pos & pos, Value * * args, Value & v)
void prim_match(EvalState & state, const Pos & pos, Value * * args, Value & v)
{
auto re = state.forceStringNoCtx(*args[0], pos);
try {
std::regex regex(re, std::regex::extended);
auto regex = state.regexCache.find(re);
if (regex == state.regexCache.end())
regex = state.regexCache.emplace(re, std::regex(re, std::regex::extended)).first;
PathSet context;
const std::string str = state.forceString(*args[1], context, pos);
std::smatch match;
if (!std::regex_match(str, match, regex)) {
if (!std::regex_match(str, match, regex->second)) {
mkNull(v);
return;
}