From be55fbede9ed7a283d769fd5cf15a3a56bdcfcf7 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Fri, 3 Nov 2023 11:51:20 +0100 Subject: [PATCH] QTZ/Darwin: Make available-ID checks cheaper MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously we've checked whether an ID is available by computing the full list of all known available IDs (converting each from NSString to QBA in the process) in order to compare the offered ID to each of those in turn. This is a significant performance bottle-neck Simply trying to construct the NSTimeZone instance should suffice to determine whether there is such a zone; if the ID is not available, we apparently get back a null pointer, which is easy enough to test for. Pick-to: 6.6 6.5 Fixes: QTBUG-118759 Change-Id: Ib272c96e25db9640490299c638e2e9196a58712f Reviewed-by: Tor Arne Vestbø --- src/corelib/time/qtimezoneprivate_mac.mm | 16 ++++++++++------ src/corelib/time/qtimezoneprivate_p.h | 2 +- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/corelib/time/qtimezoneprivate_mac.mm b/src/corelib/time/qtimezoneprivate_mac.mm index b7cfd2bb85..8bc48f893b 100644 --- a/src/corelib/time/qtimezoneprivate_mac.mm +++ b/src/corelib/time/qtimezoneprivate_mac.mm @@ -57,12 +57,10 @@ QMacTimeZonePrivate *QMacTimeZonePrivate::clone() const void QMacTimeZonePrivate::init(const QByteArray &ianaId) { - if (availableTimeZoneIds().contains(ianaId)) { - m_nstz = [[NSTimeZone timeZoneWithName:QString::fromUtf8(ianaId).toNSString()] retain]; - if (m_nstz) - m_id = ianaId; - } - if (!m_nstz) { + m_nstz = [[NSTimeZone timeZoneWithName:QString::fromUtf8(ianaId).toNSString()] retain]; + if (m_nstz) { + m_id = ianaId; + } else { // macOS has been seen returning a systemTimeZone which reports its name // as Asia/Kolkata, which doesn't appear in knownTimeZoneNames (which // calls the zone Asia/Calcutta). So explicitly check for the name @@ -286,6 +284,12 @@ QByteArray QMacTimeZonePrivate::systemTimeZoneId() const return QString::fromNSString(NSTimeZone.systemTimeZone.name).toUtf8(); } +bool QMacTimeZonePrivate::isTimeZoneIdAvailable(const QByteArray& ianaId) const +{ + QMacAutoReleasePool pool; + return [NSTimeZone timeZoneWithName:QString::fromUtf8(ianaId).toNSString()] != nil; +} + QList QMacTimeZonePrivate::availableTimeZoneIds() const { NSEnumerator *enumerator = NSTimeZone.knownTimeZoneNames.objectEnumerator; diff --git a/src/corelib/time/qtimezoneprivate_p.h b/src/corelib/time/qtimezoneprivate_p.h index 8085247953..4942ecd477 100644 --- a/src/corelib/time/qtimezoneprivate_p.h +++ b/src/corelib/time/qtimezoneprivate_p.h @@ -366,7 +366,7 @@ public: Data previousTransition(qint64 beforeMSecsSinceEpoch) const override; QByteArray systemTimeZoneId() const override; - + bool isTimeZoneIdAvailable(const QByteArray &ianaId) const override; QList availableTimeZoneIds() const override; NSTimeZone *nsTimeZone() const;