src: Improved performance of FilePathFilter (#1030)

* Only retrieve FilePath::wstr once per checked FilePath
* Replaced all invokation of FilePathFilter::isMatching with FilePathFilter::areMatching
* Don't pass match to std::regex_match
* set 'optimize' flag on regexes

partly fixes #1007
This commit is contained in:
Eberhard Gräther
2020-05-31 21:34:14 +02:00
committed by GitHub
parent b7ad802cc2
commit ef8b9e0031
7 changed files with 32 additions and 49 deletions
+1 -8
View File
@@ -96,14 +96,7 @@ std::set<FilePath> SourceGroup::filterToContainedFilePaths(
if (isInIndexedPaths)
{
for (const FilePathFilter& excludeFilter: excludeFilters)
{
if (excludeFilter.isMatching(filePath))
{
isInIndexedPaths = false;
break;
}
}
isInIndexedPaths = !FilePathFilter::areMatching(excludeFilters, filePath);
}
if (isInIndexedPaths)
+1 -9
View File
@@ -56,13 +56,5 @@ std::set<FilePath> FileManager::getAllSourceFilePaths() const
bool FileManager::isExcluded(const FilePath& filePath) const
{
for (const FilePathFilter& filter: m_excludeFilters)
{
if (filter.isMatching(filePath))
{
return true;
}
}
return false;
return FilePathFilter::areMatching(m_excludeFilters, filePath);
}
+7 -4
View File
@@ -12,9 +12,12 @@ std::wstring FilePathFilter::wstr() const
bool FilePathFilter::isMatching(const FilePath& filePath) const
{
const std::wstring s = filePath.wstr();
std::wsmatch match;
return std::regex_match(s, match, m_filterRegex);
return isMatching(filePath.wstr());
}
bool FilePathFilter::isMatching(const std::wstring& fileStr) const
{
return std::regex_match(fileStr, m_filterRegex);
}
bool FilePathFilter::operator<(const FilePathFilter& other) const
@@ -96,5 +99,5 @@ std::wregex FilePathFilter::convertFilterStringToRegex(const std::wstring& filte
regexFilterString = std::regex_replace(regexFilterString, regex, L"[^\\\\/]*");
}
return std::wregex(regexFilterString);
return std::wregex(regexFilterString, std::regex::optimize);
}
+20
View File
@@ -9,11 +9,15 @@
class FilePathFilter
{
public:
template<typename ContainerType>
static bool areMatching(const ContainerType& filters, const FilePath& filePath);
explicit FilePathFilter(const std::wstring& filterString);
std::wstring wstr() const;
bool isMatching(const FilePath& filePath) const;
bool isMatching(const std::wstring& fileStr) const;
bool operator<(const FilePathFilter& other) const;
@@ -24,4 +28,20 @@ private:
std::wregex m_filterRegex;
};
template<typename ContainerType>
bool FilePathFilter::areMatching(const ContainerType& filters, const FilePath& filePath)
{
const std::wstring fileStr = filePath.wstr();
for (const FilePathFilter& filter: filters)
{
if (filter.isMatching(fileStr))
{
return true;
}
}
return false;
}
#endif // FILE_PATH_FILTER_H
+1 -8
View File
@@ -44,14 +44,7 @@ FileRegister::FileRegister(
if (ret)
{
for (const FilePathFilter& excludeFilter: m_excludeFilters)
{
if (excludeFilter.isMatching(filePath))
{
ret = false;
break;
}
}
ret = !FilePathFilter::areMatching(m_excludeFilters, filePath);
}
return ret;
})
+1 -10
View File
@@ -64,16 +64,7 @@ std::set<FilePath> SourceGroupCxxCdb::getAllSourceFilePaths(
for (const FilePath& path: IndexerCommandCxx::getSourceFilesFromCDB(
cdb, m_settings->getCompilationDatabasePathExpandedAndAbsolute()))
{
bool excluded = false;
for (const FilePathFilter& filter: excludeFilters)
{
if (filter.isMatching(path))
{
excluded = true;
break;
}
}
bool excluded = FilePathFilter::areMatching(excludeFilters, path);
if (!excluded && path.exists())
{
sourceFilePaths.insert(path);
@@ -52,16 +52,7 @@ std::set<FilePath> SourceGroupCxxCodeblocks::getAllSourceFilePaths() const
for (const FilePath& filePath:
project->getAllSourceFilePathsCanonical(m_settings->getSourceExtensions()))
{
bool isExcluded = false;
for (const FilePathFilter& excludeFilter: excludeFilters)
{
if (excludeFilter.isMatching(filePath))
{
isExcluded = true;
break;
}
}
bool isExcluded = FilePathFilter::areMatching(excludeFilters, filePath);
if (!isExcluded && filePath.exists())
{
sourceFilePaths.insert(filePath);