about summary refs log tree commit diff
path: root/third_party/nix/src/libutil
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@google.com>2020-05-20T21·58+0100
committerVincent Ambo <tazjin@google.com>2020-05-20T21·58+0100
commit43677021e3c285c2ced2075b918da947e13fcb00 (patch)
tree261d2ecbc6a9492d6410f9f4e8fd6629f20b49e6 /third_party/nix/src/libutil
parent689ef502f5b0655c9923ed77da2ae3504630f473 (diff)
refactor(3p/nix): Apply clang-tidy's performance-* fixes r/789
This applies the performance fixes listed here:

https://clang.llvm.org/extra/clang-tidy/checks/list.html
Diffstat (limited to 'third_party/nix/src/libutil')
-rw-r--r--third_party/nix/src/libutil/archive.cc2
-rw-r--r--third_party/nix/src/libutil/args.cc2
-rw-r--r--third_party/nix/src/libutil/config.cc4
-rw-r--r--third_party/nix/src/libutil/serialise.cc4
-rw-r--r--third_party/nix/src/libutil/serialise.hh3
-rw-r--r--third_party/nix/src/libutil/util.cc14
-rw-r--r--third_party/nix/src/libutil/util.hh4
7 files changed, 17 insertions, 16 deletions
diff --git a/third_party/nix/src/libutil/archive.cc b/third_party/nix/src/libutil/archive.cc
index 6583dc20b899..32bad07f22da 100644
--- a/third_party/nix/src/libutil/archive.cc
+++ b/third_party/nix/src/libutil/archive.cc
@@ -145,7 +145,7 @@ void dumpString(const std::string& s, Sink& sink) {
        << "contents" << s << ")";
 }
 
