From 534fc4ce3ce32d75fc4f936304a80c82cd9d22e2 Mon Sep 17 00:00:00 2001 From: Alexander Hirsch Date: Fri, 15 Jun 2018 14:24:40 +0200 Subject: [PATCH] Optimize filteredItemsCache_ filling by passing in a guess for the index. --- src/data/logfiltereddata.cpp | 30 ++++++++++++++++++++---------- src/data/logfiltereddata.h | 9 ++++++++- src/marks.cpp | 4 +++- src/marks.h | 4 +++- 4 files changed, 34 insertions(+), 13 deletions(-) diff --git a/src/data/logfiltereddata.cpp b/src/data/logfiltereddata.cpp index c8d73a5c5..251aa5d19 100644 --- a/src/data/logfiltereddata.cpp +++ b/src/data/logfiltereddata.cpp @@ -177,10 +177,10 @@ LogFilteredData::FilteredLineType void LogFilteredData::addMark( qint64 line, QChar mark ) { if ( ( line >= 0 ) && ( line < sourceLogData_->getNbLine() ) ) { - marks_.addMark( line, mark ); + int index = marks_.addMark( line, mark ); maxLengthMarks_ = qMax( maxLengthMarks_, sourceLogData_->getLineLength( line ) ); - insertIntoFilteredItemsCache( FilteredItem{ static_cast( line ), Mark } ); + insertIntoFilteredItemsCache( index, FilteredItem{ static_cast( line ), Mark } ); } else LOG(logERROR) << "LogFilteredData::addMark\ @@ -237,15 +237,15 @@ void LogFilteredData::deleteMark( QChar mark ) } updateMaxLengthMarks( line ); - removeFromFilteredItemsCache( FilteredItem{ static_cast( line ), Mark } ); + removeFromFilteredItemsCache( index, FilteredItem{ static_cast( line ), Mark } ); } void LogFilteredData::deleteMark( qint64 line ) { - marks_.deleteMark( line ); + int index = marks_.deleteMark( line ); updateMaxLengthMarks( line ); - removeFromFilteredItemsCache( FilteredItem{ static_cast( line ), Mark } ); + removeFromFilteredItemsCache( index, FilteredItem{ static_cast( line ), Mark } ); } void LogFilteredData::updateMaxLengthMarks( qint64 removed_line ) @@ -300,7 +300,7 @@ void LogFilteredData::handleSearchProgressed( int nbMatches, int progress, qint6 assert( matching_lines_.size() >= start_index ); for ( auto it = next( begin( matching_lines_ ), start_index ); it != end( matching_lines_ ); ++it ) { - insertIntoFilteredItemsCache( FilteredItem{ it->lineNumber(), Match } ); + start_index = insertIntoFilteredItemsCache( start_index, FilteredItem{ it->lineNumber(), Match } ); } emit searchProgressed( nbMatches, progress, initial_position ); @@ -445,6 +445,7 @@ void LogFilteredData::doSetMultibyteEncodingOffsets( int, int ) { } +<<<<<<< HEAD void LogFilteredData::regenerateFilteredItemsCache() const { LOG(logDEBUG) << "regenerateFilteredItemsCache"; @@ -486,7 +487,7 @@ void LogFilteredData::regenerateFilteredItemsCache() const LOG(logDEBUG) << "finished regenerateFilteredItemsCache"; } -void LogFilteredData::insertIntoFilteredItemsCache( FilteredItem item ) +size_t LogFilteredData::insertIntoFilteredItemsCache( size_t insert_index, FilteredItem item ) { if ( visibility_ != MarksAndMatches ) { // this is invalidated and will be regenerated when we need it @@ -496,16 +497,24 @@ void LogFilteredData::insertIntoFilteredItemsCache( FilteredItem item ) } // Search for the corresponding index. - auto found = lower_bound( begin( filteredItemsCache_ ), end( filteredItemsCache_ ), item ); + // We can start the search from insert_index, since lineNumber >= index is always true. + auto found = lower_bound( next( begin( filteredItemsCache_ ), insert_index ), end( filteredItemsCache_ ), item ); if ( found == end( filteredItemsCache_ ) || found->lineNumber() > item.lineNumber() ) { filteredItemsCache_.insert( found, item ); + return filteredItemsCache_.size(); } else { assert( found->lineNumber() == item.lineNumber() ); found->add( item.type() ); + return distance( begin( filteredItemsCache_ ), found ); } } -void LogFilteredData::removeFromFilteredItemsCache( FilteredItem item ) +size_t LogFilteredData::insertIntoFilteredItemsCache( FilteredItem item ) +{ + return insertIntoFilteredItemsCache( 0, item ); +} + +void LogFilteredData::removeFromFilteredItemsCache( size_t remove_index, FilteredItem item ) { if ( visibility_ != MarksAndMatches ) { // this is invalidated and will be regenerated when we need it @@ -515,7 +524,8 @@ void LogFilteredData::removeFromFilteredItemsCache( FilteredItem item ) } // Search for the corresponding index. - auto found = equal_range( begin( filteredItemsCache_ ), end( filteredItemsCache_ ), item ); + // We can start the search from remove_index, since lineNumber >= index is always true. + auto found = equal_range( next( begin( filteredItemsCache_ ), remove_index ), end( filteredItemsCache_ ), item ); if( found.first == end( filteredItemsCache_ ) ) { LOG(logERROR) << "Attempt to remove line " << item.lineNumber() << " from filteredItemsCache_ failed, since it was not found"; return; diff --git a/src/data/logfiltereddata.h b/src/data/logfiltereddata.h index c4bf9959e..ca3d768ba 100644 --- a/src/data/logfiltereddata.h +++ b/src/data/logfiltereddata.h @@ -165,7 +165,14 @@ class LogFilteredData : public AbstractLogData { LineNumber findFilteredLine( LineNumber lineNum ) const; void regenerateFilteredItemsCache() const; - void insertIntoFilteredItemsCache( FilteredItem item ); + // start_index can be passed in as an optimization when finding the item. + // It refers to the index of the singular arrays (Marks or SearchResultArray) where the item was inserted. + // Returns the index at which the item was inserted. This can be used as the start_index is multiple sorted items are inserted. + size_t insertIntoFilteredItemsCache( size_t start_index, FilteredItem item ); + size_t insertIntoFilteredItemsCache( FilteredItem item ); + // remove_index can be passed in as an optimization when finding the item. + // It refers to the index of the singular arrays (Marks or SearchResultArray) where the item was removed. + void removeFromFilteredItemsCache( size_t remove_index, FilteredItem item ); void removeFromFilteredItemsCache( FilteredItem item ); void removeAllFromFilteredItemsCache( FilteredLineType type ); diff --git a/src/marks.cpp b/src/marks.cpp index 9201331c7..d3f4d2036 100644 --- a/src/marks.cpp +++ b/src/marks.cpp @@ -32,7 +32,7 @@ Marks::Marks() : marks_() { } -void Marks::addMark( qint64 line, QChar mark ) +int Marks::addMark( qint64 line, QChar mark ) { // Look for the index immediately before int index; @@ -50,6 +50,8 @@ void Marks::addMark( qint64 line, QChar mark ) // 'mark' is not used yet mark = mark; + + return index; } qint64 Marks::getMark( QChar mark ) const diff --git a/src/marks.h b/src/marks.h index e55ac6376..3a935ff42 100644 --- a/src/marks.h +++ b/src/marks.h @@ -49,7 +49,9 @@ class Marks { // Add a mark at the given line, optionally identified by the given char // If a mark for this char already exist, the previous one is replaced. // It will happily add marks anywhere, even at stupid indexes. - void addMark( qint64 line, QChar mark = QChar() ); + // Returns the index at which the mark was inserted, + // such that getLineMarkedByIndex( addMark( line ) ) == line. + int addMark( qint64 line, QChar mark = QChar() ); // Get the (unique) mark identified by the passed char. qint64 getMark( QChar mark ) const; // Returns wheither the passed line has a mark on it.