cmPropertyDefinition: Avoid string copies for Property lookups

This commit is contained in:
John Franklin Rickard
2026-02-06 12:27:13 -05:00
committed by Brad King
parent 3d546663bc
commit 9a575a835f
3 changed files with 26 additions and 20 deletions
+16 -8
View File
@@ -3,6 +3,7 @@
#include "cmPropertyDefinition.h"
#include <tuple>
#include <utility>
cmPropertyDefinition::cmPropertyDefinition(std::string shortDescription,
std::string fullDescription,
@@ -20,22 +21,29 @@ void cmPropertyDefinitionMap::DefineProperty(
std::string const& ShortDescription, std::string const& FullDescription,
bool chain, std::string const& initializeFromVariable)
{
auto it = this->Map_.find(KeyType(name, scope));
auto it = this->Map_.find(name);
if (it == this->Map_.end()) {
it = this->Map_.emplace(name, ScopeMap()).first;
}
ScopeMap& scopeMap = it->second;
auto scopeIter = scopeMap.find(scope);
if (scopeIter == scopeMap.end()) {
// try_emplace() since C++17
this->Map_.emplace(std::piecewise_construct,
std::forward_as_tuple(name, scope),
std::forward_as_tuple(ShortDescription, FullDescription,
chain, initializeFromVariable));
scopeMap.emplace(std::piecewise_construct, std::forward_as_tuple(scope),
std::forward_as_tuple(ShortDescription, FullDescription,
chain, initializeFromVariable));
}
}
cmPropertyDefinition const* cmPropertyDefinitionMap::GetPropertyDefinition(
std::string const& name, cmProperty::ScopeType scope) const
{
auto it = this->Map_.find(KeyType(name, scope));
if (it != this->Map_.end()) {
return &it->second;
auto nameIter = this->Map_.find(name);
if (nameIter != this->Map_.end()) {
auto scopeIter = nameIter->second.find(scope);
if (scopeIter != nameIter->second.end()) {
return &scopeIter->second;
}
}
return nullptr;
+3 -7
View File
@@ -6,7 +6,6 @@
#include <map>
#include <string>
#include <utility>
#include "cmProperty.h"
@@ -69,12 +68,9 @@ public:
cmPropertyDefinition const* GetPropertyDefinition(
std::string const& name, cmProperty::ScopeType scope) const;
using KeyType = std::pair<std::string, cmProperty::ScopeType>;
std::map<KeyType, cmPropertyDefinition> const& GetMap() const
{
return this->Map_;
}
using ScopeMap = std::map<cmProperty::ScopeType, cmPropertyDefinition>;
std::map<std::string, ScopeMap> const& GetMap() const { return this->Map_; }
private:
std::map<KeyType, cmPropertyDefinition> Map_;
std::map<std::string, ScopeMap> Map_;
};
+7 -5
View File
@@ -1102,11 +1102,13 @@ cmTarget::cmTarget(std::string const& name, cmStateEnums::TargetType type,
}
for (auto const& prop : mf->GetState()->GetPropertyDefinitions().GetMap()) {
if (prop.first.second == cmProperty::TARGET &&
!prop.second.GetInitializeFromVariable().empty()) {
if (auto value =
mf->GetDefinition(prop.second.GetInitializeFromVariable())) {
this->SetProperty(prop.first.first, value);
auto iter = prop.second.find(cmProperty::TARGET);
if (iter != prop.second.end()) {
if (!iter->second.GetInitializeFromVariable().empty()) {
if (auto value =
mf->GetDefinition(iter->second.GetInitializeFromVariable())) {
this->SetProperty(prop.first, value);
}
}
}
}