From a3ca277eafbfbe1d7e0d9896fa3f772c1f31c188 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Sun, 4 Aug 2019 16:44:12 +0800 Subject: [PATCH 1/3] Avoid back and forth string conversions --- src/webui/api/apicontroller.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/webui/api/apicontroller.cpp b/src/webui/api/apicontroller.cpp index c306a351b..40de8dcdf 100644 --- a/src/webui/api/apicontroller.cpp +++ b/src/webui/api/apicontroller.cpp @@ -45,8 +45,8 @@ QVariant APIController::run(const QString &action, const StringMap ¶ms, cons m_params = params; m_data = data; - const QString methodName {action + QLatin1String("Action")}; - if (!QMetaObject::invokeMethod(this, methodName.toLatin1().constData())) + const QByteArray methodName = action.toLatin1() + "Action"; + if (!QMetaObject::invokeMethod(this, methodName.constData())) throw APIError(APIErrorType::NotFound); return m_result; From c004a84f84265bf54ef3371ac48273879d560241 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Sun, 4 Aug 2019 17:05:19 +0800 Subject: [PATCH 2/3] Avoid redundant actions/conversions QSet::contains(QSet &) isn't doing anything magical it still has the usual loop inside, so lets just save all the container conversions and do the loop ourselves. --- src/webui/api/apicontroller.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/webui/api/apicontroller.cpp b/src/webui/api/apicontroller.cpp index 40de8dcdf..140ade32d 100644 --- a/src/webui/api/apicontroller.cpp +++ b/src/webui/api/apicontroller.cpp @@ -28,6 +28,8 @@ #include "apicontroller.h" +#include + #include #include @@ -69,9 +71,13 @@ const DataMap &APIController::data() const void APIController::checkParams(const QSet &requiredParams) const { - const QSet params {this->params().keys().toSet()}; + const bool hasAllRequiredParams = std::all_of(requiredParams.cbegin(), requiredParams.cend() + , [this](const QString &requiredParam) + { + return params().contains(requiredParam); + }); - if (!params.contains(requiredParams)) + if (!hasAllRequiredParams) throw APIError(APIErrorType::BadParams); } From f4e7b8c6bfdbeb7faf9799669c555ab186aa53b9 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Sun, 4 Aug 2019 17:22:28 +0800 Subject: [PATCH 3/3] Replace QMap with QHash --- src/base/bittorrent/tracker.cpp | 2 +- src/base/http/types.h | 4 ++-- src/webui/api/apicontroller.h | 7 ++++--- src/webui/webapplication.h | 2 +- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/base/bittorrent/tracker.cpp b/src/base/bittorrent/tracker.cpp index d52280727..e9d140dc5 100644 --- a/src/base/bittorrent/tracker.cpp +++ b/src/base/bittorrent/tracker.cpp @@ -128,7 +128,7 @@ Http::Response Tracker::processRequest(const Http::Request &request, const Http: void Tracker::respondToAnnounceRequest() { - const QMap &queryParams = m_request.query; + const QHash &queryParams = m_request.query; TrackerAnnounceRequest announceReq; // IP diff --git a/src/base/http/types.h b/src/base/http/types.h index 6bb443d6a..e89970c2c 100644 --- a/src/base/http/types.h +++ b/src/base/http/types.h @@ -98,8 +98,8 @@ namespace Http QString method; QString path; QStringMap headers; - QMap query; - QStringMap posts; + QHash query; + QHash posts; QVector files; }; diff --git a/src/webui/api/apicontroller.h b/src/webui/api/apicontroller.h index be620832a..0d2044efb 100644 --- a/src/webui/api/apicontroller.h +++ b/src/webui/api/apicontroller.h @@ -28,7 +28,7 @@ #pragma once -#include +#include #include #include #include @@ -36,8 +36,9 @@ class QString; struct ISessionManager; -using StringMap = QMap; -using DataMap = QMap; + +using DataMap = QHash; +using StringMap = QHash; class APIController : public QObject { diff --git a/src/webui/webapplication.h b/src/webui/webapplication.h index c6fb53811..c4f4bf3b7 100644 --- a/src/webui/webapplication.h +++ b/src/webui/webapplication.h @@ -123,7 +123,7 @@ private: WebSession *m_currentSession = nullptr; Http::Request m_request; Http::Environment m_env; - QMap m_params; + QHash m_params; const QString m_cacheID; const QRegularExpression m_apiPathPattern {(QLatin1String("^/api/v2/(?[A-Za-z_][A-Za-z_0-9]*)/(?[A-Za-z_][A-Za-z_0-9]*)$"))};