Skip to content

Commit

Permalink
Merge pull request mixxxdj#13581 from Swiftb0y/refactor/more-controld…
Browse files Browse the repository at this point in the history
…oubleprivate-optimization

more `ControlDoublePrivate` optimization
  • Loading branch information
Holzhaus authored Aug 22, 2024
2 parents 17edb4e + ac1744d commit 3ebcdf0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 54 deletions.
66 changes: 29 additions & 37 deletions src/control/control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "control/controlobject.h"
#include "moc_control.cpp"
#include "util/mutex.h"
#include "util/stat.h"

namespace {
Expand All @@ -13,6 +14,17 @@ namespace {
/// configuration object would be arduous.
UserSettingsPointer s_pUserConfig;

const QString statTrackingKey = QStringLiteral("control %1,%2"); // CO group,key

constexpr Stat::StatType kStatType = Stat::UNSPECIFIED;

constexpr Stat::ComputeFlags kComputeFlags = {Stat::COUNT,
Stat::SUM,
Stat::AVERAGE,
Stat::SAMPLE_VARIANCE,
Stat::MIN,
Stat::MAX};

/// Mutex guarding access to s_qCOHash and s_qCOAliasHash.
MMutex s_qCOHashMutex;

Expand All @@ -29,60 +41,41 @@ QHash<ConfigKey, ConfigKey> s_qCOAliasHash
QWeakPointer<ControlDoublePrivate> s_pDefaultCO;
} // namespace

// TODO: re-evaluate whether this is needed.
ControlDoublePrivate::ControlDoublePrivate()
: m_trackType(Stat::UNSPECIFIED),
m_trackFlags(Stat::COUNT | Stat::SUM | Stat::AVERAGE |
Stat::SAMPLE_VARIANCE | Stat::MIN | Stat::MAX),
m_bTrack(false),
// default CO is read only
m_confirmRequired(true),
m_bPersistInConfiguration(false),
m_bIgnoreNops(true),
m_kbdRepeatable(false) {
m_value.setValue(0.0);
}
: ControlDoublePrivate({}, nullptr, true, false, false, kDefaultValue, true){};

ControlDoublePrivate::ControlDoublePrivate(
const ConfigKey& key,
ControlObject* pCreatorCO,
bool bIgnoreNops,
bool bTrack,
bool bPersist,
double defaultValue)
double defaultValue,
bool confirmRequired = false)
: m_key(key),
m_pBehavior(nullptr),
m_name(QString()),
m_description(QString()),
m_value(defaultValue),
m_defaultValue(defaultValue),
m_pCreatorCO(pCreatorCO),
m_trackType(Stat::UNSPECIFIED),
m_trackFlags(Stat::COUNT | Stat::SUM | Stat::AVERAGE |
Stat::SAMPLE_VARIANCE | Stat::MIN | Stat::MAX),
m_bTrack(bTrack),
m_confirmRequired(false),
m_trackingKey(bTrack ? statTrackingKey.arg(key.group, key.item) : QString()),
m_confirmRequired(confirmRequired),
m_bPersistInConfiguration(bPersist),
m_bIgnoreNops(bIgnoreNops),
m_kbdRepeatable(false) {
initialize(defaultValue);
}

void ControlDoublePrivate::initialize(double defaultValue) {
double value = defaultValue;
if (m_bPersistInConfiguration) {
if (bPersist) {
UserSettingsPointer pConfig = s_pUserConfig;
if (pConfig) {
value = pConfig->getValue(m_key, defaultValue);
m_value.setValue(pConfig->getValue(m_key, defaultValue));
} else {
DEBUG_ASSERT(!"Can't load persistent value s_pUserConfig is null");
}
}
m_defaultValue.setValue(defaultValue);
m_value.setValue(value);

//qDebug() << "Creating:" << m_trackKey << "at" << &m_value << sizeof(m_value);

if (m_bTrack) {
// TODO(rryan): Make configurable.
m_trackKey = "control " + m_key.group + "," + m_key.item;
Stat::track(m_trackKey, static_cast<Stat::StatType>(m_trackType),
static_cast<Stat::ComputeFlags>(m_trackFlags),
m_value.getValue());
if (!m_trackingKey.isNull()) {
Stat::track(m_trackingKey, kStatType, kComputeFlags, m_value.getValue());
}
}

Expand Down Expand Up @@ -297,9 +290,8 @@ void ControlDoublePrivate::setInner(double value, QObject* pSender) {
m_value.setValue(value);
emit valueChanged(value, pSender);

if (m_bTrack) {
Stat::track(m_trackKey, static_cast<Stat::StatType>(m_trackType),
static_cast<Stat::ComputeFlags>(m_trackFlags), value);
if (!m_trackingKey.isNull()) {
Stat::track(m_trackingKey, kStatType, kComputeFlags, value);
}
}

Expand Down
29 changes: 12 additions & 17 deletions src/control/control.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "control/controlbehavior.h"
#include "control/controlvalue.h"
#include "preferences/usersettings.h"
#include "util/mutex.h"

class ControlObject;

Expand All @@ -34,6 +33,9 @@ class ControlDoublePrivate : public QObject {
public:
~ControlDoublePrivate() override;

// TODO: don't expose this implementation detail
constexpr static double kDefaultValue = 0.0;

// Used to implement control persistence. All controls that are marked
// "persist in user config" get and set their value on creation/deletion
// using this UserSettings.
Expand All @@ -54,7 +56,7 @@ class ControlDoublePrivate : public QObject {
bool bIgnoreNops = true,
bool bTrack = false,
bool bPersist = false,
double defaultValue = 0.0);
double defaultValue = kDefaultValue);
static QSharedPointer<ControlDoublePrivate> getDefaultControl();

// Returns a list of all existing instances.
Expand Down Expand Up @@ -172,7 +174,8 @@ class ControlDoublePrivate : public QObject {
bool bIgnoreNops,
bool bTrack,
bool bPersist,
double defaultValue);
double defaultValue,
bool confirmRequired);
ControlDoublePrivate(ControlDoublePrivate&&) = delete;
ControlDoublePrivate(const ControlDoublePrivate&) = delete;
ControlDoublePrivate& operator=(ControlDoublePrivate&&) = delete;
Expand All @@ -198,14 +201,12 @@ class ControlDoublePrivate : public QObject {

QAtomicPointer<ControlObject> m_pCreatorCO;

QString m_trackKey;
// name of the key to track using stats framework, unless the m_trackingKey isNull().
QString m_trackingKey;

// Note: keep the order of the members below to not introduce gaps due to
// memory alignment in this often used class. Whether to track value changes
// with the stats framework.
int m_trackType;
int m_trackFlags;
bool m_bTrack;
// memory alignment in this often used class.

bool m_confirmRequired;

// Whether the control should persist in the Mixxx user configuration. The
Expand All @@ -229,14 +230,8 @@ class ControlDoublePrivateConst : public ControlDoublePrivate {
public:
~ControlDoublePrivateConst() override = default;

void setInner(double value, QObject* pSender) override {
Q_UNUSED(value)
Q_UNUSED(pSender)
private:
void setInner(double, QObject*) override {
DEBUG_ASSERT(!"Trying to modify a default constructed (const) control object");
};

protected:
ControlDoublePrivateConst() = default;

friend ControlDoublePrivate;
};

0 comments on commit 3ebcdf0

Please sign in to comment.