From 2bdb9e99eee3190ca5437d394cece78e043eacd6 Mon Sep 17 00:00:00 2001 From: ComixHe Date: Mon, 21 Aug 2023 18:38:27 +0800 Subject: [PATCH] refact: optimize regular expression initialization add profiling test. Signed-off-by: ComixHe --- CMakeLists.txt | 5 +++++ apps/dde-application-manager/src/main.cpp | 15 +++++++++++++-- src/desktopentry.cpp | 12 ++++++++---- src/global.h | 3 +++ tools/profiling.sh | 20 ++++++++++++++++++++ tools/test-coverage.sh | 2 +- 6 files changed, 50 insertions(+), 7 deletions(-) create mode 100755 tools/profiling.sh diff --git a/CMakeLists.txt b/CMakeLists.txt index a94ae70..9d79a78 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,6 +15,7 @@ set(THREADS_PREFER_PTHREAD_FLAG ON) set(BUILD_EXAMPLES ON CACHE BOOL "Whether to build examples or not.") set(DEBUG_MODE ON CACHE BOOL "start a dde-applicatiom-manager service for debug") +set(PROFILING_MODE OFF CACHE BOOL "run a valgrind performance profiling.") find_package(Qt6 REQUIRED COMPONENTS Core DBus Concurrent) find_package(Threads REQUIRED) @@ -25,6 +26,10 @@ if(DEBUG_MODE) add_compile_definitions(-DDEBUG_MODE) endif() +if(PROFILING_MODE) + add_compile_definitions(-DDPROFILING_MODE) +endif() + add_subdirectory(src) add_subdirectory(plugins) add_subdirectory(apps) diff --git a/apps/dde-application-manager/src/main.cpp b/apps/dde-application-manager/src/main.cpp index c2cd5ff..0130a74 100644 --- a/apps/dde-application-manager/src/main.cpp +++ b/apps/dde-application-manager/src/main.cpp @@ -8,6 +8,10 @@ #include #include "dbus/applicationmanager1service.h" #include "cgroupsidentifier.h" +#include +#include + +Q_LOGGING_CATEGORY(DDEAMProf, "dde.am.prof", QtInfoMsg) namespace { void registerComplexDbusType() @@ -21,6 +25,7 @@ void registerComplexDbusType() int main(int argc, char *argv[]) { + auto start = std::chrono::high_resolution_clock::now(); QCoreApplication app{argc, argv}; auto &bus = ApplicationManager1DBus::instance(); @@ -43,6 +48,12 @@ int main(int argc, char *argv[]) AMService.addApplication(std::move(ret).value()); return false; // means to apply this function to the rest of the files }); - - return QCoreApplication::exec(); + auto end = std::chrono::high_resolution_clock::now(); + qCInfo(DDEAMProf) << std::chrono::duration_cast(end - start).count() << "ms"; + return +#ifdef PROFILING_MODE + QCoreApplication::exec(); +#else + 0; +#endif } diff --git a/src/desktopentry.cpp b/src/desktopentry.cpp index abdb09e..8c9c4a2 100644 --- a/src/desktopentry.cpp +++ b/src/desktopentry.cpp @@ -59,8 +59,12 @@ DesktopErrorCode DesktopEntry::parseEntry(const QString &str, decltype(m_entryMa const static auto validKey = QString("^%1(?:\\[(?%2%3?%4?%5?)\\])?$").arg(MainKey).arg(Language).arg(Country).arg(Encoding).arg(Modifier); // example: https://regex101.com/r/hylOay/1 - QRegularExpression re{validKey}; - re.optimize(); + static QRegularExpression re = []() -> QRegularExpression { + QRegularExpression tmp{validKey}; + tmp.optimize(); + return tmp; + }(); + auto matcher = re.match(keyStr); if (!matcher.hasMatch()) { #ifdef DEBUG_MODE @@ -216,8 +220,8 @@ DesktopErrorCode DesktopEntry::parse(QTextStream &stream) noexcept continue; } - if (line.startsWith("[")) { - if (!line.endsWith("]")) { + if (line[0] == '[') { + if (!(line[line.size() - 1] == ']')) { return DesktopErrorCode::GroupHeaderInvalid; } currentGroup = parserGroupHeader(line); diff --git a/src/global.h b/src/global.h index a835f03..12f7bdd 100644 --- a/src/global.h +++ b/src/global.h @@ -19,10 +19,13 @@ #include #include #include +#include #include #include "constant.h" #include "config.h" +Q_DECLARE_LOGGING_CATEGORY(DDEAMProf) + using IconMap = QMap>>; using ObjectMap = QMap; diff --git a/tools/profiling.sh b/tools/profiling.sh new file mode 100755 index 0000000..ff8a4cc --- /dev/null +++ b/tools/profiling.sh @@ -0,0 +1,20 @@ +#!/bin/env bash + +# SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. +# +# SPDX-License-Identifier: LGPL-3.0-or-later + +cd "$(git rev-parse --show-toplevel)" || exit 255 + +BUILD_DIR=${BUILD_DIR:="build-prof"} + +cmake -B "$BUILD_DIR" \ + -DCMAKE_BUILD_TYPE=RelWithDebInfo \ + -DDEBUG_MODE=OFF \ + -DPROFILING_MODE=ON + +cmake --build "$BUILD_DIR" -j$(nproc) + +QT_LOGGING_RULES="*.debug=false;*.info=false;*.warning=false" valgrind --tool=cachegrind --cachegrind-out-file="$BUILD_DIR/profiling.out" "$BUILD_DIR/apps/dde-application-manager/src/dde-application-manager" + +echo "you can use Kcachegrind to check this profiling result." diff --git a/tools/test-coverage.sh b/tools/test-coverage.sh index 1db2636..d6564ad 100755 --- a/tools/test-coverage.sh +++ b/tools/test-coverage.sh @@ -21,4 +21,4 @@ cmake --build "$BUILD_DIR" -j$(nproc) cmake --build "$BUILD_DIR" -j$(nproc) -t test -gcovr -f "src/*" --html-details "$BUILD_DIR"/coverage.html +gcovr -f "src/*" --html-details "$HTML_DIR"/coverage.html