cmMakefile::AddCacheDefinition: avoid conversions between char* and string

Running the testsuite this function is entered more than 126,000 times. Reorder
the code flow so that a conversion from char* to std::string is only done when
the cache entry is a path one, which happens only ~50 times during the
testsuite.
This commit is contained in:
Rolf Eike Beer 2016-04-20 22:40:58 +02:00
parent 484958b781
commit 068358e1ed
1 changed files with 11 additions and 10 deletions

View File

@ -1824,10 +1824,11 @@ void cmMakefile::AddCacheDefinition(const std::string& name, const char* value,
cmState::CacheEntryType type, cmState::CacheEntryType type,
bool force) bool force)
{ {
bool haveVal = value ? true : false;
std::string val = haveVal ? value : "";
const char* existingValue = const char* existingValue =
this->GetState()->GetInitializedCacheValue(name); this->GetState()->GetInitializedCacheValue(name);
// must be outside the following if() to keep it alive long enough
std::string nvalue;
if(existingValue if(existingValue
&& (this->GetState()->GetCacheEntryType(name) && (this->GetState()->GetCacheEntryType(name)
== cmState::UNINITIALIZED)) == cmState::UNINITIALIZED))
@ -1836,15 +1837,16 @@ void cmMakefile::AddCacheDefinition(const std::string& name, const char* value,
// if it is a force, then use the value being passed in // if it is a force, then use the value being passed in
if(!force) if(!force)
{ {
val = existingValue; value = existingValue;
haveVal = true;
} }
if ( type == cmState::PATH || type == cmState::FILEPATH ) if ( type == cmState::PATH || type == cmState::FILEPATH )
{ {
std::vector<std::string>::size_type cc; std::vector<std::string>::size_type cc;
std::vector<std::string> files; std::vector<std::string> files;
std::string nvalue = ""; nvalue = value ? value : "";
cmSystemTools::ExpandListArgument(val, files);
cmSystemTools::ExpandListArgument(nvalue, files);
nvalue = "";
for ( cc = 0; cc < files.size(); cc ++ ) for ( cc = 0; cc < files.size(); cc ++ )
{ {
if(!cmSystemTools::IsOff(files[cc].c_str())) if(!cmSystemTools::IsOff(files[cc].c_str()))
@ -1859,13 +1861,12 @@ void cmMakefile::AddCacheDefinition(const std::string& name, const char* value,
} }
this->GetCMakeInstance()->AddCacheEntry(name, nvalue.c_str(), doc, type); this->GetCMakeInstance()->AddCacheEntry(name, nvalue.c_str(), doc, type);
val = this->GetState()->GetInitializedCacheValue(name); nvalue = this->GetState()->GetInitializedCacheValue(name);
haveVal = true; value = nvalue.c_str();
} }
} }
this->GetCMakeInstance()->AddCacheEntry(name, haveVal ? val.c_str() : 0, this->GetCMakeInstance()->AddCacheEntry(name, value, doc, type);
doc, type);
// if there was a definition then remove it // if there was a definition then remove it
this->StateSnapshot.RemoveDefinition(name); this->StateSnapshot.RemoveDefinition(name);
} }