Skip to content

Commit

Permalink
Remove a number of unnecessary copies made while parsing the command …
Browse files Browse the repository at this point in the history
…line

PiperOrigin-RevId: 240222700
  • Loading branch information
Googler authored and copybara-github committed Mar 25, 2019
1 parent 70e3c33 commit 9cda2ec
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
24 changes: 14 additions & 10 deletions src/main/cpp/option_processor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <string.h>
#include <algorithm>
#include <cassert>
#include <iterator>
#include <set>
#include <sstream>
#include <utility>
Expand Down Expand Up @@ -65,7 +66,7 @@ OptionProcessor::OptionProcessor(
system_bazelrc_path_(system_bazelrc_path) {}

std::unique_ptr<CommandLine> OptionProcessor::SplitCommandLine(
const vector<string>& args, string* error) const {
vector<string> args, string* error) const {
const string lowercase_product_name =
parsed_startup_options_->GetLowercaseProductName();

Expand All @@ -75,13 +76,13 @@ std::unique_ptr<CommandLine> OptionProcessor::SplitCommandLine(
return nullptr;
}

const string path_to_binary(args[0]);
string& path_to_binary = args[0];

// Process the startup options.
vector<string> startup_args;
vector<string>::size_type i = 1;
while (i < args.size() && IsArg(args[i])) {
const string current_arg(args[i]);
string& current_arg = args[i];
// If the current argument is a valid nullary startup option such as
// --master_bazelrc or --nomaster_bazelrc proceed to examine the next
// argument.
Expand All @@ -95,7 +96,7 @@ std::unique_ptr<CommandLine> OptionProcessor::SplitCommandLine(
// The option is of the form '--bazelrc=value', hence proceed to
// examine the next argument.
if (current_arg.find("=") != string::npos) {
startup_args.push_back(current_arg);
startup_args.push_back(std::move(current_arg));
i++;
} else {
// Otherwise, the option is of the form '--bazelrc value', hence
Expand All @@ -112,7 +113,8 @@ std::unique_ptr<CommandLine> OptionProcessor::SplitCommandLine(
return nullptr;
}
// In this case we transform it to the form '--bazelrc=value'.
startup_args.push_back(current_arg + string("=") + args[i + 1]);
startup_args.push_back(std::move(current_arg) + "=" +
std::move(args[i + 1]));
i += 2;
}
} else {
Expand All @@ -129,16 +131,18 @@ std::unique_ptr<CommandLine> OptionProcessor::SplitCommandLine(

// The command is the arg right after the startup options.
if (i == args.size()) {
return std::unique_ptr<CommandLine>(
new CommandLine(path_to_binary, startup_args, "", {}));
return std::unique_ptr<CommandLine>(new CommandLine(
std::move(path_to_binary), std::move(startup_args), "", {}));
}
const string command(args[i]);
string& command = args[i];

// The rest are the command arguments.
const vector<string> command_args(args.begin() + i + 1, args.end());
vector<string> command_args(std::make_move_iterator(args.begin() + i + 1),
std::make_move_iterator(args.end()));

return std::unique_ptr<CommandLine>(
new CommandLine(path_to_binary, startup_args, command, command_args));
new CommandLine(std::move(path_to_binary), std::move(startup_args),
std::move(command), std::move(command_args)));
}

namespace internal {
Expand Down
30 changes: 15 additions & 15 deletions src/main/cpp/option_processor.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <list>
#include <memory>
#include <string>
#include <utility>
#include <vector>

#include "src/main/cpp/rc_file.h"
Expand All @@ -33,19 +34,19 @@ class WorkspaceLayout;
// breakdown should suffice to access the parts of the command line that the
// client cares about, notably the binary and startup startup options.
struct CommandLine {
const std::string path_to_binary;
const std::vector<std::string> startup_args;
const std::string command;
const std::vector<std::string> command_args;

CommandLine(const std::string& path_to_binary_arg,
const std::vector<std::string>& startup_args_arg,
const std::string& command_arg,
const std::vector<std::string>& command_args_arg)
: path_to_binary(path_to_binary_arg),
startup_args(startup_args_arg),
command(command_arg),
command_args(command_args_arg) {}
std::string path_to_binary;
std::vector<std::string> startup_args;
std::string command;
std::vector<std::string> command_args;

CommandLine(std::string path_to_binary_arg,
std::vector<std::string> startup_args_arg,
std::string command_arg,
std::vector<std::string> command_args_arg)
: path_to_binary(std::move(path_to_binary_arg)),
startup_args(std::move(startup_args_arg)),
command(std::move(command_arg)),
command_args(std::move(command_args_arg)) {}
};

// This class is responsible for parsing the command line of the Blaze binary,
Expand Down Expand Up @@ -82,8 +83,7 @@ class OptionProcessor {
// If the method fails then error will contain the cause, otherwise error
// remains untouched.
virtual std::unique_ptr<CommandLine> SplitCommandLine(
const std::vector<std::string>& args,
std::string* error) const;
std::vector<std::string> args, std::string* error) const;

// Parse a command line and the appropriate blazerc files and stores the
// results. This should be invoked only once per OptionProcessor object.
Expand Down

0 comments on commit 9cda2ec

Please sign in to comment.