Skip to content

Commit

Permalink
Optimize filteredItemsCache_ filling
Browse files Browse the repository at this point in the history
by passing in a guess for the index.
  • Loading branch information
gin-ahirsch committed Jun 15, 2018
1 parent 5ad3b28 commit 534fc4c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 13 deletions.
30 changes: 20 additions & 10 deletions src/data/logfiltereddata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<LineNumber>( line ), Mark } );
insertIntoFilteredItemsCache( index, FilteredItem{ static_cast<LineNumber>( line ), Mark } );
}
else
LOG(logERROR) << "LogFilteredData::addMark\
Expand Down Expand Up @@ -237,15 +237,15 @@ void LogFilteredData::deleteMark( QChar mark )
}

updateMaxLengthMarks( line );
removeFromFilteredItemsCache( FilteredItem{ static_cast<LineNumber>( line ), Mark } );
removeFromFilteredItemsCache( index, FilteredItem{ static_cast<LineNumber>( line ), Mark } );
}

void LogFilteredData::deleteMark( qint64 line )
{
marks_.deleteMark( line );
int index = marks_.deleteMark( line );

updateMaxLengthMarks( line );
removeFromFilteredItemsCache( FilteredItem{ static_cast<LineNumber>( line ), Mark } );
removeFromFilteredItemsCache( index, FilteredItem{ static_cast<LineNumber>( line ), Mark } );
}

void LogFilteredData::updateMaxLengthMarks( qint64 removed_line )
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -445,6 +445,7 @@ void LogFilteredData::doSetMultibyteEncodingOffsets( int, int )
{
}

<<<<<<< HEAD
void LogFilteredData::regenerateFilteredItemsCache() const
{
LOG(logDEBUG) << "regenerateFilteredItemsCache";
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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;
Expand Down
9 changes: 8 additions & 1 deletion src/data/logfiltereddata.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down
4 changes: 3 additions & 1 deletion src/marks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/marks.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 534fc4c

Please sign in to comment.