Skip to content

Commit

Permalink
perf: don't count cardinality twice
Browse files Browse the repository at this point in the history
  • Loading branch information
variar committed Nov 17, 2024
1 parent a677ffd commit a5f8959
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 29 deletions.
36 changes: 18 additions & 18 deletions src/logdata/include/logfiltereddataworker.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,23 @@

#include <qthreadpool.h>


#ifndef Q_MOC_RUN
#include <tbb/task_group.h>
#include <roaring.hh>
#include <roaring64map.hh>
#include <tbb/task_group.h>
#endif

#include "atomicflag.h"
#include "regularexpression.h"
#include "linetypes.h"
#include "regularexpression.h"
#include "synchronization.h"

class LogData;

// Class encapsulating a single matching line
// Contains the line number the line was found in and its content.
class MatchingLine {
public:
public:
MatchingLine( LineNumber line )
: lineNumber_{ line }
{
Expand All @@ -77,7 +76,7 @@ class MatchingLine {
return lineNumber_ < other.lineNumber_;
}

private:
private:
LineNumber lineNumber_;
};

Expand All @@ -95,12 +94,13 @@ struct SearchResults {
// This class is a mutex protected set of search result data.
// It is thread safe.
class SearchData {
public:
public:
// will clear new matches
SearchResults takeCurrentResults() const;

// Atomically add to all the existing search data.
void addAll( LineLength length, const SearchResultArray& matches, LinesCount nbLinesProcessed );
void addAll( LineLength length, const SearchResultArray& matches, LinesCount nbMatches,
LinesCount nbLinesProcessed );
// Get the number of matches
LinesCount getNbMatches() const;
// Get the last matched line number
Expand All @@ -116,7 +116,7 @@ class SearchData {
// Atomically clear the data.
void clear();

private:
private:
mutable SharedMutex dataMutex_;

SearchResultArray matches_;
Expand All @@ -128,7 +128,7 @@ class SearchData {

class SearchOperation : public QObject {
Q_OBJECT
public:
public:
SearchOperation( const LogData& sourceLogData, AtomicFlag& interruptRequested,
const RegularExpressionPattern& regExp, LineNumber startLine,
LineNumber endLine );
Expand All @@ -137,11 +137,11 @@ class SearchOperation : public QObject {
// and false if it has been cancelled (results not copied)
virtual void run( SearchData& result ) = 0;

Q_SIGNALS:
Q_SIGNALS:
void searchProgressed( LinesCount nbMatches, int percent, LineNumber initialLine );
void searchFinished();

protected:
protected:
// Implement the common part of the search, passing
// the shared results and the line to begin the search from.
void doSearch( SearchData& result, LineNumber initialLine );
Expand All @@ -155,7 +155,7 @@ class SearchOperation : public QObject {

class FullSearchOperation : public SearchOperation {
Q_OBJECT
public:
public:
FullSearchOperation( const LogData& sourceLogData, AtomicFlag& interruptRequested,
const RegularExpressionPattern& regExp, LineNumber startLine,
LineNumber endLine )
Expand All @@ -168,7 +168,7 @@ class FullSearchOperation : public SearchOperation {

class UpdateSearchOperation : public SearchOperation {
Q_OBJECT
public:
public:
UpdateSearchOperation( const LogData& sourceLogData, AtomicFlag& interruptRequested,
const RegularExpressionPattern& regExp, LineNumber startLine,
LineNumber endLine, LineNumber position )
Expand All @@ -179,14 +179,14 @@ class UpdateSearchOperation : public SearchOperation {

void run( SearchData& result ) override;

private:
private:
LineNumber initialPosition_;
};

class LogFilteredDataWorker : public QObject {
Q_OBJECT

public:
public:
explicit LogFilteredDataWorker( const LogData& sourceLogData );
~LogFilteredDataWorker() noexcept override;

Expand All @@ -209,18 +209,18 @@ class LogFilteredDataWorker : public QObject {
// get the current indexing data
SearchResults getSearchResults() const;

Q_SIGNALS:
Q_SIGNALS:
// Sent during the indexing process to signal progress
// percent being the percentage of completion.
void searchProgressed( LinesCount nbMatches, int percent, LineNumber initialLine );
// Sent when indexing is finished, signals the client
// to copy the new data back.
void searchFinished();

private:
private:
void connectSignalsAndRun( SearchOperation* operationRequested );

private:
private:
const LogData& sourceLogData_;
AtomicFlag interruptRequested_;

Expand Down
28 changes: 17 additions & 11 deletions src/logdata/src/logfiltereddataworker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
#include "configuration.h"
#include "dispatch_to.h"
#include "issuereporter.h"
#include "linetypes.h"
#include "log.h"
#include "progress.h"
#include "runnable_lambda.h"
Expand Down Expand Up @@ -129,13 +130,14 @@ SearchResults SearchData::takeCurrentResults() const
return SearchResults{ std::exchange( newMatches_, {} ), maxLength_, nbLinesProcessed_ };
}

void SearchData::addAll( LineLength length, const SearchResultArray& matches, LinesCount lines )
void SearchData::addAll( LineLength length, const SearchResultArray& matches,
LinesCount matchedLines, LinesCount processedLines )
{
UniqueLock lock( dataMutex_ );

maxLength_ = qMax( maxLength_, length );
nbLinesProcessed_ = qMax( nbLinesProcessed_, lines );
nbMatches_ += LinesCount( matches.cardinality() );
nbLinesProcessed_ = qMax( nbLinesProcessed_, processedLines );
nbMatches_ += matchedLines;

newMatches_ |= matches;
}
Expand Down Expand Up @@ -376,7 +378,9 @@ void SearchOperation::doSearch( SearchData& searchData, LineNumber initialLine )
if ( matchResults.processedLines.get() ) {

maxLength = qMax( maxLength, matchResults.maxLength );
nbMatches += LinesCount( matchResults.matchingLines.cardinality() );
const LinesCount matchesCount
= LinesCount( matchResults.matchingLines.cardinality() );
nbMatches += matchesCount;

const auto processedLines = LinesCount{ matchResults.chunkStart.get()
+ matchResults.processedLines.get() };
Expand All @@ -385,12 +389,18 @@ void SearchOperation::doSearch( SearchData& searchData, LineNumber initialLine )

// After each block, copy the data to shared data
// and update the client
searchData.addAll( maxLength, matchResults.matchingLines, processedLines );
searchData.addAll( maxLength, matchResults.matchingLines, matchesCount,
processedLines );

LOG_DEBUG << "done Searching chunk starting at " << matchResults.chunkStart
<< ", " << matchResults.processedLines << " lines read.";
}

delete blockData;

const auto matchProcessorEndTime = high_resolution_clock::now();
matchCombiningDuration += duration_cast<microseconds>( matchProcessorEndTime
- matchProcessorStartTime );
const int percentage
= calculateProgress( totalProcessedLines.get(), totalLines.get() );

Expand All @@ -402,10 +412,6 @@ void SearchOperation::doSearch( SearchData& searchData, LineNumber initialLine )
reportedMatches = nbMatches;
}

const auto matchProcessorEndTime = high_resolution_clock::now();
matchCombiningDuration += duration_cast<microseconds>( matchProcessorEndTime
- matchProcessorStartTime );
delete blockData;
return tbb::flow::continue_msg{};
} );

Expand All @@ -431,8 +437,8 @@ void SearchOperation::doSearch( SearchData& searchData, LineNumber initialLine )
/*LOG_DEBUG << "Sending chunk starting at " << chunkStart << ", " <<
lines.second.size()
<< " lines read.";*/
BlockDataType blockData = new SearchBlockData{chunkStart, std::move(lines)};
BlockDataType blockData = new SearchBlockData{ chunkStart, std::move( lines ) };

const auto lineSourceEndTime = high_resolution_clock::now();
const auto chunkReadTime
= duration_cast<microseconds>( lineSourceEndTime - lineSourceStartTime );
Expand Down

0 comments on commit a5f8959

Please sign in to comment.