From ef8b9e00317383ea5b647d084631b517df81c0f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eberhard=20Gr=C3=A4ther?= Date: Sun, 31 May 2020 21:34:14 +0200 Subject: [PATCH] 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 --- src/lib/project/SourceGroup.cpp | 9 +-------- src/lib/utility/file/FileManager.cpp | 10 +--------- src/lib/utility/file/FilePathFilter.cpp | 11 ++++++---- src/lib/utility/file/FilePathFilter.h | 20 +++++++++++++++++++ src/lib/utility/file/FileRegister.cpp | 9 +-------- src/lib_cxx/project/SourceGroupCxxCdb.cpp | 11 +--------- .../project/SourceGroupCxxCodeblocks.cpp | 11 +--------- 7 files changed, 32 insertions(+), 49 deletions(-) diff --git a/src/lib/project/SourceGroup.cpp b/src/lib/project/SourceGroup.cpp index c1164e48..87de69b7 100644 --- a/src/lib/project/SourceGroup.cpp +++ b/src/lib/project/SourceGroup.cpp @@ -96,14 +96,7 @@ std::set SourceGroup::filterToContainedFilePaths( if (isInIndexedPaths) { - for (const FilePathFilter& excludeFilter: excludeFilters) - { - if (excludeFilter.isMatching(filePath)) - { - isInIndexedPaths = false; - break; - } - } + isInIndexedPaths = !FilePathFilter::areMatching(excludeFilters, filePath); } if (isInIndexedPaths) diff --git a/src/lib/utility/file/FileManager.cpp b/src/lib/utility/file/FileManager.cpp index cf7cc28e..9601e1d8 100644 --- a/src/lib/utility/file/FileManager.cpp +++ b/src/lib/utility/file/FileManager.cpp @@ -56,13 +56,5 @@ std::set 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); } diff --git a/src/lib/utility/file/FilePathFilter.cpp b/src/lib/utility/file/FilePathFilter.cpp index c27a3cfb..afb422fe 100644 --- a/src/lib/utility/file/FilePathFilter.cpp +++ b/src/lib/utility/file/FilePathFilter.cpp @@ -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); } diff --git a/src/lib/utility/file/FilePathFilter.h b/src/lib/utility/file/FilePathFilter.h index b204fd0a..0040d68a 100644 --- a/src/lib/utility/file/FilePathFilter.h +++ b/src/lib/utility/file/FilePathFilter.h @@ -9,11 +9,15 @@ class FilePathFilter { public: + template + 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 +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 diff --git a/src/lib/utility/file/FileRegister.cpp b/src/lib/utility/file/FileRegister.cpp index 39dabe14..d4e7d3f9 100644 --- a/src/lib/utility/file/FileRegister.cpp +++ b/src/lib/utility/file/FileRegister.cpp @@ -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; }) diff --git a/src/lib_cxx/project/SourceGroupCxxCdb.cpp b/src/lib_cxx/project/SourceGroupCxxCdb.cpp index 7df59e9e..cd4ecbc1 100644 --- a/src/lib_cxx/project/SourceGroupCxxCdb.cpp +++ b/src/lib_cxx/project/SourceGroupCxxCdb.cpp @@ -64,16 +64,7 @@ std::set 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); diff --git a/src/lib_cxx/project/SourceGroupCxxCodeblocks.cpp b/src/lib_cxx/project/SourceGroupCxxCodeblocks.cpp index 683cee4e..8d9ee6ce 100644 --- a/src/lib_cxx/project/SourceGroupCxxCodeblocks.cpp +++ b/src/lib_cxx/project/SourceGroupCxxCodeblocks.cpp @@ -52,16 +52,7 @@ std::set 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);