Major optimization of C/C++ dependency scanning.

Now only the dependencies for the file where the dependencies actually may
have changed are rescanned, before that this was done for all source files
even if only one source file had changed.
This reduces e.g. on my machine the time for scanning the dependencies
of kdelibs/khtml/ when only one file (khtml_global.cpp) has changed from
around 7.5 seconds to 1.2 seconds.

The tests succeed, it does what I expected it to do on kdelibs, and Brad
also reviewed the patch, so I think it should be ok.

Alex
This commit is contained in:
Alexander Neundorf 2009-09-23 14:02:05 -04:00
parent 551fcc23c2
commit 39383ef8cb
8 changed files with 96 additions and 20 deletions

View File

@ -87,7 +87,8 @@ bool cmDepends::Finalize(std::ostream&,
}
//----------------------------------------------------------------------------
bool cmDepends::Check(const char *makeFile, const char *internalFile)
bool cmDepends::Check(const char *makeFile, const char *internalFile,
std::map<std::string, DependencyVector>& validDeps)
{
// Dependency checks must be done in proper working directory.
std::string oldcwd = ".";
@ -102,7 +103,7 @@ bool cmDepends::Check(const char *makeFile, const char *internalFile)
// Check whether dependencies must be regenerated.
bool okay = true;
std::ifstream fin(internalFile);
if(!(fin && this->CheckDependencies(fin)))
if(!(fin && this->CheckDependencies(fin, validDeps)))
{
// Clear all dependencies so they will be regenerated.
this->Clear(makeFile);
@ -146,13 +147,16 @@ bool cmDepends::WriteDependencies(const char*, const char*,
}
//----------------------------------------------------------------------------
bool cmDepends::CheckDependencies(std::istream& internalDepends)
bool cmDepends::CheckDependencies(std::istream& internalDepends,
std::map<std::string, DependencyVector>& validDeps)
{
// Parse dependencies from the stream. If any dependee is missing
// or newer than the depender then dependencies should be
// regenerated.
bool okay = true;
bool dependerExists = false;
DependencyVector* currentDependencies = 0;
while(internalDepends.getline(this->Dependee, this->MaxPath))
{
if ( this->Dependee[0] == 0 || this->Dependee[0] == '#' ||
@ -174,6 +178,9 @@ bool cmDepends::CheckDependencies(std::istream& internalDepends)
// kdelibs/khtml this reduces the number of calls from 184k down to 92k,
// or the time for cmake -E cmake_depends from 0.3 s down to 0.21 s.
dependerExists = cmSystemTools::FileExists(this->Depender);
DependencyVector tmp;
validDeps[this->Depender] = tmp;
currentDependencies = &validDeps[this->Depender];
continue;
}
/*
@ -189,6 +196,11 @@ bool cmDepends::CheckDependencies(std::istream& internalDepends)
bool regenerate = false;
const char* dependee = this->Dependee+1;
const char* depender = this->Depender;
if (currentDependencies != 0)
{
currentDependencies->push_back(dependee);
}
if(!cmSystemTools::FileExists(dependee))
{
// The dependee does not exist.
@ -230,6 +242,14 @@ bool cmDepends::CheckDependencies(std::istream& internalDepends)
// Dependencies must be regenerated.
okay = false;
// Remove the information of this depender from the map, it needs
// to be rescanned
if (currentDependencies != 0)
{
validDeps.erase(this->Depender);
currentDependencies = 0;
}
// Remove the depender to be sure it is rebuilt.
if (dependerExists)
{

View File

@ -59,12 +59,16 @@ public:
/** Write dependencies for the target file. */
bool Write(std::ostream &makeDepends, std::ostream &internalDepends);
class DependencyVector: public std::vector<std::string> {};
/** Check dependencies for the target file. Returns true if
dependencies are okay and false if they must be generated. If
they must be generated Clear has already been called to wipe out
the old dependencies. */
bool Check(const char *makeFile, const char* internalFile);
the old dependencies.
Dependencies which are still valid will be stored in validDeps. */
bool Check(const char *makeFile, const char* internalFile,
std::map<std::string, DependencyVector>& validDeps);
/** Clear dependencies for the target file so they will be regenerated. */
void Clear(const char *file);
@ -83,7 +87,8 @@ protected:
// Check dependencies for the target file in the given stream.
// Return false if dependencies must be regenerated and true
// otherwise.
virtual bool CheckDependencies(std::istream& internalDepends);
virtual bool CheckDependencies(std::istream& internalDepends,
std::map<std::string, DependencyVector>& validDeps);
// Finalize the dependency information for the target.
virtual bool Finalize(std::ostream& makeDepends,

View File

@ -34,12 +34,17 @@
//----------------------------------------------------------------------------
cmDependsC::cmDependsC()
: ValidDeps(0)
{
}
//----------------------------------------------------------------------------
cmDependsC::cmDependsC(cmLocalGenerator* lg, const char* targetDir,
const char* lang): cmDepends(lg, targetDir)
cmDependsC::cmDependsC(cmLocalGenerator* lg,
const char* targetDir,
const char* lang,
const std::map<std::string, DependencyVector>* validDeps)
: cmDepends(lg, targetDir)
, ValidDeps(validDeps)
{
cmMakefile* mf = lg->GetMakefile();
@ -113,6 +118,32 @@ bool cmDependsC::WriteDependencies(const char *src, const char *obj,
return false;
}
if (this->ValidDeps != 0)
{
std::map<std::string, DependencyVector>::const_iterator tmpIt =
this->ValidDeps->find(obj);
if (tmpIt!= this->ValidDeps->end())
{
// Write the dependencies to the output stream. Makefile rules
// written by the original local generator for this directory
// convert the dependencies to paths relative to the home output
// directory. We must do the same here.
internalDepends << obj << std::endl;
for(DependencyVector::const_iterator i=tmpIt->second.begin();
i != tmpIt->second.end(); ++i)
{
makeDepends << obj << ": " <<
this->LocalGenerator->Convert(i->c_str(),
cmLocalGenerator::HOME_OUTPUT,
cmLocalGenerator::MAKEFILE)
<< std::endl;
internalDepends << " " << i->c_str() << std::endl;
}
makeDepends << std::endl;
return true;
}
}
// Walk the dependency graph starting with the source file.
bool first = true;
UnscannedEntry root;

View File

@ -30,7 +30,8 @@ public:
/** Checking instances need to know the build directory name and the
relative path from the build directory to the target file. */
cmDependsC();
cmDependsC(cmLocalGenerator* lg, const char* targetDir, const char* lang);
cmDependsC(cmLocalGenerator* lg, const char* targetDir, const char* lang,
const std::map<std::string, DependencyVector>* validDeps);
/** Virtual destructor to cleanup subclasses properly. */
virtual ~cmDependsC();
@ -83,6 +84,7 @@ public:
bool Used;
};
protected:
const std::map<std::string, DependencyVector>* ValidDeps;
std::set<cmStdString> Encountered;
std::queue<UnscannedEntry> Unscanned;
t_CharBuffer Buffer;

View File

@ -43,7 +43,8 @@ bool cmDependsJava::WriteDependencies(const char *src, const char *,
return true;
}
bool cmDependsJava::CheckDependencies(std::istream&)
bool cmDependsJava::CheckDependencies(std::istream&,
std::map<std::string, DependencyVector >&)
{
return true;
}

View File

@ -36,7 +36,8 @@ protected:
// Implement writing/checking methods required by superclass.
virtual bool WriteDependencies(const char *src, const char *file,
std::ostream& makeDepends, std::ostream& internalDepends);
virtual bool CheckDependencies(std::istream& internalDepends);
virtual bool CheckDependencies(std::istream& internalDepends,
std::map<std::string, DependencyVector >& validDeps);
private:
cmDependsJava(cmDependsJava const&); // Purposely not implemented.

View File

@ -16,7 +16,6 @@
=========================================================================*/
#include "cmLocalUnixMakefileGenerator3.h"
#include "cmDepends.h"
#include "cmGeneratedFileStream.h"
#include "cmGlobalUnixMakefileGenerator3.h"
#include "cmMakefile.h"
@ -1387,9 +1386,9 @@ bool cmLocalUnixMakefileGenerator3::UpdateDependencies(const char* tgtInfo,
if(verbose)
{
cmOStringStream msg;
msg << "Dependee \"" << internalDependFile
msg << "Dependee \"" << dirInfoFile
<< "\" is newer than depender \""
<< dirInfoFile << "\"." << std::endl;
<< internalDependFile << "\"." << std::endl;
cmSystemTools::Stdout(msg.str().c_str());
}
needRescanDirInfo = true;
@ -1400,14 +1399,26 @@ bool cmLocalUnixMakefileGenerator3::UpdateDependencies(const char* tgtInfo,
// The build.make file may have explicit dependencies for the object
// files but these will not affect the scanning process so they need
// not be considered.
std::map<std::string, cmDepends::DependencyVector> validDependencies;
bool needRescanDependencies = false;
if (needRescanDirInfo == false)
{
cmDependsC checker;
checker.SetVerbose(verbose);
checker.SetFileComparison(ftc);
// cmDependsC::Check() fills the vector validDependencies() with the
// dependencies for those files where they are still valid, i.e. neither
// the files themselves nor any files they depend on have changed.
// We don't do that if the CMakeDirectoryInformation.cmake file has
// changed, because then potentially all dependencies have changed.
// This information is given later on to cmDependsC, which then only
// rescans the files where it did not get valid dependencies via this
// dependency vector. This means that in the normal case, when only
// few or one file have been edited, then also only this one file is
// actually scanned again, instead of all files for this target.
needRescanDependencies = !checker.Check(dependFile.c_str(),
internalDependFile.c_str());
internalDependFile.c_str(),
validDependencies);
}
if(needRescanDependInfo || needRescanDirInfo || needRescanDependencies)
@ -1426,7 +1437,7 @@ bool cmLocalUnixMakefileGenerator3::UpdateDependencies(const char* tgtInfo,
fprintf(stdout, "%s\n", message.c_str());
#endif
return this->ScanDependencies(dir.c_str());
return this->ScanDependencies(dir.c_str(), validDependencies);
}
else
{
@ -1438,7 +1449,8 @@ bool cmLocalUnixMakefileGenerator3::UpdateDependencies(const char* tgtInfo,
//----------------------------------------------------------------------------
bool
cmLocalUnixMakefileGenerator3
::ScanDependencies(const char* targetDir)
::ScanDependencies(const char* targetDir,
std::map<std::string, cmDepends::DependencyVector>& validDeps)
{
// Read the directory information file.
cmMakefile* mf = this->Makefile;
@ -1526,7 +1538,7 @@ cmLocalUnixMakefileGenerator3
if(lang == "C" || lang == "CXX" || lang == "RC")
{
// TODO: Handle RC (resource files) dependencies correctly.
scanner = new cmDependsC(this, targetDir, lang.c_str());
scanner = new cmDependsC(this, targetDir, lang.c_str(), &validDeps);
}
#ifdef CMAKE_BUILD_WITH_CMAKE
else if(lang == "Fortran")

View File

@ -19,6 +19,9 @@
#include "cmLocalGenerator.h"
// for cmDepends::DependencyVector
#include "cmDepends.h"
class cmCustomCommand;
class cmDependInformation;
class cmDepends;
@ -343,7 +346,8 @@ protected:
cmTarget& target, const char* filename =0);
// Helper methods for dependeny updates.
bool ScanDependencies(const char* targetDir);
bool ScanDependencies(const char* targetDir,
std::map<std::string, cmDepends::DependencyVector>& validDeps);
void CheckMultipleOutputs(bool verbose);
private: