From patchwork Thu Jan 30 19:58:41 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= X-Patchwork-Id: 22714 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 60389BD808 for ; Thu, 30 Jan 2025 19:58:48 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 1BD7068572; Thu, 30 Jan 2025 20:58:48 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=protonmail.com header.i=@protonmail.com header.b="wN8PeKGr"; dkim-atps=neutral Received: from mail-4322.protonmail.ch (mail-4322.protonmail.ch [185.70.43.22]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 560AC6851B for ; Thu, 30 Jan 2025 20:58:46 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1738267125; x=1738526325; bh=lUpm/1QNowLSz3JziR4VCi/4wAJywYeIWioU9X+eg+0=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector:List-Unsubscribe:List-Unsubscribe-Post; b=wN8PeKGrqrcWIIwo7iJ+iHPapxyA3S5Kafz1DLQiNk05I5eKCwiRzR3SfK9pkvAda sVvdDzCdofmnyA6lKI4m+6xC7EBeFiLiGvUdotmgd+xozyUqhCWwUcOCoaHlbzQR3p Wl5uZDjmetIcup9xqHHWxLm2efyXC3OxDUaKmu9QjhW1UzZXm4DS5/LQuH1QKEFG9Y II476hchEbHvp2sfIYtHqu6ymTs0u8z8f65Lx/yc4+0Cf8/O0lpMJIkAuItRxtjy3T BNcrMVg5JEAMrH531pPbxWD6uP6xRbnjUU83gdZVk9fsnMtbVDK6z449vRehSrGQkd XAzCF105D1BGw== Date: Thu, 30 Jan 2025 19:58:41 +0000 To: libcamera-devel@lists.libcamera.org From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= Cc: Laurent Pinchart Subject: [RFC PATCH v2 5/9] libcamera: base: log: Use `std::string_view` to avoid some copies Message-ID: <20250130195811.1230581-6-pobrn@protonmail.com> In-Reply-To: <20250130195811.1230581-1-pobrn@protonmail.com> References: <20250130195811.1230581-1-pobrn@protonmail.com> Feedback-ID: 20568564:user:proton X-Pm-Message-ID: e3bf1bcbf4e43f9c453191ef29587c44cb99a65e MIME-Version: 1.0 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Use `std::string_view` to avoid some largely unnecessary copies, and to make string comparisong potentially faster by eliminating repeated `strlen()` calls. Signed-off-by: Barnabás Pőcze Reviewed-by: Laurent Pinchart Reviewed-by: Jacopo Mondi --- include/libcamera/base/log.h | 5 +++-- src/libcamera/base/log.cpp | 25 +++++++++++++------------ 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/include/libcamera/base/log.h b/include/libcamera/base/log.h index 4137d87a4..acef24203 100644 --- a/include/libcamera/base/log.h +++ b/include/libcamera/base/log.h @@ -9,6 +9,7 @@ #include #include +#include #include @@ -29,7 +30,7 @@ enum LogSeverity { class LogCategory { public: - static LogCategory *create(const char *name); + static LogCategory *create(std::string_view name); const std::string &name() const { return name_; } LogSeverity severity() const { return severity_.load(std::memory_order_relaxed); } @@ -38,7 +39,7 @@ public: static const LogCategory &defaultCategory(); private: - explicit LogCategory(const char *name); + explicit LogCategory(std::string_view name); const std::string name_; diff --git a/src/libcamera/base/log.cpp b/src/libcamera/base/log.cpp index 2abbad478..9415b6937 100644 --- a/src/libcamera/base/log.cpp +++ b/src/libcamera/base/log.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -312,11 +313,11 @@ private: void parseLogFile(); void parseLogLevels(); - static LogSeverity parseLogLevel(const std::string &level); + static LogSeverity parseLogLevel(std::string_view level); friend LogCategory; void registerCategory(LogCategory *category); - LogCategory *findCategory(const char *name) const; + LogCategory *findCategory(std::string_view name) const; static bool destroyed_; @@ -641,17 +642,17 @@ void Logger::parseLogLevels() if (!len) continue; - std::string category; - std::string level; + std::string_view category; + std::string_view level; const char *colon = static_cast(memchr(pair, ':', len)); if (!colon) { /* 'x' is a shortcut for '*:x'. */ category = "*"; - level = std::string(pair, len); + level = std::string_view(pair, len); } else { - category = std::string(pair, colon - pair); - level = std::string(colon + 1, comma - colon - 1); + category = std::string_view(pair, colon - pair); + level = std::string_view(colon + 1, comma - colon - 1); } /* Both the category and the level must be specified. */ @@ -662,7 +663,7 @@ void Logger::parseLogLevels() if (severity == LogInvalid) continue; - levels_.push_back({ category, severity }); + levels_.emplace_back(category, severity); } } @@ -676,7 +677,7 @@ void Logger::parseLogLevels() * * \return The log severity, or LogInvalid if the string is invalid */ -LogSeverity Logger::parseLogLevel(const std::string &level) +LogSeverity Logger::parseLogLevel(std::string_view level) { static const char *const names[] = { "DEBUG", @@ -743,7 +744,7 @@ void Logger::registerCategory(LogCategory *category) * \param[in] name Name of the log category * \return The pointer to the found log category or nullptr if not found */ -LogCategory *Logger::findCategory(const char *name) const +LogCategory *Logger::findCategory(std::string_view name) const { if (auto it = std::find_if(categories_.begin(), categories_.end(), [name](auto c) { return c->name() == name; }); @@ -787,7 +788,7 @@ LogCategory *Logger::findCategory(const char *name) const * * \return The pointer to the LogCategory */ -LogCategory *LogCategory::create(const char *name) +LogCategory *LogCategory::create(std::string_view name) { static Mutex mutex_; MutexLocker locker(mutex_); @@ -805,7 +806,7 @@ LogCategory *LogCategory::create(const char *name) * \brief Construct a log category * \param[in] name The category name */ -LogCategory::LogCategory(const char *name) +LogCategory::LogCategory(std::string_view name) : name_(name), severity_(LogSeverity::LogInfo) { }