-static SerialisationError badArchive(string s) {
+static SerialisationError badArchive(const string& s) {
   return SerialisationError("bad archive: " + s);
 }
 
diff --git a/third_party/nix/src/libutil/args.cc b/third_party/nix/src/libutil/args.cc
index 2b4407480254..48fa715fdf0f 100644
--- a/third_party/nix/src/libutil/args.cc
+++ b/third_party/nix/src/libutil/args.cc
@@ -175,7 +175,7 @@ Args::FlagMaker& Args::FlagMaker::mkHashTypeFlag(HashType* ht) {
   arity(1);
   label("type");
   description("hash algorithm ('md5', 'sha1', 'sha256', or 'sha512')");
-  handler([ht](std::string s) {
+  handler([ht](const std::string& s) {
     *ht = parseHashType(s);
     if (*ht == htUnknown) {
       throw UsageError("unknown hash type '%1%'", s);
diff --git a/third_party/nix/src/libutil/config.cc b/third_party/nix/src/libutil/config.cc
index ef47c167ed49..828ee1811bdb 100644
--- a/third_party/nix/src/libutil/config.cc
+++ b/third_party/nix/src/libutil/config.cc
@@ -246,12 +246,12 @@ void BaseSetting<bool>::convertToArg(Args& args, const std::string& category) {
   args.mkFlag()
       .longName(name)
       .description(description)
-      .handler([=](std::vector<std::string> ss) { override(true); })
+      .handler([=](const std::vector<std::string>& ss) { override(true); })
       .category(category);
   args.mkFlag()
       .longName("no-" + name)
       .description(description)
-      .handler([=](std::vector<std::string> ss) { override(false); })
+      .handler([=](const std::vector<std::string>& ss) { override(false); })
       .category(category);
 }
 
diff --git a/third_party/nix/src/libutil/serialise.cc b/third_party/nix/src/libutil/serialise.cc
index 4b12367196f1..34af4e840ab6 100644
--- a/third_party/nix/src/libutil/serialise.cc
+++ b/third_party/nix/src/libutil/serialise.cc
@@ -157,8 +157,8 @@ size_t StringSource::read(unsigned char* data, size_t len) {
 #error Coroutines are broken in this version of Boost!
 #endif
 
-std::unique_ptr<Source> sinkToSource(std::function<void(Sink&)> fun,
-                                     std::function<void()> eof) {
+std::unique_ptr<Source> sinkToSource(const std::function<void(Sink&)>& fun,
+                                     const std::function<void()>& eof) {
   struct SinkToSource : Source {
     using coro_t = boost::coroutines2::coroutine<std::string>;
 
diff --git a/third_party/nix/src/libutil/serialise.hh b/third_party/nix/src/libutil/serialise.hh
index d8c8b181f411..a5992b50ec6e 100644
--- a/third_party/nix/src/libutil/serialise.hh
+++ b/third_party/nix/src/libutil/serialise.hh
@@ -209,7 +209,8 @@ struct LambdaSource : Source {
 /* Convert a function that feeds data into a Sink into a Source. The
    Source executes the function as a coroutine. */
 std::unique_ptr<Source> sinkToSource(
-    std::function<void(Sink&)> fun, std::function<void()> eof = []() {
+    const std::function<void(Sink&)>& fun,
+    const std::function<void()>& eof = []() {
       throw EndOfFile("coroutine has finished");
     });
 
diff --git a/third_party/nix/src/libutil/util.cc b/third_party/nix/src/libutil/util.cc
index 21da06b8ab1e..b13dd7e4f975 100644
--- a/third_party/nix/src/libutil/util.cc
+++ b/third_party/nix/src/libutil/util.cc
@@ -76,9 +76,9 @@ void clearEnv() {
   }
 }
 
-void replaceEnv(std::map<std::string, std::string> newEnv) {
+void replaceEnv(const std::map<std::string, std::string>& newEnv) {
   clearEnv();
-  for (auto newEnvVar : newEnv) {
+  for (const auto& newEnvVar : newEnv) {
     setenv(newEnvVar.first.c_str(), newEnvVar.second.c_str(), 1);
   }
 }
@@ -888,9 +888,9 @@ void killUser(uid_t uid) {
 
 /* Wrapper around vfork to prevent the child process from clobbering
    the caller's stack frame in the parent. */
-static pid_t doFork(bool allowVfork, std::function<void()> fun)
+static pid_t doFork(bool allowVfork, const std::function<void()>& fun)
     __attribute__((noinline));
-static pid_t doFork(bool allowVfork, std::function<void()> fun) {
+static pid_t doFork(bool allowVfork, const std::function<void()>& fun) {
 #ifdef __linux__
   pid_t pid = allowVfork ? vfork() : fork();
 #else
@@ -944,7 +944,7 @@ std::vector<char*> stringsToCharPtrs(const Strings& ss) {
   return res;
 }
 
-string runProgram(Path program, bool searchPath, const Strings& args,
+string runProgram(const Path& program, bool searchPath, const Strings& args,
                   const std::optional<std::string>& input) {
   RunOptions opts(program, args);
   opts.searchPath = searchPath;
@@ -1425,7 +1425,7 @@ string base64Decode(const string& s) {
 }
 
 void callFailure(const std::function<void(std::exception_ptr exc)>& failure,
-                 std::exception_ptr exc) {
+                 const std::exception_ptr& exc) {
   try {
     failure(exc);
   } catch (std::exception& e) {
@@ -1516,7 +1516,7 @@ struct InterruptCallbackImpl : InterruptCallback {
 };
 
 std::unique_ptr<InterruptCallback> createInterruptCallback(
-    std::function<void()> callback) {
+    const std::function<void()>& callback) {
   auto interruptCallbacks(_interruptCallbacks.lock());
   interruptCallbacks->push_back(callback);
 
diff --git a/third_party/nix/src/libutil/util.hh b/third_party/nix/src/libutil/util.hh
index 737c4aa77f7a..5ce12f2ede43 100644
--- a/third_party/nix/src/libutil/util.hh
+++ b/third_party/nix/src/libutil/util.hh
@@ -246,7 +246,7 @@ pid_t startProcess(std::function<void()> fun,
 
 /* Run a program and return its stdout in a string (i.e., like the
    shell backtick operator). */
-string runProgram(Path program, bool searchPath = false,
+string runProgram(const Path& program, bool searchPath = false,
                   const Strings& args = Strings(),
                   const std::optional<std::string>& input = {});
 
@@ -453,7 +453,7 @@ struct InterruptCallback {
 /* Register a function that gets called on SIGINT (in a non-signal
    context). */
 std::unique_ptr<InterruptCallback> createInterruptCallback(
-    std::function<void()> callback);
+    const std::function<void()>& callback);
 
 void triggerInterrupt();