From 39383ef8cb691656012275721064baef1e4f7511 Mon Sep 17 00:00:00 2001 From: Alexander Neundorf Date: Wed, 23 Sep 2009 14:02:05 -0400 Subject: 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 --- Source/cmDepends.cxx | 26 +++++++++++++++++++++--- Source/cmDepends.h | 13 ++++++++---- Source/cmDependsC.cxx | 35 ++++++++++++++++++++++++++++++-- Source/cmDependsC.h | 4 +++- Source/cmDependsJava.cxx | 3 ++- Source/cmDependsJava.h | 3 ++- Source/cmLocalUnixMakefileGenerator3.cxx | 26 +++++++++++++++++------- Source/cmLocalUnixMakefileGenerator3.h | 6 +++++- 8 files changed, 96 insertions(+), 20 deletions(-) diff --git a/Source/cmDepends.cxx b/Source/cmDepends.cxx index 40e9730..72199fc 100644 --- a/Source/cmDepends.cxx +++ b/Source/cmDepends.cxx @@ -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& 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& 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) { diff --git a/Source/cmDepends.h b/Source/cmDepends.h index 75afaaf..0fc66f8 100644 --- a/Source/cmDepends.h +++ b/Source/cmDepends.h @@ -59,12 +59,16 @@ public: /** Write dependencies for the target file. */ bool Write(std::ostream &makeDepends, std::ostream &internalDepends); - + + class DependencyVector: public std::vector {}; + /** 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& 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& validDeps); // Finalize the dependency information for the target. virtual bool Finalize(std::ostream& makeDepends, diff --git a/Source/cmDependsC.cxx b/Source/cmDependsC.cxx index b431b29..99d17aa 100644 --- a/Source/cmDependsC.cxx +++ b/Source/cmDependsC.cxx @@ -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* 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::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; diff --git a/Source/cmDependsC.h b/Source/cmDependsC.h index dafb112..0830cca 100644 --- a/Source/cmDependsC.h +++ b/Source/cmDependsC.h @@ -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* validDeps); /** Virtual destructor to cleanup subclasses properly. */ virtual ~cmDependsC(); @@ -83,6 +84,7 @@ public: bool Used; }; protected: + const std::map* ValidDeps; std::set Encountered; std::queue Unscanned; t_CharBuffer Buffer; diff --git a/Source/cmDependsJava.cxx b/Source/cmDependsJava.cxx index dbca276..b7b1e9a 100644 --- a/Source/cmDependsJava.cxx +++ b/Source/cmDependsJava.cxx @@ -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&) { return true; } diff --git a/Source/cmDependsJava.h b/Source/cmDependsJava.h index 9275130..1325830 100644 --- a/Source/cmDependsJava.h +++ b/Source/cmDependsJava.h @@ -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& validDeps); private: cmDependsJava(cmDependsJava const&); // Purposely not implemented. diff --git a/Source/cmLocalUnixMakefileGenerator3.cxx b/Source/cmLocalUnixMakefileGenerator3.cxx index 495a05a..db22d1d 100644 --- a/Source/cmLocalUnixMakefileGenerator3.cxx +++ b/Source/cmLocalUnixMakefileGenerator3.cxx @@ -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 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& 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") diff --git a/Source/cmLocalUnixMakefileGenerator3.h b/Source/cmLocalUnixMakefileGenerator3.h index ce6b45f..3a35191 100644 --- a/Source/cmLocalUnixMakefileGenerator3.h +++ b/Source/cmLocalUnixMakefileGenerator3.h @@ -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& validDeps); void CheckMultipleOutputs(bool verbose); private: -- cgit v0.12