From: Olivier Goffart Date: Thu, 16 Jun 2016 06:28:30 +0000 (+0200) Subject: Checksum speedup. (#4968) X-Git-Tag: archive/raspbian/3.16.7-1_deb13u1+rpi1~1^2~1150^2~16 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=b4900d60b76826d17de803ee0c727ac73f4492ba;p=nextcloud-desktop.git Checksum speedup. (#4968) No need to allocate (and initialize to 0) a 10 MiB buffer for each files, even when most files are much smaller than that. So make sure the buffer that we allocate is not bigger than the file size. And Also 10 MiB is a bit big for a buffer. 500 KiB should be more than enough. (Too big allocations can cause problem because of memory fragmentation and such) --- diff --git a/src/libsync/filesystem.cpp b/src/libsync/filesystem.cpp index f5f5e7da4..2dd6b51dd 100644 --- a/src/libsync/filesystem.cpp +++ b/src/libsync/filesystem.cpp @@ -496,16 +496,16 @@ QString FileSystem::fileSystemForPath(const QString & path) } #endif -#define BUFSIZE 1024*1024*10 +#define BUFSIZE qint64(500*1024) // 500 KiB static QByteArray readToCrypto( const QString& filename, QCryptographicHash::Algorithm algo ) { - const qint64 bufSize = BUFSIZE; - QByteArray buf(bufSize,0); + QFile file(filename); + const qint64 bufSize = qMin(BUFSIZE, file.size() + 1); + QByteArray buf(bufSize, Qt::Uninitialized); QByteArray arr; QCryptographicHash crypto( algo ); - QFile file(filename); if (file.open(QIODevice::ReadOnly)) { qint64 size; while (!file.atEnd()) { @@ -532,11 +532,11 @@ QByteArray FileSystem::calcSha1( const QString& filename ) #ifdef ZLIB_FOUND QByteArray FileSystem::calcAdler32( const QString& filename ) { - unsigned int adler = adler32(0L, Z_NULL, 0); - const qint64 bufSize = BUFSIZE; - QByteArray buf(bufSize, 0); - QFile file(filename); + const qint64 bufSize = qMin(BUFSIZE, file.size() + 1); + QByteArray buf(bufSize, Qt::Uninitialized); + + unsigned int adler = adler32(0L, Z_NULL, 0); if (file.open(QIODevice::ReadOnly)) { qint64 size; while (!file.atEnd()) {