diff options
author | Jeongik Cha <jeongik@google.com> | 2023-03-15 21:21:22 +0900 |
---|---|---|
committer | Jeongik Cha <jeongik@google.com> | 2023-03-15 21:25:53 +0900 |
commit | 5d2e4cc33ce16912d3e28fbd916253023c7f49ed (patch) | |
tree | 381e51955e0ffd0227244fdbeebd90d58b5a2ba4 | |
parent | 85feab1d7308ac61770ffde92d21aa935bda2d09 (diff) | |
download | ninja-5d2e4cc33ce16912d3e28fbd916253023c7f49ed.tar.gz |
Declare a new type for WeightDataSource
Maintaining both vector<HashStr> and unordered_map<HashStrView, V> for
lifecycle could be fragile.
Bug: 271527305
Test: NINJA_PRIORITY_FROM_BUILD_LOG=true m
Change-Id: Ic80c2b153b191fc2abd7ad2f0cb27a0f2d51cfa8
-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); } } } |