From 5f1f0fe0b71c19be041ec85a9449584649c9fc18 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 26 Jun 2019 20:56:40 +0200 Subject: QDateTimePrivate: inherit QSharedData and other cleanups Don't manage the ref-count yourself, as this requires the code to use the QAtomic copy ctor, which we want to remove going forward. Using QSharedData, we can let the compiler write the code for us. Since 'ref' this way moves to the first spot in the list of effective members, creating a 4B hole between itself and 'msecs', swap 'status' and 'msecs' to fill the hole: offset: 0 8 16 24 | | | | without v v v v adj.mnt: |*R*| | msecs | S | U | TZ.... before: | msecs | S | U |*R*| | TZ... after: |*R*| S | msecs | U | | TZ.... This keeps the padding out of the critical first word, which improves latency. That said, for accessing the members the old layout surely was optimal. This layout optimizes copies and pessimizes access to 'msecs' on 32-bit platforms without the Critical Word First optimization. Requires adjustments to tst_toolsupport and the qhooks version. Also default members using NSDMI, consequently drop the manual default ctor. Change-Id: I3c48e68694ad29b28a13aa47ea0f283fae52edd7 Reviewed-by: Edward Welbourne Reviewed-by: Thiago Macieira --- src/corelib/global/qhooks.cpp | 2 +- src/corelib/time/qdatetime_p.h | 18 +++++------------- tests/auto/other/toolsupport/tst_toolsupport.cpp | 6 +++--- 3 files changed, 9 insertions(+), 17 deletions(-) diff --git a/src/corelib/global/qhooks.cpp b/src/corelib/global/qhooks.cpp index 020dee3710..79f9b7d6c2 100644 --- a/src/corelib/global/qhooks.cpp +++ b/src/corelib/global/qhooks.cpp @@ -67,7 +67,7 @@ quintptr Q_CORE_EXPORT qtHookData[] = { // The required sizes and offsets are tested in tests/auto/other/toolsupport. // When this fails and the change was intentional, adjust the test and // adjust this value here. - 17 + 18 }; Q_STATIC_ASSERT(QHooks::LastHookIndex == sizeof(qtHookData) / sizeof(qtHookData[0])); diff --git a/src/corelib/time/qdatetime_p.h b/src/corelib/time/qdatetime_p.h index 6e4120d762..f4f00a8b9b 100644 --- a/src/corelib/time/qdatetime_p.h +++ b/src/corelib/time/qdatetime_p.h @@ -56,7 +56,7 @@ #include "qplatformdefs.h" #include "QtCore/qatomic.h" #include "QtCore/qdatetime.h" -#include "QtCore/qpair.h" +#include "QtCore/qshareddata.h" #if QT_CONFIG(timezone) #include "qtimezone.h" @@ -64,7 +64,7 @@ QT_BEGIN_NAMESPACE -class QDateTimePrivate +class QDateTimePrivate : public QSharedData { public: // forward the declarations from QDateTime (this makes them public) @@ -110,13 +110,6 @@ public: DaylightMask = SetToStandardTime | SetToDaylightTime }; - QDateTimePrivate() : m_msecs(0), - m_status(StatusFlag(Qt::LocalTime << TimeSpecShift)), - m_offsetFromUtc(0), - ref(0) - { - } - static QDateTime::Data create(const QDate &toDate, const QTime &toTime, Qt::TimeSpec toSpec, int offsetSeconds); @@ -124,10 +117,9 @@ public: static QDateTime::Data create(const QDate &toDate, const QTime &toTime, const QTimeZone & timeZone); #endif // timezone - qint64 m_msecs; - StatusFlags m_status; - int m_offsetFromUtc; - mutable QAtomicInt ref; + StatusFlags m_status = StatusFlag(Qt::LocalTime << TimeSpecShift); + qint64 m_msecs = 0; + int m_offsetFromUtc = 0; #if QT_CONFIG(timezone) QTimeZone m_timeZone; #endif // timezone diff --git a/tests/auto/other/toolsupport/tst_toolsupport.cpp b/tests/auto/other/toolsupport/tst_toolsupport.cpp index 8c129adaf3..ab7bca8322 100644 --- a/tests/auto/other/toolsupport/tst_toolsupport.cpp +++ b/tests/auto/other/toolsupport/tst_toolsupport.cpp @@ -135,11 +135,11 @@ void tst_toolsupport::offsets_data() { QTest::newRow("QDateTimePrivate::m_msecs") - << pmm_to_offsetof(&QDateTimePrivate::m_msecs) << 0 << 0; + << pmm_to_offsetof(&QDateTimePrivate::m_msecs) << 8 << 8; QTest::newRow("QDateTimePrivate::m_status") - << pmm_to_offsetof(&QDateTimePrivate::m_status) << 8 << 8; + << pmm_to_offsetof(&QDateTimePrivate::m_status) << 4 << 4; QTest::newRow("QDateTimePrivate::m_offsetFromUtc") - << pmm_to_offsetof(&QDateTimePrivate::m_offsetFromUtc) << 12 << 12; + << pmm_to_offsetof(&QDateTimePrivate::m_offsetFromUtc) << 16 << 16; QTest::newRow("QDateTimePrivate::m_timeZone") << pmm_to_offsetof(&QDateTimePrivate::m_timeZone) << 20 << 24; } -- cgit v1.2.3