From 08fff11232ca6ca5165ba76bd268881a8a0060a1 Mon Sep 17 00:00:00 2001 From: Anton Kudryavtsev Date: Mon, 30 Oct 2023 18:28:30 +0300 Subject: [PATCH] qnetworkcookiejar: optimize cookiesForUrl MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Check cheap conditions first. Use string view types more Don't insert in the middle of list, just append and then sort Change-Id: I1ad7c3459eab45b2289bfe044314eec7130d8153 Reviewed-by: MÃ¥rten Nordheim --- src/network/access/qnetworkcookiejar.cpp | 43 +++++++++--------------- 1 file changed, 16 insertions(+), 27 deletions(-) diff --git a/src/network/access/qnetworkcookiejar.cpp b/src/network/access/qnetworkcookiejar.cpp index c83fb0574e..270408a4bb 100644 --- a/src/network/access/qnetworkcookiejar.cpp +++ b/src/network/access/qnetworkcookiejar.cpp @@ -200,49 +200,38 @@ QList QNetworkCookieJar::cookiesForUrl(const QUrl &url) const Q_D(const QNetworkCookieJar); const QDateTime now = QDateTime::currentDateTimeUtc(); QList result; - bool isEncrypted = url.scheme() == "https"_L1; + const bool isEncrypted = url.scheme() == "https"_L1; // scan our cookies for something that matches - QList::ConstIterator it = d->allCookies.constBegin(), - end = d->allCookies.constEnd(); - for ( ; it != end; ++it) { - if (!isParentDomain(url.host(), it->domain())) + for (const auto &cookie : std::as_const(d->allCookies)) { + if (!isEncrypted && cookie.isSecure()) continue; - if (!isParentPath(url.path(), it->path())) + if (!cookie.isSessionCookie() && cookie.expirationDate() < now) continue; - if (!(*it).isSessionCookie() && (*it).expirationDate() < now) + const QString urlHost = url.host(); + const QString cookieDomain = cookie.domain(); + if (!isParentDomain(urlHost, cookieDomain)) continue; - if ((*it).isSecure() && !isEncrypted) + if (!isParentPath(url.path(), cookie.path())) continue; - QString domain = it->domain(); + QStringView domain = cookieDomain; if (domain.startsWith(u'.')) /// Qt6?: remove when compliant with RFC6265 - domain = domain.mid(1); + domain = domain.sliced(1); #if QT_CONFIG(topleveldomain) - if (qIsEffectiveTLD(domain) && url.host() != domain) + if (urlHost != domain && qIsEffectiveTLD(domain)) continue; #else - if (!domain.contains(u'.') && url.host() != domain) + if (!domain.contains(u'.') && urlHost != domain) continue; #endif // topleveldomain - // insert this cookie into result, sorted by path - QList::Iterator insertIt = result.begin(); - while (insertIt != result.end()) { - if (insertIt->path().size() < it->path().size()) { - // insert here - insertIt = result.insert(insertIt, *it); - break; - } else { - ++insertIt; - } - } - - // this is the shortest path yet, just append - if (insertIt == result.end()) - result += *it; + result += cookie; } + auto longerPath = [](const auto &c1, const auto &c2) + { return c1.path().size() > c2.path().size(); }; + std::sort(result.begin(), result.end(), longerPath); return result; }