about summary refs log tree commit diff
path: root/third_party/nix/src/libstore
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2020-07-15T22·51+0100
committertazjin <mail@tazj.in>2020-07-16T00·37+0000
commiteff165425a1a225ac22427ec94b25da9ef6d6e04 (patch)
treeab7212cbd65e461b872431214287eece0d8558b9 /third_party/nix/src/libstore
parente92c8243304bdb60d75dcd4297c386584b7d41c8 (diff)
refactor(3p/libstore): Optimise nix::printString r/1309
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>
Diffstat (limited to 'third_party/nix/src/libstore')
-rw-r--r--third_party/nix/src/libstore/derivations.cc49
1 files changed, 37 insertions, 12 deletions
diff --git a/third_party/nix/src/libstore/derivations.cc b/third_party/nix/src/libstore/derivations.cc
index 64d1c6a15144..59b2a505a307 100644
--- a/third_party/nix/src/libstore/derivations.cc
+++ b/third_party/nix/src/libstore/derivations.cc
@@ -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 += '"';
 }