Optimize RunCommand by removing /bin/sh wrapper when possible
For every $(shell echo "test: $PATH") command, when SHELL is /bin/bash,
we essentially run: (each arg wrapped in [])
[/bin/sh] [-c] [/bin/bash -c "echo \"test: \$PATH\""]
This is redundant, since we can just use SHELL, and then we don't need
to do an extra level of shell escaping either. This change makes us run
this instead:
[/bin/bash] [-c] [echo "test: $PATH"]
If SHELL is more complicated than an absolute path to a binary, then
we'll fall back to /bin/sh.
Using the benchmark introduced in the last change, this reduces a
minimal RunCommand execution with a simple SHELL from 3.7ms to 1.3ms.
For a more complex benchmark (though less normalized), for an AOSP
Android build, this change shrinks the average time spent in $(shell)
functions from 4.5ms to 3ms.
Change-Id: I622116e33565e58bb123ee9e9bdd302616a6609c
diff --git a/fileutil.cc b/fileutil.cc
index 0d3c2d6..4cf653b 100644
--- a/fileutil.cc
+++ b/fileutil.cc
@@ -60,15 +60,24 @@
return GetTimestampFromStat(st);
}
-int RunCommand(const string& shell, const string& cmd,
- RedirectStderr redirect_stderr,
+int RunCommand(const string& shell, const string& shellflag,
+ const string& cmd, RedirectStderr redirect_stderr,
string* s) {
- string cmd_escaped = cmd;
- EscapeShell(&cmd_escaped);
- string cmd_with_shell = shell + " \"" + cmd_escaped + "\"";
- const char* argv[] = {
- "/bin/sh", "-c", cmd_with_shell.c_str(), NULL
- };
+ const char* argv[] = { NULL, NULL, NULL, NULL };
+ string cmd_with_shell;
+ if (shell[0] != '/' || shell.find_first_of(" $") != string::npos) {
+ string cmd_escaped = cmd;
+ EscapeShell(&cmd_escaped);
+ cmd_with_shell = shell + " " + shellflag + " \"" + cmd_escaped + "\"";
+ argv[0] = "/bin/sh";
+ argv[1] = "-c";
+ argv[2] = cmd_with_shell.c_str();
+ } else {
+ // If the shell isn't complicated, we don't need to wrap in /bin/sh
+ argv[0] = shell.c_str();
+ argv[1] = shellflag.c_str();
+ argv[2] = cmd.c_str();
+ }
int pipefd[2];
if (pipe(pipefd) != 0)