Browse Source

Use std::vector instead of std::deque in QAlertDispatcher

As we never use {push,pop}_front std::vector works here perfectly.
Also reserve memory for std::vector out of lock.

This could be considered as an optimization, but in reality this is just
using right container in right place. According to my measurements total
speedup is under 0.2%.
adaptive-webui-19844
Ivan Sorokin 10 years ago
parent
commit
333978f1ff
  1. 9
      src/qtlibtorrent/alertdispatcher.cpp
  2. 6
      src/qtlibtorrent/alertdispatcher.h
  3. 6
      src/qtlibtorrent/qbtsession.cpp

9
src/qtlibtorrent/alertdispatcher.cpp

@ -33,12 +33,15 @@
#include <boost/bind.hpp> #include <boost/bind.hpp>
#include <QMutexLocker> #include <QMutexLocker>
const size_t DEFAULT_ALERTS_CAPACITY = 32;
QAlertDispatcher::QAlertDispatcher(libtorrent::session *session, QObject* parent) QAlertDispatcher::QAlertDispatcher(libtorrent::session *session, QObject* parent)
: QObject(parent) : QObject(parent)
, m_session(session) , m_session(session)
, current_tag(new QAtomicPointer<QAlertDispatcher>(this)) , current_tag(new QAtomicPointer<QAlertDispatcher>(this))
, event_posted(false) , event_posted(false)
{ {
alerts.reserve(DEFAULT_ALERTS_CAPACITY);
m_session->set_alert_dispatch(boost::bind(&QAlertDispatcher::dispatch, current_tag, _1)); m_session->set_alert_dispatch(boost::bind(&QAlertDispatcher::dispatch, current_tag, _1));
} }
@ -60,16 +63,18 @@ QAlertDispatcher::~QAlertDispatcher() {
m_session->set_alert_dispatch(dispatch_function_t()); m_session->set_alert_dispatch(dispatch_function_t());
} }
void QAlertDispatcher::getPendingAlertsNoWait(std::deque<libtorrent::alert*>& out) { void QAlertDispatcher::getPendingAlertsNoWait(std::vector<libtorrent::alert*>& out) {
Q_ASSERT(out.empty()); Q_ASSERT(out.empty());
out.reserve(DEFAULT_ALERTS_CAPACITY);
QMutexLocker lock(&alerts_mutex); QMutexLocker lock(&alerts_mutex);
alerts.swap(out); alerts.swap(out);
event_posted = false; event_posted = false;
} }
void QAlertDispatcher::getPendingAlerts(std::deque<libtorrent::alert*>& out, unsigned long time) { void QAlertDispatcher::getPendingAlerts(std::vector<libtorrent::alert*>& out, unsigned long time) {
Q_ASSERT(out.empty()); Q_ASSERT(out.empty());
out.reserve(DEFAULT_ALERTS_CAPACITY);
QMutexLocker lock(&alerts_mutex); QMutexLocker lock(&alerts_mutex);

6
src/qtlibtorrent/alertdispatcher.h

@ -46,8 +46,8 @@ public:
QAlertDispatcher(libtorrent::session *session, QObject* parent); QAlertDispatcher(libtorrent::session *session, QObject* parent);
~QAlertDispatcher(); ~QAlertDispatcher();
void getPendingAlertsNoWait(std::deque<libtorrent::alert*>&); void getPendingAlertsNoWait(std::vector<libtorrent::alert*>&);
void getPendingAlerts(std::deque<libtorrent::alert*>&, unsigned long time = ULONG_MAX); void getPendingAlerts(std::vector<libtorrent::alert*>&, unsigned long time = ULONG_MAX);
signals: signals:
void alertsReceived(); void alertsReceived();
@ -64,7 +64,7 @@ private:
libtorrent::session *m_session; libtorrent::session *m_session;
QMutex alerts_mutex; QMutex alerts_mutex;
QWaitCondition alerts_condvar; QWaitCondition alerts_condvar;
std::deque<libtorrent::alert*> alerts; std::vector<libtorrent::alert*> alerts;
QSharedPointer<QAtomicPointer<QAlertDispatcher> > current_tag; QSharedPointer<QAtomicPointer<QAlertDispatcher> > current_tag;
bool event_posted; bool event_posted;
}; };

6
src/qtlibtorrent/qbtsession.cpp

@ -1649,7 +1649,7 @@ void QBtSession::saveFastResumeData() {
} catch(libtorrent::invalid_handle&) {} } catch(libtorrent::invalid_handle&) {}
} }
while (num_resume_data > 0) { while (num_resume_data > 0) {
std::deque<alert*> alerts; std::vector<alert*> alerts;
m_alertDispatcher->getPendingAlerts(alerts, 30*1000); m_alertDispatcher->getPendingAlerts(alerts, 30*1000);
if (alerts.empty()) { if (alerts.empty()) {
std::cerr << " aborting with " << num_resume_data << " outstanding " std::cerr << " aborting with " << num_resume_data << " outstanding "
@ -1657,7 +1657,7 @@ void QBtSession::saveFastResumeData() {
break; break;
} }
for (std::deque<alert*>::const_iterator i = alerts.begin(), end = alerts.end(); i != end; ++i) for (std::vector<alert*>::const_iterator i = alerts.begin(), end = alerts.end(); i != end; ++i)
{ {
alert const* a = *i; alert const* a = *i;
// Saving fastresume data can fail // Saving fastresume data can fail
@ -2147,7 +2147,7 @@ void QBtSession::sendNotificationEmail(const QTorrentHandle &h) {
// Read alerts sent by the Bittorrent session // Read alerts sent by the Bittorrent session
void QBtSession::readAlerts() { void QBtSession::readAlerts() {
typedef std::deque<alert*> alerts_t; typedef std::vector<alert*> alerts_t;
alerts_t alerts; alerts_t alerts;
m_alertDispatcher->getPendingAlertsNoWait(alerts); m_alertDispatcher->getPendingAlertsNoWait(alerts);

Loading…
Cancel
Save