1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-22 16:07:49 +08:00

cmDefinitions: Reduce allocation of keys and values in MakeClosure

Use `cm::String` to store keys and values so that `MakeClosure` does
not need to allocate new copies of all of them.

Issue: #19581
This commit is contained in:
Brad King
2019-08-08 14:50:09 -04:00
parent e07e2bc8bb
commit be7807478c
3 changed files with 14 additions and 14 deletions

View File

@@ -17,7 +17,7 @@ cmDefinitions::Def const& cmDefinitions::GetInternal(const std::string& key,
{ {
assert(begin != end); assert(begin != end);
{ {
auto it = begin->Map.find(key); auto it = begin->Map.find(cm::String::borrow(key));
if (it != begin->Map.end()) { if (it != begin->Map.end()) {
it->second.Used = true; it->second.Used = true;
return it->second; return it->second;
@@ -39,7 +39,7 @@ const std::string* cmDefinitions::Get(const std::string& key, StackIter begin,
StackIter end) StackIter end)
{ {
Def const& def = cmDefinitions::GetInternal(key, begin, end, false); Def const& def = cmDefinitions::GetInternal(key, begin, end, false);
return def.Exists ? &def.Value : nullptr; return def.Value ? def.Value.str_if_stable() : nullptr;
} }
void cmDefinitions::Raise(const std::string& key, StackIter begin, void cmDefinitions::Raise(const std::string& key, StackIter begin,
@@ -52,7 +52,7 @@ bool cmDefinitions::HasKey(const std::string& key, StackIter begin,
StackIter end) StackIter end)
{ {
for (StackIter it = begin; it != end; ++it) { for (StackIter it = begin; it != end; ++it) {
if (it->Map.find(key) != it->Map.end()) { if (it->Map.find(cm::String::borrow(key)) != it->Map.end()) {
return true; return true;
} }
} }
@@ -68,11 +68,11 @@ cmDefinitions cmDefinitions::MakeClosure(StackIter begin, StackIter end)
for (auto const& mi : it->Map) { for (auto const& mi : it->Map) {
// Use this key if it is not already set or unset. // Use this key if it is not already set or unset.
if (closure.Map.find(mi.first) == closure.Map.end() && if (closure.Map.find(mi.first) == closure.Map.end() &&
undefined.find(mi.first) == undefined.end()) { undefined.find(mi.first.view()) == undefined.end()) {
if (mi.second.Exists) { if (mi.second.Value) {
closure.Map.insert(mi); closure.Map.insert(mi);
} else { } else {
undefined.emplace(mi.first); undefined.emplace(mi.first.view());
} }
} }
} }
@@ -90,8 +90,8 @@ std::vector<std::string> cmDefinitions::ClosureKeys(StackIter begin,
defined.reserve(defined.size() + it->Map.size()); defined.reserve(defined.size() + it->Map.size());
for (auto const& mi : it->Map) { for (auto const& mi : it->Map) {
// Use this key if it is not already set or unset. // Use this key if it is not already set or unset.
if (bound.emplace(mi.first).second && mi.second.Exists) { if (bound.emplace(mi.first.view()).second && mi.second.Value) {
defined.push_back(mi.first); defined.push_back(*mi.first.str_if_stable());
} }
} }
} }
@@ -116,7 +116,7 @@ std::vector<std::string> cmDefinitions::UnusedKeys() const
// Consider local definitions. // Consider local definitions.
for (auto const& mi : this->Map) { for (auto const& mi : this->Map) {
if (!mi.second.Used) { if (!mi.second.Used) {
keys.push_back(mi.first); keys.push_back(*mi.first.str_if_stable());
} }
} }
return keys; return keys;

View File

@@ -8,7 +8,9 @@
#include "cm_string_view.hxx" #include "cm_string_view.hxx"
#include "cmLinkedTree.h" #include "cmLinkedTree.h"
#include "cmString.hxx"
#include <functional>
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include <vector> #include <vector>
@@ -57,16 +59,14 @@ private:
Def() = default; Def() = default;
Def(cm::string_view value) Def(cm::string_view value)
: Value(value) : Value(value)
, Exists(true)
{ {
} }
std::string Value; cm::String Value;
bool Exists = false;
bool Used = false; bool Used = false;
}; };
static Def NoDef; static Def NoDef;
std::unordered_map<std::string, Def> Map; std::unordered_map<cm::String, Def> Map;
static Def const& GetInternal(const std::string& key, StackIter begin, static Def const& GetInternal(const std::string& key, StackIter begin,
StackIter end, bool raise); StackIter end, bool raise);

View File

@@ -19,11 +19,11 @@
#include "cmState.h" #include "cmState.h"
#include "cmStateSnapshot.h" #include "cmStateSnapshot.h"
#include "cmStateTypes.h" #include "cmStateTypes.h"
#include "cmString.hxx"
#include "cmStringAlgorithms.h" #include "cmStringAlgorithms.h"
#include "cmSystemTools.h" #include "cmSystemTools.h"
#include "cmTarget.h" #include "cmTarget.h"
#include "cm_static_string_view.hxx" #include "cm_static_string_view.hxx"
#include "cm_string_view.hxx"
#include "cmake.h" #include "cmake.h"
#include "cmsys/RegularExpression.hxx" #include "cmsys/RegularExpression.hxx"