QInputDeviceManager: replace a QMap<enum,int> with a std::array<int,#enum-values>

QInputDeviceManager::DeviceType is a (non-flag) enum with all of five
values. Using a self-rebalancing rb-tree to map these to an int is
overkill. An array can do the job just as well and better, given the
enum values are consecutive, and even if some don't make sense (like
Unknown), the waste caused by unused array slots is more than
compensated by the lack of any memory allocations. Even more so as the
array API is 1:1 identical with the map's for the use-case at hand.

Saves 1.2KiB in text size on optimized Linux AMD64 GCC 9.1 builds.

Change-Id: I1bfa115ac75e2f7d9a4bd9a0e3f3241bf68c9989
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
This commit is contained in:
Marc Mutz 2019-06-05 21:59:46 +02:00
parent 3619e87ba1
commit cbc9d89478
3 changed files with 8 additions and 5 deletions

View File

@ -75,13 +75,13 @@ int QInputDeviceManager::deviceCount(DeviceType type) const
int QInputDeviceManagerPrivate::deviceCount(QInputDeviceManager::DeviceType type) const
{
return m_deviceCount.value(type);
return m_deviceCount[type];
}
void QInputDeviceManagerPrivate::setDeviceCount(QInputDeviceManager::DeviceType type, int count)
{
Q_Q(QInputDeviceManager);
if (m_deviceCount.value(type) != count) {
if (m_deviceCount[type] != count) {
m_deviceCount[type] = count;
emit q->deviceListChanged(type);
}

View File

@ -69,7 +69,9 @@ public:
DeviceTypePointer,
DeviceTypeKeyboard,
DeviceTypeTouch,
DeviceTypeTablet
DeviceTypeTablet,
NumDeviceTypes
};
QInputDeviceManager(QObject *parent = nullptr);

View File

@ -52,10 +52,11 @@
//
#include <QtGui/private/qtguiglobal_p.h>
#include <QtCore/qmap.h>
#include <private/qobject_p.h>
#include "qinputdevicemanager_p.h"
#include <array>
QT_BEGIN_NAMESPACE
class Q_GUI_EXPORT QInputDeviceManagerPrivate : public QObjectPrivate
@ -68,7 +69,7 @@ public:
int deviceCount(QInputDeviceManager::DeviceType type) const;
void setDeviceCount(QInputDeviceManager::DeviceType type, int count);
QMap<QInputDeviceManager::DeviceType, int> m_deviceCount;
std::array<int, QInputDeviceManager::NumDeviceTypes> m_deviceCount;
Qt::KeyboardModifiers keyboardModifiers;
};