Skip to content

Commit caee335

Browse files
committed
fix: optimize using constexpr and templates
1 parent 5763371 commit caee335

File tree

7 files changed

+39
-25
lines changed

7 files changed

+39
-25
lines changed

src/logdata/include/compressedlinestorage.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ class CompressedLinePositionStorage {
138138
// Cache the last position read
139139
// This is to speed up consecutive reads (whole page)
140140
struct Cache {
141-
LineNumber index {std::numeric_limits<LineNumber::UnderlyingType>::max() - 1U};
141+
static constexpr auto i = std::numeric_limits<LineNumber::UnderlyingType>::max() - 1U;
142+
LineNumber index {i};
142143
LineOffset position {0};
143144
BlockOffset offset {0};
144145
};

src/logdata/include/logfiltereddata.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ class LogFilteredData : public AbstractLogData {
8888
// Nothing is done if no search is in progress.
8989
void interruptSearch();
9090
// Clear the search and the list of results.
91-
void clearSearch( bool dropCache = false );
91+
template <bool dropCache = false>
92+
void clearSearch();
9293

9394
// Returns the line number in the original LogData where the element
9495
// 'index' was found.

src/logdata/src/logfiltereddata.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ void LogFilteredData::interruptSearch()
150150
workerThread_.interrupt();
151151
}
152152

153-
void LogFilteredData::clearSearch( bool dropCache )
153+
template <bool dropCache>
154+
void LogFilteredData::clearSearch()
154155
{
155156
interruptSearch();
156157

@@ -160,7 +161,7 @@ void LogFilteredData::clearSearch( bool dropCache )
160161
maxLength_ = 0_length;
161162
nbLinesProcessed_ = 0_lcount;
162163

163-
if ( dropCache ) {
164+
if constexpr ( dropCache ) {
164165
searchResultsCache_.clear();
165166
}
166167
}

src/ui/include/mainwindow.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,8 @@ class MainWindow : public QMainWindow {
208208
void logScreenInfo( QScreen* screen );
209209
void removeFromFavorites( const QString& pathToRemove );
210210
void removeFromRecent( const QString& pathToRemove );
211-
void tryOpenClipboard( int tryTimes );
211+
template<int tryTimes>
212+
void tryOpenClipboard();
212213
void updateShortcuts();
213214

214215
WindowSession session_;

src/ui/src/crawlerwidget.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ void CrawlerWidget::reload()
265265
{
266266
searchState_.resetState();
267267
constexpr auto DropCache = true;
268-
logFilteredData_->clearSearch( DropCache );
268+
logFilteredData_->clearSearch< DropCache >();
269269
logFilteredData_->clearMarks();
270270
filteredView_->updateData();
271271
printSearchInfoMessage();
@@ -698,7 +698,7 @@ void CrawlerWidget::fileChangedHandler( MonitoredFileStatus status )
698698
if ( !searchInfoLine_->text().isEmpty() ) {
699699
// Invalidate the search
700700
constexpr auto DropCache = true;
701-
logFilteredData_->clearSearch( DropCache );
701+
logFilteredData_->clearSearch< DropCache >();
702702
filteredView_->updateData();
703703
searchState_.truncateFile();
704704
printSearchInfoMessage();
@@ -1033,7 +1033,8 @@ void CrawlerWidget::setup()
10331033
searchLineEdit_->addItems( savedSearches_->recentSearches() );
10341034
searchLineEdit_->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum );
10351035
searchLineEdit_->setSizeAdjustPolicy( QComboBox::AdjustToMinimumContentsLengthWithIcon );
1036-
searchLineEdit_->lineEdit()->setMaxLength( std::numeric_limits<int>::max() / 1024 );
1036+
constexpr auto maxLength = std::numeric_limits<int>::max() / 1024;
1037+
searchLineEdit_->lineEdit()->setMaxLength( maxLength );
10371038
searchLineEdit_->setContentsMargins( 2, 2, 2, 2 );
10381039

10391040
QAction* clearSearchHistoryAction = new QAction( "Clear search history", this );

src/ui/src/displayfilepath.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,25 @@
2424
constexpr const int MaxPathLength = 128;
2525
namespace {
2626
// inspired by http://chadkuehn.com/shrink-file-paths-with-an-ellipsis-in-c/
27-
QString shrinkPath( QString fullPath, int limit, QString delimiter = "" )
27+
template <int limit>
28+
QString shrinkPath( const QString& fullPath )
2829
{
2930
if ( fullPath.isEmpty() ) {
3031
return fullPath;
3132
}
3233

34+
constexpr const auto delimiter = "";
35+
constexpr auto delimiterLen =
36+
static_cast<qsizetype>( std::char_traits<char>::length( delimiter ) );
37+
3338
const auto fileInfo = QFileInfo( fullPath );
3439
const auto fileName = fileInfo.fileName();
3540
const auto absoluteNativePath = QDir::toNativeSeparators( fileInfo.absolutePath() );
3641

37-
const auto idealMinLength = fileName.length() + delimiter.length();
42+
const auto idealMinLength = fileName.length() + delimiterLen;
3843

3944
// less than the minimum amt
40-
if ( limit < ( ( 2 * delimiter.length() ) + 1 ) ) {
45+
if constexpr ( limit < ( ( 2 * delimiterLen ) + 1 ) ) {
4146
return "";
4247
}
4348

@@ -48,15 +53,16 @@ QString shrinkPath( QString fullPath, int limit, QString delimiter = "…" )
4853

4954
// file name condensing
5055
if ( limit < idealMinLength ) {
51-
return delimiter + fileName.mid( 0, ( limit - ( 2 * delimiter.length() ) ) ) + delimiter;
56+
constexpr auto n = ( limit - ( 2 * delimiterLen ) );
57+
return delimiter + fileName.mid( 0, n ) + delimiter;
5258
}
5359

5460
// whole name only, no folder structure shown
5561
if ( limit == idealMinLength ) {
5662
return delimiter + fileName;
5763
}
5864

59-
return absoluteNativePath.mid( 0, ( limit - ( idealMinLength + 1 ) ) ) + delimiter
65+
return absoluteNativePath.mid( 0, ( static_cast<qsizetype>( limit ) - ( idealMinLength + 1 ) ) ) + delimiter
6066
+ QDir::separator() + fileName;
6167
}
6268

@@ -65,7 +71,7 @@ QString shrinkPath( QString fullPath, int limit, QString delimiter = "…" )
6571
DisplayFilePath::DisplayFilePath( const QString& fullPath )
6672
: fullPath_( fullPath )
6773
, nativeFullPath_( QDir::toNativeSeparators( fullPath ) )
68-
, displayName_( shrinkPath( fullPath, MaxPathLength ) )
74+
, displayName_( shrinkPath<MaxPathLength>( fullPath ) )
6975
{
7076
}
7177

src/ui/src/mainwindow.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -915,28 +915,31 @@ void MainWindow::openInEditor()
915915
openFileInDefaultApplication( session_.getFilename( currentCrawlerWidget() ) );
916916
}
917917

918-
void MainWindow::tryOpenClipboard( int tryTimes )
918+
template <int tryTimes>
919+
void MainWindow::tryOpenClipboard()
919920
{
920921
auto clipboard = QGuiApplication::clipboard();
921922
auto text = clipboard->text();
922923

923-
if ( text.isEmpty() && tryTimes > 0 ) {
924-
QTimer::singleShot( 50, [ tryTimes, this ]() { tryOpenClipboard( tryTimes - 1 ); } );
925-
}
926-
else {
927-
auto tempFile = new QTemporaryFile( tempDir_.filePath( "klogg_clipboard" ), this );
928-
if ( tempFile->open() ) {
929-
tempFile->write( text.toUtf8() );
930-
tempFile->flush();
924+
if constexpr ( tryTimes > 0 ) {
925+
if ( text.isEmpty() ) {
926+
QTimer::singleShot( 50, [ this ]() { tryOpenClipboard< tryTimes - 1 >(); } );
927+
}
928+
else {
929+
auto tempFile = new QTemporaryFile( tempDir_.filePath( "klogg_clipboard" ), this );
930+
if ( tempFile->open() ) {
931+
tempFile->write( text.toUtf8() );
932+
tempFile->flush();
931933

932-
loadFile( tempFile->fileName() );
934+
loadFile( tempFile->fileName() );
935+
}
933936
}
934937
}
935938
}
936939

937940
void MainWindow::openClipboard()
938941
{
939-
tryOpenClipboard( ClipboardMaxTry );
942+
tryOpenClipboard< ClipboardMaxTry >();
940943
}
941944

942945
void MainWindow::openUrl()

0 commit comments

Comments
 (0)