{"id":22875,"url":"https://patchwork.libcamera.org/api/1.1/patches/22875/?format=json","web_url":"https://patchwork.libcamera.org/patch/22875/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20250225173531.2595922-6-pobrn@protonmail.com>","date":"2025-02-25T17:36:05","name":"[v4,5/8] libcamera: base: log: Use `std::string_view` to avoid some copies","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"6f0ae78c0101306d1fa70dec7b36ef0ad60151ea","submitter":{"id":133,"url":"https://patchwork.libcamera.org/api/1.1/people/133/?format=json","name":"Pőcze Barnabás","email":"pobrn@protonmail.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/22875/mbox/","series":[{"id":5022,"url":"https://patchwork.libcamera.org/api/1.1/series/5022/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=5022","date":"2025-02-25T17:35:34","name":"libcamera: base: log: Misc. changes","version":4,"mbox":"https://patchwork.libcamera.org/series/5022/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/22875/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/22875/checks/","tags":{},"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 F20CEBDB1C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 25 Feb 2025 17:36:12 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id A931B68737;\n\tTue, 25 Feb 2025 18:36:12 +0100 (CET)","from mail-40131.protonmail.ch (mail-40131.protonmail.ch\n\t[185.70.40.131])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 1EC1C68733\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 25 Feb 2025 18:36:11 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (2048-bit key;\n\tunprotected) header.d=protonmail.com header.i=@protonmail.com\n\theader.b=\"cmMJx9cF\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com;\n\ts=protonmail3; t=1740504970; x=1740764170;\n\tbh=R499hL1Oqw34Nkmesu/RcseK3JOp4A1gHphROydOY7s=;\n\th=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References:\n\tFeedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID:\n\tMessage-ID:BIMI-Selector:List-Unsubscribe:List-Unsubscribe-Post;\n\tb=cmMJx9cF27nPMk6xF/UaILEmMIaurD7ilgc7alIMr92ZJNuwUjThW/zDKCJgvOdiY\n\thBIf+N8MRQ8HRIh0vcg4B/NEVsqah8HJNv/PZ/6I3bU5ZVYQfbKL26quKNE3pHTAQ5\n\tsNSXDmXy3goCtnxaALSATmisWN10WJfrSX8tJG1Q4UzfJX2OXKFvzIExxiqng8Aeam\n\t8huZkRt/wsi/HSD5fr0/cBIgXxhLYgFk7UU7FDQQVpzJ1qfNwxB/2bawXL1ejUTLTX\n\twmCIdIxHbyJt+pvfBkKT02yTdN/45PrVcD993kf1GDyvPtmjQpCRFCOkjpRTN2dxfI\n\tQIm4NyKuUBphA==","Date":"Tue, 25 Feb 2025 17:36:05 +0000","To":"libcamera-devel@lists.libcamera.org","From":"=?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= <pobrn@protonmail.com>","Cc":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>,\n\tJacopo Mondi <jacopo.mondi@ideasonboard.com>","Subject":"[PATCH v4 5/8] libcamera: base: log: Use `std::string_view` to avoid\n\tsome copies","Message-ID":"<20250225173531.2595922-6-pobrn@protonmail.com>","In-Reply-To":"<20250225173531.2595922-1-pobrn@protonmail.com>","References":"<20250225173531.2595922-1-pobrn@protonmail.com>","Feedback-ID":"20568564:user:proton","X-Pm-Message-ID":"ec2462dbcadd5a661e27f9f9bb53ebc12b3ec1f4","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Transfer-Encoding":"quoted-printable","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>"},"content":"Use `std::string_view` to avoid some largely unnecessary copies, and\nto make string comparisong potentially faster by eliminating repeated\n`strlen()` calls.\n\nSigned-off-by: Barnabás Pőcze <pobrn@protonmail.com>\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\nReviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>\n---\n include/libcamera/base/log.h |  5 +++--\n src/libcamera/base/log.cpp   | 25 +++++++++++++------------\n 2 files changed, 16 insertions(+), 14 deletions(-)","diff":"diff --git a/include/libcamera/base/log.h b/include/libcamera/base/log.h\nindex 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 \ndiff --git a/src/libcamera/base/log.cpp b/src/libcamera/base/log.cpp\nindex ce3f04547..c0e0be60f 100644\n--- a/src/libcamera/base/log.cpp\n+++ b/src/libcamera/base/log.cpp\n@@ -16,6 +16,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@@ -313,11 +314,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@@ -642,17 +643,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@@ -663,7 +664,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@@ -677,7 +678,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@@ -729,7 +730,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@@ -773,7 +774,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@@ -791,7 +792,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","prefixes":["v4","5/8"]}