From e262b86c44917b1d80a91eb2cbc8d87829f92a17 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Sun, 27 Oct 2019 21:37:23 +0800 Subject: [PATCH] Reserve memory for file data buffers --- src/base/bittorrent/session.cpp | 1 + src/base/bittorrent/torrenthandle.cpp | 10 +++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/base/bittorrent/session.cpp b/src/base/bittorrent/session.cpp index 5b1c1f18d..14445914c 100644 --- a/src/base/bittorrent/session.cpp +++ b/src/base/bittorrent/session.cpp @@ -3507,6 +3507,7 @@ void Session::handleTorrentResumeDataReady(TorrentHandle *const torrent, const l // isn't cheap too. QByteArray out; + out.reserve(1024 * 1024); // most fastresume file sizes are under 1 MB lt::bencode(std::back_inserter(out), data); const QString filename = QString("%1.fastresume").arg(torrent->hash()); diff --git a/src/base/bittorrent/torrenthandle.cpp b/src/base/bittorrent/torrenthandle.cpp index 497c31322..6d60072d3 100644 --- a/src/base/bittorrent/torrenthandle.cpp +++ b/src/base/bittorrent/torrenthandle.cpp @@ -1581,11 +1581,15 @@ bool TorrentHandle::saveTorrentFile(const QString &path) #endif const lt::entry torrentEntry = torrentCreator.generate(); - QVector out; + QByteArray out; + out.reserve(1024 * 1024); // most torrent file sizes are under 1 MB lt::bencode(std::back_inserter(out), torrentEntry); + if (out.isEmpty()) + return false; + QFile torrentFile(path); - if (!out.empty() && torrentFile.open(QIODevice::WriteOnly)) - return (torrentFile.write(&out[0], out.size()) == out.size()); + if (torrentFile.open(QIODevice::WriteOnly)) + return (torrentFile.write(out) == out.size()); return false; }