refactor(3p/libstore): Optimise nix::printString

Slight performance optimisation of nix::printString by copying chunks
of the input string which do not need escaping as contiguous blocks.

Paired-With: Perry Lorier <isomer@tvl.fyi>
Change-Id: I48bad90c8f2831ae4524c814a12b1982989922f9
Reviewed-on: https://cl.tvl.fyi/c/depot/+/1184
Tested-by: BuildkiteCI
Reviewed-by: edef <edef@edef.eu>
Reviewed-by: glittershark <grfn@gws.fyi>
This commit is contained in:
Vincent Ambo 2020-07-15 23:51:25 +01:00 committed by tazjin
parent e92c824330
commit eff165425a

View file

@ -199,22 +199,47 @@ Derivation Store::derivationFromPath(const Path& drvPath) {
}
}
const char* findChunk(const char* begin) {
while (*begin != 0 && *begin != '\"' && *begin != '\\' && *begin != '\n' &&
*begin != '\r' && *begin != '\t') {
begin++;
}
return begin;
}
static void printString(std::string& res, const std::string& s) {
res += '"';
for (const char* i = s.c_str(); *i != 0; i++) {
if (*i == '\"' || *i == '\\') {
res += "\\";
res += *i;
} else if (*i == '\n') {
res += "\\n";
} else if (*i == '\r') {
res += "\\r";
} else if (*i == '\t') {
res += "\\t";
} else {
res += *i;
const char* it = s.c_str();
while (*it != 0) {
const char* end = findChunk(it);
std::copy(it, end, std::back_inserter(res));
it = end;
switch (*it) {
case '"':
case '\\':
res += "\\";
res += *it;
break;
case '\n':
res += "\\n";
break;
case '\r':
res += "\\r";
break;
case '\t':
res += "\\t";
break;
default:
continue;
}
it++;
}
res += '"';
}