From caee335388cb9aef673b759879e47e3badcc6deb Mon Sep 17 00:00:00 2001 From: Herman Semenov Date: Mon, 8 May 2023 03:21:31 +0300 Subject: [PATCH] fix: optimize using constexpr and templates --- src/logdata/include/compressedlinestorage.h | 3 ++- src/logdata/include/logfiltereddata.h | 3 ++- src/logdata/src/logfiltereddata.cpp | 5 +++-- src/ui/include/mainwindow.h | 3 ++- src/ui/src/crawlerwidget.cpp | 7 +++--- src/ui/src/displayfilepath.cpp | 18 ++++++++++----- src/ui/src/mainwindow.cpp | 25 ++++++++++++--------- 7 files changed, 39 insertions(+), 25 deletions(-) diff --git a/src/logdata/include/compressedlinestorage.h b/src/logdata/include/compressedlinestorage.h index c58ce6f97..3feb99798 100644 --- a/src/logdata/include/compressedlinestorage.h +++ b/src/logdata/include/compressedlinestorage.h @@ -138,7 +138,8 @@ class CompressedLinePositionStorage { // Cache the last position read // This is to speed up consecutive reads (whole page) struct Cache { - LineNumber index {std::numeric_limits::max() - 1U}; + static constexpr auto i = std::numeric_limits::max() - 1U; + LineNumber index {i}; LineOffset position {0}; BlockOffset offset {0}; }; diff --git a/src/logdata/include/logfiltereddata.h b/src/logdata/include/logfiltereddata.h index d1368fbd3..d9a6234ba 100644 --- a/src/logdata/include/logfiltereddata.h +++ b/src/logdata/include/logfiltereddata.h @@ -88,7 +88,8 @@ class LogFilteredData : public AbstractLogData { // Nothing is done if no search is in progress. void interruptSearch(); // Clear the search and the list of results. - void clearSearch( bool dropCache = false ); + template + void clearSearch(); // Returns the line number in the original LogData where the element // 'index' was found. diff --git a/src/logdata/src/logfiltereddata.cpp b/src/logdata/src/logfiltereddata.cpp index 9358c2834..9fcb52198 100644 --- a/src/logdata/src/logfiltereddata.cpp +++ b/src/logdata/src/logfiltereddata.cpp @@ -150,7 +150,8 @@ void LogFilteredData::interruptSearch() workerThread_.interrupt(); } -void LogFilteredData::clearSearch( bool dropCache ) +template +void LogFilteredData::clearSearch() { interruptSearch(); @@ -160,7 +161,7 @@ void LogFilteredData::clearSearch( bool dropCache ) maxLength_ = 0_length; nbLinesProcessed_ = 0_lcount; - if ( dropCache ) { + if constexpr ( dropCache ) { searchResultsCache_.clear(); } } diff --git a/src/ui/include/mainwindow.h b/src/ui/include/mainwindow.h index 16cddd93b..782699c11 100644 --- a/src/ui/include/mainwindow.h +++ b/src/ui/include/mainwindow.h @@ -208,7 +208,8 @@ class MainWindow : public QMainWindow { void logScreenInfo( QScreen* screen ); void removeFromFavorites( const QString& pathToRemove ); void removeFromRecent( const QString& pathToRemove ); - void tryOpenClipboard( int tryTimes ); + template + void tryOpenClipboard(); void updateShortcuts(); WindowSession session_; diff --git a/src/ui/src/crawlerwidget.cpp b/src/ui/src/crawlerwidget.cpp index b62f8c503..649c900b1 100644 --- a/src/ui/src/crawlerwidget.cpp +++ b/src/ui/src/crawlerwidget.cpp @@ -265,7 +265,7 @@ void CrawlerWidget::reload() { searchState_.resetState(); constexpr auto DropCache = true; - logFilteredData_->clearSearch( DropCache ); + logFilteredData_->clearSearch< DropCache >(); logFilteredData_->clearMarks(); filteredView_->updateData(); printSearchInfoMessage(); @@ -698,7 +698,7 @@ void CrawlerWidget::fileChangedHandler( MonitoredFileStatus status ) if ( !searchInfoLine_->text().isEmpty() ) { // Invalidate the search constexpr auto DropCache = true; - logFilteredData_->clearSearch( DropCache ); + logFilteredData_->clearSearch< DropCache >(); filteredView_->updateData(); searchState_.truncateFile(); printSearchInfoMessage(); @@ -1033,7 +1033,8 @@ void CrawlerWidget::setup() searchLineEdit_->addItems( savedSearches_->recentSearches() ); searchLineEdit_->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum ); searchLineEdit_->setSizeAdjustPolicy( QComboBox::AdjustToMinimumContentsLengthWithIcon ); - searchLineEdit_->lineEdit()->setMaxLength( std::numeric_limits::max() / 1024 ); + constexpr auto maxLength = std::numeric_limits::max() / 1024; + searchLineEdit_->lineEdit()->setMaxLength( maxLength ); searchLineEdit_->setContentsMargins( 2, 2, 2, 2 ); QAction* clearSearchHistoryAction = new QAction( "Clear search history", this ); diff --git a/src/ui/src/displayfilepath.cpp b/src/ui/src/displayfilepath.cpp index 754680b80..6bad11017 100644 --- a/src/ui/src/displayfilepath.cpp +++ b/src/ui/src/displayfilepath.cpp @@ -24,20 +24,25 @@ constexpr const int MaxPathLength = 128; namespace { // inspired by http://chadkuehn.com/shrink-file-paths-with-an-ellipsis-in-c/ -QString shrinkPath( QString fullPath, int limit, QString delimiter = "…" ) +template +QString shrinkPath( const QString& fullPath ) { if ( fullPath.isEmpty() ) { return fullPath; } + constexpr const auto delimiter = "…"; + constexpr auto delimiterLen = + static_cast( std::char_traits::length( delimiter ) ); + const auto fileInfo = QFileInfo( fullPath ); const auto fileName = fileInfo.fileName(); const auto absoluteNativePath = QDir::toNativeSeparators( fileInfo.absolutePath() ); - const auto idealMinLength = fileName.length() + delimiter.length(); + const auto idealMinLength = fileName.length() + delimiterLen; // less than the minimum amt - if ( limit < ( ( 2 * delimiter.length() ) + 1 ) ) { + if constexpr ( limit < ( ( 2 * delimiterLen ) + 1 ) ) { return ""; } @@ -48,7 +53,8 @@ QString shrinkPath( QString fullPath, int limit, QString delimiter = "…" ) // file name condensing if ( limit < idealMinLength ) { - return delimiter + fileName.mid( 0, ( limit - ( 2 * delimiter.length() ) ) ) + delimiter; + constexpr auto n = ( limit - ( 2 * delimiterLen ) ); + return delimiter + fileName.mid( 0, n ) + delimiter; } // whole name only, no folder structure shown @@ -56,7 +62,7 @@ QString shrinkPath( QString fullPath, int limit, QString delimiter = "…" ) return delimiter + fileName; } - return absoluteNativePath.mid( 0, ( limit - ( idealMinLength + 1 ) ) ) + delimiter + return absoluteNativePath.mid( 0, ( static_cast( limit ) - ( idealMinLength + 1 ) ) ) + delimiter + QDir::separator() + fileName; } @@ -65,7 +71,7 @@ QString shrinkPath( QString fullPath, int limit, QString delimiter = "…" ) DisplayFilePath::DisplayFilePath( const QString& fullPath ) : fullPath_( fullPath ) , nativeFullPath_( QDir::toNativeSeparators( fullPath ) ) - , displayName_( shrinkPath( fullPath, MaxPathLength ) ) + , displayName_( shrinkPath( fullPath ) ) { } diff --git a/src/ui/src/mainwindow.cpp b/src/ui/src/mainwindow.cpp index cb2657838..0730de8f5 100644 --- a/src/ui/src/mainwindow.cpp +++ b/src/ui/src/mainwindow.cpp @@ -915,28 +915,31 @@ void MainWindow::openInEditor() openFileInDefaultApplication( session_.getFilename( currentCrawlerWidget() ) ); } -void MainWindow::tryOpenClipboard( int tryTimes ) +template +void MainWindow::tryOpenClipboard() { auto clipboard = QGuiApplication::clipboard(); auto text = clipboard->text(); - if ( text.isEmpty() && tryTimes > 0 ) { - QTimer::singleShot( 50, [ tryTimes, this ]() { tryOpenClipboard( tryTimes - 1 ); } ); - } - else { - auto tempFile = new QTemporaryFile( tempDir_.filePath( "klogg_clipboard" ), this ); - if ( tempFile->open() ) { - tempFile->write( text.toUtf8() ); - tempFile->flush(); + if constexpr ( tryTimes > 0 ) { + if ( text.isEmpty() ) { + QTimer::singleShot( 50, [ this ]() { tryOpenClipboard< tryTimes - 1 >(); } ); + } + else { + auto tempFile = new QTemporaryFile( tempDir_.filePath( "klogg_clipboard" ), this ); + if ( tempFile->open() ) { + tempFile->write( text.toUtf8() ); + tempFile->flush(); - loadFile( tempFile->fileName() ); + loadFile( tempFile->fileName() ); + } } } } void MainWindow::openClipboard() { - tryOpenClipboard( ClipboardMaxTry ); + tryOpenClipboard< ClipboardMaxTry >(); } void MainWindow::openUrl()