[{"id":33264,"web_url":"https://patchwork.libcamera.org/comment/33264/","msgid":"<4jghm6g77tfam2wltlfllusndziqawtwk33kdtpvjypqqix433@3rxcnkpp3p6g>","date":"2025-02-03T16:46:44","subject":"Re: [RFC PATCH v2 5/9] libcamera: base: log: Use `std::string_view`\n\tto avoid some copies","submitter":{"id":143,"url":"https://patchwork.libcamera.org/api/people/143/","name":"Jacopo Mondi","email":"jacopo.mondi@ideasonboard.com"},"content":"Hi Barnabás\n\nOn Thu, Jan 30, 2025 at 07:58:41PM +0000, Barnabás Pőcze wrote:\n> Use `std::string_view` to avoid some largely unnecessary copies, and\n> to make string comparisong potentially faster by eliminating repeated\n> `strlen()` calls.\n>\n> Signed-off-by: Barnabás Pőcze <pobrn@protonmail.com>\n> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\nI recall Laurent had issues with string_view<>.\nApparently not in this case :)\n\nReviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>\n\nThanks\n  j\n\n> ---\n>  include/libcamera/base/log.h |  5 +++--\n>  src/libcamera/base/log.cpp   | 25 +++++++++++++------------\n>  2 files changed, 16 insertions(+), 14 deletions(-)\n>\n> diff --git a/include/libcamera/base/log.h b/include/libcamera/base/log.h\n> index 4137d87a4..acef24203 100644\n> --- a/include/libcamera/base/log.h\n> +++ b/include/libcamera/base/log.h\n> @@ -9,6 +9,7 @@\n>\n>  #include <atomic>\n>  #include <sstream>\n> +#include <string_view>\n>\n>  #include <libcamera/base/private.h>\n>\n> @@ -29,7 +30,7 @@ enum LogSeverity {\n>  class LogCategory\n>  {\n>  public:\n> -\tstatic LogCategory *create(const char *name);\n> +\tstatic LogCategory *create(std::string_view name);\n>\n>  \tconst std::string &name() const { return name_; }\n>  \tLogSeverity severity() const { return severity_.load(std::memory_order_relaxed); }\n> @@ -38,7 +39,7 @@ public:\n>  \tstatic const LogCategory &defaultCategory();\n>\n>  private:\n> -\texplicit LogCategory(const char *name);\n> +\texplicit LogCategory(std::string_view name);\n>\n>  \tconst std::string name_;\n>\n> diff --git a/src/libcamera/base/log.cpp b/src/libcamera/base/log.cpp\n> index 2abbad478..9415b6937 100644\n> --- a/src/libcamera/base/log.cpp\n> +++ b/src/libcamera/base/log.cpp\n> @@ -15,6 +15,7 @@\n>  #include <stdio.h>\n>  #include <stdlib.h>\n>  #include <string.h>\n> +#include <string_view>\n>  #include <syslog.h>\n>  #include <time.h>\n>  #include <unordered_set>\n> @@ -312,11 +313,11 @@ private:\n>\n>  \tvoid parseLogFile();\n>  \tvoid parseLogLevels();\n> -\tstatic LogSeverity parseLogLevel(const std::string &level);\n> +\tstatic LogSeverity parseLogLevel(std::string_view level);\n>\n>  \tfriend LogCategory;\n>  \tvoid registerCategory(LogCategory *category);\n> -\tLogCategory *findCategory(const char *name) const;\n> +\tLogCategory *findCategory(std::string_view name) const;\n>\n>  \tstatic bool destroyed_;\n>\n> @@ -641,17 +642,17 @@ void Logger::parseLogLevels()\n>  \t\tif (!len)\n>  \t\t\tcontinue;\n>\n> -\t\tstd::string category;\n> -\t\tstd::string level;\n> +\t\tstd::string_view category;\n> +\t\tstd::string_view level;\n>\n>  \t\tconst char *colon = static_cast<const char *>(memchr(pair, ':', len));\n>  \t\tif (!colon) {\n>  \t\t\t/* 'x' is a shortcut for '*:x'. */\n>  \t\t\tcategory = \"*\";\n> -\t\t\tlevel = std::string(pair, len);\n> +\t\t\tlevel = std::string_view(pair, len);\n>  \t\t} else {\n> -\t\t\tcategory = std::string(pair, colon - pair);\n> -\t\t\tlevel = std::string(colon + 1, comma - colon - 1);\n> +\t\t\tcategory = std::string_view(pair, colon - pair);\n> +\t\t\tlevel = std::string_view(colon + 1, comma - colon - 1);\n>  \t\t}\n>\n>  \t\t/* Both the category and the level must be specified. */\n> @@ -662,7 +663,7 @@ void Logger::parseLogLevels()\n>  \t\tif (severity == LogInvalid)\n>  \t\t\tcontinue;\n>\n> -\t\tlevels_.push_back({ category, severity });\n> +\t\tlevels_.emplace_back(category, severity);\n>  \t}\n>  }\n>\n> @@ -676,7 +677,7 @@ void Logger::parseLogLevels()\n>   *\n>   * \\return The log severity, or LogInvalid if the string is invalid\n>   */\n> -LogSeverity Logger::parseLogLevel(const std::string &level)\n> +LogSeverity Logger::parseLogLevel(std::string_view level)\n>  {\n>  \tstatic const char *const names[] = {\n>  \t\t\"DEBUG\",\n> @@ -743,7 +744,7 @@ void Logger::registerCategory(LogCategory *category)\n>   * \\param[in] name Name of the log category\n>   * \\return The pointer to the found log category or nullptr if not found\n>   */\n> -LogCategory *Logger::findCategory(const char *name) const\n> +LogCategory *Logger::findCategory(std::string_view name) const\n>  {\n>  \tif (auto it = std::find_if(categories_.begin(), categories_.end(),\n>  \t\t\t\t   [name](auto c) { return c->name() == name; });\n> @@ -787,7 +788,7 @@ LogCategory *Logger::findCategory(const char *name) const\n>   *\n>   * \\return The pointer to the LogCategory\n>   */\n> -LogCategory *LogCategory::create(const char *name)\n> +LogCategory *LogCategory::create(std::string_view name)\n>  {\n>  \tstatic Mutex mutex_;\n>  \tMutexLocker locker(mutex_);\n> @@ -805,7 +806,7 @@ LogCategory *LogCategory::create(const char *name)\n>   * \\brief Construct a log category\n>   * \\param[in] name The category name\n>   */\n> -LogCategory::LogCategory(const char *name)\n> +LogCategory::LogCategory(std::string_view name)\n>  \t: name_(name), severity_(LogSeverity::LogInfo)\n>  {\n>  }\n> --\n> 2.48.1\n>\n>","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 66BE2BD80A\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon,  3 Feb 2025 16:46:50 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 7B96B6859A;\n\tMon,  3 Feb 2025 17:46:49 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id DEC1061876\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon,  3 Feb 2025 17:46:47 +0100 (CET)","from ideasonboard.com (93-61-96-190.ip145.fastwebnet.it\n\t[93.61.96.190])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 454B1497;\n\tMon,  3 Feb 2025 17:45:36 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"g5FLFwod\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1738601136;\n\tbh=Di9GTuvVe8ydyB3BxvUdgi3coj08rRrccbqG5I0Ir4g=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=g5FLFwodNfnskz8IAeHdblXkG8dhTaIl68PAZVuKkTOug8wuAjSwWQNGNXIQrBwDX\n\te1WLbGO0iiJBBKKMuUmMC9BmMF+4wjeZev05ZjL8Pg1656169qRnVLtepjZNmVkhCR\n\tVfmpYqmZTAyH4T/Z5O7C44K8bin4L7vL5n+TMAxY=","Date":"Mon, 3 Feb 2025 17:46:44 +0100","From":"Jacopo Mondi <jacopo.mondi@ideasonboard.com>","To":"=?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= <pobrn@protonmail.com>","Cc":"libcamera-devel@lists.libcamera.org, \n\tLaurent Pinchart <laurent.pinchart@ideasonboard.com>","Subject":"Re: [RFC PATCH v2 5/9] libcamera: base: log: Use `std::string_view`\n\tto avoid some copies","Message-ID":"<4jghm6g77tfam2wltlfllusndziqawtwk33kdtpvjypqqix433@3rxcnkpp3p6g>","References":"<20250130195811.1230581-1-pobrn@protonmail.com>\n\t<20250130195811.1230581-6-pobrn@protonmail.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","Content-Transfer-Encoding":"8bit","In-Reply-To":"<20250130195811.1230581-6-pobrn@protonmail.com>","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]