diff options
author | Jeongik Cha <jeongik@google.com> | 2023-03-15 22:55:17 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2023-03-15 22:55:17 +0000 |
commit | 4566a1ad1f3393f284e9bf35fa2bb7c0e415f1ba (patch) | |
tree | 80ea8b1dd5875de38df42b6743daabe873cdc8f2 | |
parent | a6b76d1e577f3bf3d3956d1b135d130e8083af69 (diff) | |
parent | 5d2e4cc33ce16912d3e28fbd916253023c7f49ed (diff) | |
download | ninja-4566a1ad1f3393f284e9bf35fa2bb7c0e415f1ba.tar.gz |
Merge "Declare a new type for WeightDataSource"
-rw-r--r-- | src/build.cc | 30 |
1 files changed, 24 insertions, 6 deletions
diff --git a/src/build.cc b/src/build.cc index 26a6cda..65f22e0 100644 --- a/src/build.cc +++ b/src/build.cc @@ -84,7 +84,27 @@ bool DryRunCommandRunner::WaitForCommand(Result* result) { return true; } -typedef std::unordered_map<HashedStrView, int64_t> WeightDataSource; +struct WeightDataSource { +public: + void Put(const std::string& key, int64_t value) { + const auto& [it, inserted] = key_data_.emplace(key); + if (inserted) { + map_data_.try_emplace(*it, value); + } else { + map_data_.find(*it)->second = value; + } + } + + std::optional<int64_t> Get(const HashedStrView& key) const { + if (auto it = map_data_.find(key); it != map_data_.end()) { + return it->second; + } + return {}; + } +private: + std::unordered_set<HashedStr> key_data_; + std::unordered_map<HashedStrView, int64_t> map_data_; +}; // Get weight from data source, it isn't related to Edge.weight because // Edge.weight is used for task distribution across pools which we don't @@ -96,8 +116,8 @@ int64_t GetWeight(const WeightDataSource& data_source, Edge* edge) { if (edge->is_phony()) { return 1; } - if (auto it = data_source.find(edge->outputs_[0]->globalPath().h); it != data_source.end()) { - return it->second; + if (const auto& opt = data_source.Get(edge->outputs_[0]->globalPath().h)) { + return *opt; } return 1; } @@ -615,8 +635,6 @@ Node* Builder::AddTarget(const string& name, string* err) { void Builder::RefreshPriority(const std::vector<Node*>& start_nodes) { METRIC_RECORD("RefreshPriority"); WeightDataSource data_source; - // To keep HashedStrView in WeightDataSource valid. - std::vector<HashedStr> paths; if (config_.weight_list_path) { std::ifstream file(config_.weight_list_path.value()); std::string line; @@ -627,7 +645,7 @@ void Builder::RefreshPriority(const std::vector<Node*>& start_nodes) { char* idx; int64_t weight = std::strtoll(raw_weight.c_str(), &idx, 10); if (idx != nullptr) { - data_source[paths.emplace_back(path)] = weight; + data_source.Put(path, weight); } } } |