1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-12-24 01:41:08 +01:00

libexpr: avoid regex engine in getDerivations attr filtering

- getDerivations() filters attribute names with std::regex_match, which runs the regex engine for every attribute visited during nixpkgs scanning.
- BM_GetDerivationsAttrScan/10000_mean: 3.338 ms → 1.506 ms (≈ -54.9%)
This commit is contained in:
Kamil Monicz 2025-12-18 03:26:35 +00:00
parent 0c8751d3f4
commit 048d0b6781
No known key found for this signature in database
GPG key ID: F9FB19F1C1DC9C23

View file

@ -367,7 +367,26 @@ static std::string addToPath(const std::string & s1, std::string_view s2)
return s1.empty() ? std::string(s2) : s1 + "." + s2;
}
static std::regex attrRegex("[A-Za-z_][A-Za-z0-9-_+]*");
static bool isAttrPathComponent(std::string_view symbol)
{
if (symbol.empty())
return false;
/* [A-Za-z_] */
unsigned char first = symbol[0];
if (!((first >= 'A' && first <= 'Z') || (first >= 'a' && first <= 'z') || first == '_'))
return false;
/* [A-Za-z0-9-_+]* */
for (unsigned char c : symbol.substr(1)) {
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-' || c == '_'
|| c == '+')
continue;
return false;
}
return true;
}
static void getDerivations(
EvalState & state,
@ -400,7 +419,7 @@ static void getDerivations(
std::string_view symbol{state.symbols[i->name]};
try {
debug("evaluating attribute '%1%'", symbol);
if (!std::regex_match(symbol.begin(), symbol.end(), attrRegex))
if (!isAttrPathComponent(symbol))
continue;
std::string pathPrefix2 = addToPath(pathPrefix, symbol);
if (combineChannels)