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:
parent
e92c824330
commit
eff165425a
1 changed files with 37 additions and 12 deletions
49
third_party/nix/src/libstore/derivations.cc
vendored
49
third_party/nix/src/libstore/derivations.cc
vendored
|
@ -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 += '"';
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue