diff options
author | Eelco Dolstra <edolstra@gmail.com> | 2020-02-21 19:25:49 +0100 |
---|---|---|
committer | Eelco Dolstra <edolstra@gmail.com> | 2020-03-04 11:44:33 +0100 |
commit | 401b5bc5418f3eb6d57da9d9e66df055f8bce122 (patch) | |
tree | def41918a82908b259517da056a69c790a88d2c5 /src/libexpr/primops.cc | |
parent | d700eecea9a274c1b45549141f40180ac74454ce (diff) |
builtins.cache: Cache regular expressions
The evaluator was spending about 1% of its time compiling a small
number of regexes over and over again.
Diffstat (limited to 'src/libexpr/primops.cc')
-rw-r--r-- | src/libexpr/primops.cc | 8 |
1 files changed, 5 insertions, 3 deletions
diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 29302c9b6..4cd28698c 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -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; } |