Skip to content

Commit

Permalink
Remove open/close on each file access.
Browse files Browse the repository at this point in the history
Performance is greatly improved:
sequentialRead 76.7s -> 9.8s
randomRead     15.7s -> 12.8s
browsingSearch 121s  -> 29.8s
  • Loading branch information
nickbnf committed Jul 11, 2015
1 parent f62e2c2 commit 90684c5
Showing 1 changed file with 5 additions and 30 deletions.
35 changes: 5 additions & 30 deletions src/data/logdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,9 @@ void LogData::attachFile( const QString& fileName )
throw CantReattachErr();
}

workerThread_.interrupt();
attached_file_.reset( new QFile( fileName ) );
attached_file_->open( QIODevice::ReadOnly );

// If an attach operation is already in progress, the new one will
// be delayed until the current one is finished (canceled)
std::shared_ptr<const LogDataOperation> operation( new AttachOperation( fileName ) );
enqueueOperation( std::move( operation ) );
}
Expand Down Expand Up @@ -204,8 +203,6 @@ void LogData::fileChangedOnDisk()
{
LOG(logDEBUG) << "signalFileChanged";

// fileWatcher_->removeFile( file_->fileName() );

const QString name = attached_file_->fileName();
QFileInfo info( name );

Expand Down Expand Up @@ -250,23 +247,9 @@ void LogData::indexingFinished( LoadingStatus status )
", found " << nbLines_ << " lines.";

if ( status == LoadingStatus::Successful ) {
// Use the new filename if needed
if ( !currentOperation_->getFilename().isNull() ) {
QString newFileName = currentOperation_->getFilename();

if ( attached_file_ ) {
QMutexLocker locker( &fileMutex_ );
attached_file_->setFileName( newFileName );
}
else {
QMutexLocker locker( &fileMutex_ );
attached_file_.reset( new QFile( newFileName ) );

// And we watch the file for updates
fileChangedOnDisk_ = Unchanged;
fileWatcher_->addFile( attached_file_->fileName() );
}
}
// Start watching we watch the file for updates
fileChangedOnDisk_ = Unchanged;
fileWatcher_->addFile( attached_file_->fileName() );

// Update the modified date/time if the file exists
lastModifiedDate_ = QDateTime();
Expand Down Expand Up @@ -323,13 +306,11 @@ QString LogData::doGetLineString( qint64 line ) const

dataMutex_.lock();
fileMutex_.lock();
attached_file_->open( QIODevice::ReadOnly );

attached_file_->seek( (line == 0) ? 0 : linePosition_[line-1] );

QString string = QString( attached_file_->readLine() );

attached_file_->close();
fileMutex_.unlock();
dataMutex_.unlock();

Expand All @@ -344,13 +325,11 @@ QString LogData::doGetExpandedLineString( qint64 line ) const

dataMutex_.lock();
fileMutex_.lock();
attached_file_->open( QIODevice::ReadOnly );

attached_file_->seek( (line == 0) ? 0 : linePosition_[line-1] );

QByteArray rawString = attached_file_->readLine();

attached_file_->close();
fileMutex_.unlock();
dataMutex_.unlock();

Expand Down Expand Up @@ -382,15 +361,13 @@ QStringList LogData::doGetLines( qint64 first_line, int number ) const
dataMutex_.lock();

fileMutex_.lock();
attached_file_->open( QIODevice::ReadOnly );

const qint64 first_byte = (first_line == 0) ? 0 : linePosition_[first_line-1];
const qint64 last_byte = linePosition_[last_line];
// LOG(logDEBUG) << "LogData::doGetLines first_byte:" << first_byte << " last_byte:" << last_byte;
attached_file_->seek( first_byte );
QByteArray blob = attached_file_->read( last_byte - first_byte );

attached_file_->close();
fileMutex_.unlock();

qint64 beginning = 0;
Expand Down Expand Up @@ -426,15 +403,13 @@ QStringList LogData::doGetExpandedLines( qint64 first_line, int number ) const
dataMutex_.lock();

fileMutex_.lock();
attached_file_->open( QIODevice::ReadOnly );

const qint64 first_byte = (first_line == 0) ? 0 : linePosition_[first_line-1];
const qint64 last_byte = linePosition_[last_line];
// LOG(logDEBUG) << "LogData::doGetExpandedLines first_byte:" << first_byte << " last_byte:" << last_byte;
attached_file_->seek( first_byte );
QByteArray blob = attached_file_->read( last_byte - first_byte );

attached_file_->close();
fileMutex_.unlock();

qint64 beginning = 0;
Expand Down

0 comments on commit 90684c5

Please sign in to comment.