Optimize some atomic counters

Define the static QAtomic at file scope to avoid GCC's pessimisation with
function-static QAtomic (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79561),
and make sure the initial value is 0, so it ends up in BSS, not TEXT.

In QRhi..., don't create a static instance of the wrapper class, use a file-
static atomic, too. This turns the class into a glorified namespace.

Change-Id: I707f628e2b434330028077223071716d5704ba32
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Marc Mutz 2019-06-25 22:21:44 +02:00
parent 6fa34930c2
commit 8da3eea4fb
5 changed files with 11 additions and 12 deletions

View File

@ -5192,10 +5192,11 @@ int QRhi::ubufAlignment() const
return d->ubufAlignment(); return d->ubufAlignment();
} }
static QBasicAtomicInteger<QRhiGlobalObjectIdGenerator::Type> counter = Q_BASIC_ATOMIC_INITIALIZER(0);
QRhiGlobalObjectIdGenerator::Type QRhiGlobalObjectIdGenerator::newId() QRhiGlobalObjectIdGenerator::Type QRhiGlobalObjectIdGenerator::newId()
{ {
static QRhiGlobalObjectIdGenerator inst; return counter.fetchAndAddRelaxed(1) + 1;
return ++inst.counter;
} }
bool QRhiPassResourceTracker::isEmpty() const bool QRhiPassResourceTracker::isEmpty() const

View File

@ -52,7 +52,6 @@
#include "qrhiprofiler_p_p.h" #include "qrhiprofiler_p_p.h"
#include <QBitArray> #include <QBitArray>
#include <QAtomicInt> #include <QAtomicInt>
#include <QAtomicInteger>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
@ -484,9 +483,6 @@ public:
using Type = quint32; using Type = quint32;
#endif #endif
static Type newId(); static Type newId();
private:
QAtomicInteger<Type> counter;
}; };
class QRhiPassResourceTracker class QRhiPassResourceTracker

View File

@ -2799,12 +2799,12 @@ void QFontCache::cleanup()
cache->setLocalData(0); cache->setLocalData(0);
} }
QBasicAtomicInt font_cache_id = Q_BASIC_ATOMIC_INITIALIZER(1); static QBasicAtomicInt font_cache_id = Q_BASIC_ATOMIC_INITIALIZER(0);
QFontCache::QFontCache() QFontCache::QFontCache()
: QObject(), total_cost(0), max_cost(min_cost), : QObject(), total_cost(0), max_cost(min_cost),
current_timestamp(0), fast(false), timer_id(-1), current_timestamp(0), fast(false), timer_id(-1),
m_id(font_cache_id.fetchAndAddRelaxed(1)) m_id(font_cache_id.fetchAndAddRelaxed(1) + 1)
{ {
} }

View File

@ -45,11 +45,12 @@
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
static QBasicAtomicInt winIdGenerator = Q_BASIC_ATOMIC_INITIALIZER(0);
QFbWindow::QFbWindow(QWindow *window) QFbWindow::QFbWindow(QWindow *window)
: QPlatformWindow(window), mBackingStore(0), mWindowState(Qt::WindowNoState) : QPlatformWindow(window), mBackingStore(0), mWindowState(Qt::WindowNoState)
{ {
static QAtomicInt winIdGenerator(1); mWindowId = winIdGenerator.fetchAndAddRelaxed(1) + 1;
mWindowId = winIdGenerator.fetchAndAddRelaxed(1);
} }
QFbWindow::~QFbWindow() QFbWindow::~QFbWindow()

View File

@ -49,13 +49,14 @@
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
static QBasicAtomicInt winIdGenerator = Q_BASIC_ATOMIC_INITIALIZER(0);
QAndroidPlatformWindow::QAndroidPlatformWindow(QWindow *window) QAndroidPlatformWindow::QAndroidPlatformWindow(QWindow *window)
: QPlatformWindow(window) : QPlatformWindow(window)
{ {
m_windowFlags = Qt::Widget; m_windowFlags = Qt::Widget;
m_windowState = Qt::WindowNoState; m_windowState = Qt::WindowNoState;
static QAtomicInt winIdGenerator(1); m_windowId = winIdGenerator.fetchAndAddRelaxed(1) + 1;
m_windowId = winIdGenerator.fetchAndAddRelaxed(1);
setWindowState(window->windowStates()); setWindowState(window->windowStates());
} }