From f13c604fbe124dccddc1087437462e7235c4fc1a Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Sat, 21 May 2016 00:55:03 +0800 Subject: [PATCH] Use boost:circular_buffer instead of QList. QList has to store an additional pointer for each element which leads to bad space efficiency. --- src/base/bittorrent/private/speedmonitor.cpp | 14 +++++++++----- src/base/bittorrent/private/speedmonitor.h | 10 ++++++++-- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/base/bittorrent/private/speedmonitor.cpp b/src/base/bittorrent/private/speedmonitor.cpp index 49ce65f58..096f9b883 100644 --- a/src/base/bittorrent/private/speedmonitor.cpp +++ b/src/base/bittorrent/private/speedmonitor.cpp @@ -27,17 +27,21 @@ * exception statement from your version. */ -#include #include "speedmonitor.h" +SpeedMonitor::SpeedMonitor() + : m_speedSamples(MAX_SAMPLES) +{ +} + void SpeedMonitor::addSample(const SpeedSample &sample) { - m_speedSamples.push_back(sample); - m_sum += sample; - if (m_speedSamples.size() > MAX_SAMPLES) { + if (m_speedSamples.size() >= MAX_SAMPLES) { m_sum -= m_speedSamples.front(); - m_speedSamples.pop_front(); } + + m_speedSamples.push_back(sample); + m_sum += sample; } SpeedSampleAvg SpeedMonitor::average() const diff --git a/src/base/bittorrent/private/speedmonitor.h b/src/base/bittorrent/private/speedmonitor.h index 61cb33baa..4bbc90c4e 100644 --- a/src/base/bittorrent/private/speedmonitor.h +++ b/src/base/bittorrent/private/speedmonitor.h @@ -30,7 +30,11 @@ #ifndef SPEEDMONITOR_H #define SPEEDMONITOR_H -template class QList; +#ifndef Q_MOC_RUN +#include +#endif + +#include template struct Sample @@ -71,13 +75,15 @@ typedef Sample SpeedSampleAvg; class SpeedMonitor { public: + SpeedMonitor(); + void addSample(const SpeedSample &sample); SpeedSampleAvg average() const; void reset(); private: static const int MAX_SAMPLES = 30; - QList m_speedSamples; + boost::circular_buffer m_speedSamples; SpeedSample m_sum; };