Refactor Rcfile to use modern Google C++ practices
* Prefer `absl::flat_hash_map` to `std::unordered_map`. This is a much more efficient data structure.
* Prefer `absl::string_view` to `const char*`. This allows compile-time strlen.
* Prefer `absl::WrapUnique` to `std::unique_ptr<T>(T*)`. This better signals intent to wrap a raw pointer.
* Prefer `std::vector` to `std::deque` given we only need a stack, not a queue. This is much faster and more memory-efficient.
* Use if-init to more tightly scope variables.
* Prefer `absl::StrCat` to `blaze_util::StringPrintf` in simple cases. This is faster and more readable when merely concatenating a few strings together.
* Prefer `absl::StrFormat` to `blaze_util::StringPrintf` in cases where we need to do more complicated string construction. This is the standard printf-style library in Google.
* Prefer `absl::StrAppend` to `std::string::operator+`. This is more efficient and readable.
* Prefer `absl::StrSplit` to `blaze_util::Split`. This is the standard string splitting library in Google.
* Prefer `absl::StartsWith` to `std::string::compare`. This is a more expressive API.
* Prefer `absl::c_linear_search` to `std::find`. This avoids needing to name the container three times. This should really be `absl::c_contains`, which provides a better name to the operation, but this is blocked by https://github.com/bazelbuild/bazel/issues/22719.
* Use the early-return pattern where possible to decrease nesting.
* Use references to remove unnecessary copies.
* Don't store unnecessary variables in the class. This decreases object size.
* Remove using-declarations for names in `std::`, which is discouraged.
PiperOrigin-RevId: 661149102
Change-Id: I2ab8fc617e1cec9f6bb81c6a3d5bbb883164f299
diff --git a/src/main/cpp/rc_file.h b/src/main/cpp/rc_file.h
index 91bb119..88af39a 100644
--- a/src/main/cpp/rc_file.h
+++ b/src/main/cpp/rc_file.h
@@ -14,13 +14,12 @@
#ifndef BAZEL_SRC_MAIN_CPP_RC_FILE_H_
#define BAZEL_SRC_MAIN_CPP_RC_FILE_H_
-#include <deque>
#include <memory>
#include <string>
-#include <unordered_map>
#include <vector>
#include "src/main/cpp/workspace_layout.h"
+#include "absl/container/flat_hash_map.h"
namespace blaze {
@@ -39,8 +38,8 @@
// error and error text on failure.
enum class ParseError { NONE, UNREADABLE_FILE, INVALID_FORMAT, IMPORT_LOOP };
static std::unique_ptr<RcFile> Parse(
- std::string filename, const WorkspaceLayout* workspace_layout,
- std::string workspace, ParseError* error, std::string* error_text);
+ const std::string& filename, const WorkspaceLayout* workspace_layout,
+ const std::string& workspace, ParseError* error, std::string* error_text);
// Movable and copyable.
RcFile(const RcFile&) = default;
@@ -54,24 +53,19 @@
}
// Command -> all options for that command (in order of appearance).
- using OptionMap = std::unordered_map<std::string, std::vector<RcOption>>;
+ using OptionMap = absl::flat_hash_map<std::string, std::vector<RcOption>>;
const OptionMap& options() const { return options_; }
private:
- RcFile(std::string filename, const WorkspaceLayout* workspace_layout,
- std::string workspace);
+ RcFile() = default;
// Recursive call to parse a file and its imports.
ParseError ParseFile(const std::string& filename,
- std::deque<std::string>* import_stack,
+ const std::string& workspace,
+ const WorkspaceLayout& workspace_layout,
+ std::vector<std::string>& import_stack,
std::string* error_text);
- std::string filename_;
-
- // Workspace definition.
- const WorkspaceLayout* workspace_layout_;
- std::string workspace_;
-
// Full closure of rcfile paths imported from this file (including itself).
// These are all canonical paths, created with blaze_util::MakeCanonical.
// This also means all of these paths should exist.