Implement a minor optimization when loading module maps to satisfy a

module import: don't re-check for a loaded module unless we've
actually loaded a new module map file. Already-loaded module map files
aren't interesting.

llvm-svn: 144435
This commit is contained in:
Douglas Gregor 2011-11-12 00:22:19 +00:00
parent 48032f9cde
commit 80b6904baf
2 changed files with 46 additions and 18 deletions

View File

@ -408,15 +408,35 @@ public:
size_t getTotalMemory() const; size_t getTotalMemory() const;
private: private:
/// \brief Describes what happened when we tried to load a module map file.
enum LoadModuleMapResult {
/// \brief The module map file had already been loaded.
LMM_AlreadyLoaded,
/// \brief The module map file was loaded by this invocation.
LMM_NewlyLoaded,
/// \brief There is was directory with the given name.
LMM_NoDirectory,
/// \brief There was either no module map file or the module map file was
/// invalid.
LMM_InvalidModuleMap
};
/// \brief Try to load the module map file in the given directory. /// \brief Try to load the module map file in the given directory.
/// ///
/// \returns false if the module map was loaded successfully, true otherwise. /// \param DirName The name of the directory where we will look for a module
bool loadModuleMapFile(StringRef DirName); /// map file.
///
/// \returns The result of attempting to load the module map file from the
/// named directory.
LoadModuleMapResult loadModuleMapFile(StringRef DirName);
/// \brief Try to load the module map file in the given directory. /// \brief Try to load the module map file in the given directory.
/// ///
/// \returns false if the module map was loaded successfully, true otherwise. /// \param Dir The directory where we will look for a module map file.
bool loadModuleMapFile(const DirectoryEntry *Dir); ///
/// \returns The result of attempting to load the module map file from the
/// named directory.
LoadModuleMapResult loadModuleMapFile(const DirectoryEntry *Dir);
/// getFileInfo - Return the HeaderFileInfo structure for the specified /// getFileInfo - Return the HeaderFileInfo structure for the specified
/// FileEntry. /// FileEntry.

View File

@ -137,22 +137,22 @@ const FileEntry *HeaderSearch::lookupModule(StringRef ModuleName,
if (!SearchDirs[Idx].isNormalDir()) if (!SearchDirs[Idx].isNormalDir())
continue; continue;
// Search for a module map in this directory, if we haven't already // Search for a module map file in this directory.
// looked there. if (loadModuleMapFile(SearchDirs[Idx].getDir()) == LMM_NewlyLoaded) {
if (!loadModuleMapFile(SearchDirs[Idx].getDir())) { // We just loaded a module map file; check whether the module is
// If we found a module map, look for the module again. // available now.
Module = ModMap.findModule(ModuleName); Module = ModMap.findModule(ModuleName);
if (Module) if (Module)
break; break;
} }
// Search for a module map in a subdirectory with the same name as the // Search for a module map in a subdirectory with the same name as the
// module. // module.
llvm::SmallString<128> NestedModuleMapDirName; llvm::SmallString<128> NestedModuleMapDirName;
NestedModuleMapDirName = SearchDirs[Idx].getDir()->getName(); NestedModuleMapDirName = SearchDirs[Idx].getDir()->getName();
llvm::sys::path::append(NestedModuleMapDirName, ModuleName); llvm::sys::path::append(NestedModuleMapDirName, ModuleName);
if (!loadModuleMapFile(NestedModuleMapDirName)) { if (loadModuleMapFile(NestedModuleMapDirName) == LMM_NewlyLoaded) {
// If we found a module map, look for the module again. // If we just loaded a module map file, look for the module again.
Module = ModMap.findModule(ModuleName); Module = ModMap.findModule(ModuleName);
if (Module) if (Module)
break; break;
@ -766,13 +766,19 @@ bool HeaderSearch::hasModuleMap(StringRef FileName,
return false; return false;
// Try to load the module map file in this directory. // Try to load the module map file in this directory.
if (!loadModuleMapFile(Dir)) { switch (loadModuleMapFile(Dir)) {
case LMM_NewlyLoaded:
case LMM_AlreadyLoaded:
// Success. All of the directories we stepped through inherit this module // Success. All of the directories we stepped through inherit this module
// map file. // map file.
for (unsigned I = 0, N = FixUpDirectories.size(); I != N; ++I) for (unsigned I = 0, N = FixUpDirectories.size(); I != N; ++I)
DirectoryHasModuleMap[FixUpDirectories[I]] = true; DirectoryHasModuleMap[FixUpDirectories[I]] = true;
return true; return true;
case LMM_NoDirectory:
case LMM_InvalidModuleMap:
break;
} }
// If we hit the top of our search, we're done. // If we hit the top of our search, we're done.
@ -794,18 +800,20 @@ StringRef HeaderSearch::findModuleForHeader(const FileEntry *File) {
return StringRef(); return StringRef();
} }
bool HeaderSearch::loadModuleMapFile(StringRef DirName) { HeaderSearch::LoadModuleMapResult
HeaderSearch::loadModuleMapFile(StringRef DirName) {
if (const DirectoryEntry *Dir = FileMgr.getDirectory(DirName)) if (const DirectoryEntry *Dir = FileMgr.getDirectory(DirName))
return loadModuleMapFile(Dir); return loadModuleMapFile(Dir);
return true; return LMM_NoDirectory;
} }
bool HeaderSearch::loadModuleMapFile(const DirectoryEntry *Dir) { HeaderSearch::LoadModuleMapResult
HeaderSearch::loadModuleMapFile(const DirectoryEntry *Dir) {
llvm::DenseMap<const DirectoryEntry *, bool>::iterator KnownDir llvm::DenseMap<const DirectoryEntry *, bool>::iterator KnownDir
= DirectoryHasModuleMap.find(Dir); = DirectoryHasModuleMap.find(Dir);
if (KnownDir != DirectoryHasModuleMap.end()) if (KnownDir != DirectoryHasModuleMap.end())
return !KnownDir->second; return KnownDir->second? LMM_AlreadyLoaded : LMM_InvalidModuleMap;
llvm::SmallString<128> ModuleMapFileName; llvm::SmallString<128> ModuleMapFileName;
ModuleMapFileName += Dir->getName(); ModuleMapFileName += Dir->getName();
@ -816,12 +824,12 @@ bool HeaderSearch::loadModuleMapFile(const DirectoryEntry *Dir) {
// This directory has a module map. // This directory has a module map.
DirectoryHasModuleMap[Dir] = true; DirectoryHasModuleMap[Dir] = true;
return false; return LMM_NewlyLoaded;
} }
} }
// No suitable module map. // No suitable module map.
DirectoryHasModuleMap[Dir] = false; DirectoryHasModuleMap[Dir] = false;
return true; return LMM_InvalidModuleMap;
} }