From 81a45e1f13fdf56129aed952a6e3479e16c14a2c Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Sat, 26 Jul 2014 01:43:18 +0200 Subject: QTimeZone: don't use QSet, use sorted QList QSet, as a node-based container, requires one memory allocation per element inserted. QList, as a contiguous-memory container (at least in the case of a QByteArray payload), requires one memory allocation per container. The higher lookup speed might still speak for using QSet, but there are only two uses of the sets: 1. Checking for existence (or lack thereof) of timezone names. For this, first generating a container full of data just to check for existence of one item of data is extremely wasteful. The QTZPrivate API should be extended to allow said lookup to be performed on the native data store instead. That leaves 2. Returning a sorted(!) list(!) from the public QTimeZone API. There is no reason why, during the construction of those sorted lists, the data should be held in a set. Instead, the well-known technique of first cramming everything into a result container, which is subsequently sorted and has its duplicates removed, can be used here. Saves more than 8K of text size on AMD64 stripped release builds. Change-Id: I71c2298e94e02d55b0c9fb6f7ebeaed79a1fe2db Reviewed-by: Thiago Macieira --- src/corelib/tools/qtimezone.cpp | 42 +++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) (limited to 'src/corelib/tools/qtimezone.cpp') diff --git a/src/corelib/tools/qtimezone.cpp b/src/corelib/tools/qtimezone.cpp index b6160a3fd8..c2ee99a2d8 100644 --- a/src/corelib/tools/qtimezone.cpp +++ b/src/corelib/tools/qtimezone.cpp @@ -378,12 +378,10 @@ QTimeZone::QTimeZone(int offsetSeconds) QTimeZone::QTimeZone(const QByteArray &ianaId, int offsetSeconds, const QString &name, const QString &abbreviation, QLocale::Country country, const QString &comment) + : d() { - // ianaId must be a valid ID and must not clash with the standard system names - if (QTimeZonePrivate::isValidId(ianaId) && !availableTimeZoneIds().contains(ianaId)) + if (!isTimeZoneIdAvailable(ianaId)) d = new QUtcTimeZonePrivate(ianaId, offsetSeconds, name, abbreviation, country, comment); - else - d = 0; } /*! @@ -821,7 +819,20 @@ bool QTimeZone::isTimeZoneIdAvailable(const QByteArray &ianaId) { // isValidId is not strictly required, but faster to weed out invalid // IDs as availableTimeZoneIds() may be slow - return (QTimeZonePrivate::isValidId(ianaId) && (availableTimeZoneIds().contains(ianaId))); + if (!QTimeZonePrivate::isValidId(ianaId)) + return false; + const QList tzIds = availableTimeZoneIds(); + return std::binary_search(tzIds.begin(), tzIds.end(), ianaId); +} + +static QList set_union(const QList &l1, const QList &l2) +{ + QList result; + result.reserve(l1.size() + l2.size()); + std::set_union(l1.begin(), l1.end(), + l2.begin(), l2.end(), + std::back_inserter(result)); + return result; } /*! @@ -832,11 +843,8 @@ bool QTimeZone::isTimeZoneIdAvailable(const QByteArray &ianaId) QList QTimeZone::availableTimeZoneIds() { - QSet set = QUtcTimeZonePrivate().availableTimeZoneIds() - + global_tz->backend->availableTimeZoneIds(); - QList list = set.toList(); - std::sort(list.begin(), list.end()); - return list; + return set_union(QUtcTimeZonePrivate().availableTimeZoneIds(), + global_tz->backend->availableTimeZoneIds()); } /*! @@ -852,11 +860,8 @@ QList QTimeZone::availableTimeZoneIds() QList QTimeZone::availableTimeZoneIds(QLocale::Country country) { - QSet set = QUtcTimeZonePrivate().availableTimeZoneIds(country) - + global_tz->backend->availableTimeZoneIds(country); - QList list = set.toList(); - std::sort(list.begin(), list.end()); - return list; + return set_union(QUtcTimeZonePrivate().availableTimeZoneIds(country), + global_tz->backend->availableTimeZoneIds(country)); } /*! @@ -868,11 +873,8 @@ QList QTimeZone::availableTimeZoneIds(QLocale::Country country) QList QTimeZone::availableTimeZoneIds(int offsetSeconds) { - QSet set = QUtcTimeZonePrivate().availableTimeZoneIds(offsetSeconds) - + global_tz->backend->availableTimeZoneIds(offsetSeconds); - QList list = set.toList(); - std::sort(list.begin(), list.end()); - return list; + return set_union(QUtcTimeZonePrivate().availableTimeZoneIds(offsetSeconds), + global_tz->backend->availableTimeZoneIds(offsetSeconds)); } /*! -- cgit v1.2.3