Skip to content

Commit

Permalink
Optimize highlighting FTS matches in articles
Browse files Browse the repository at this point in the history
The wall time of calls to ArticleView::highlightAllFtsOccurences() on my
GNU/Linux system before and at this commit:
allMatches.size()   uniqueMatches.size()    before(ms)  at(ms)
79                  1                       277         4
98                  1                       380         4
267                 1                       16803       65
  • Loading branch information
vedgy committed May 23, 2022
1 parent bd5b36c commit b87b023
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions articleview.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2664,8 +2664,22 @@ void ArticleView::highlightFTSResults()
void ArticleView::highlightAllFtsOccurences( QWebPage::FindFlags flags )
{
flags |= QWebPage::HighlightAllOccurrences;
for( int x = 0; x < allMatches.size(); x++ )
ui.definition->findText( allMatches.at( x ), flags );

// Usually allMatches contains mostly duplicates. Thus searching for each element of
// allMatches to highlight them takes a long time => collect unique elements into a
// set and search for them instead.
// Don't use QList::toSet() or QSet's range constructor because they reserve space
// for QList::size() elements, whereas the final QSet size is likely 1 or 2.
QSet< QString > uniqueMatches;
for( int x = 0; x < allMatches.size(); ++x )
{
QString const & match = allMatches.at( x );
// Consider words that differ only in case equal if the search is case-insensitive.
uniqueMatches.insert( ftsSearchMatchCase ? match : match.toLower() );
}

for( QSet< QString >::const_iterator it = uniqueMatches.constBegin(); it != uniqueMatches.constEnd(); ++it )
ui.definition->findText( *it, flags );
}

void ArticleView::performFtsFindOperation( bool backwards )
Expand Down

0 comments on commit b87b023

Please sign in to comment.