[RFC PATCH v2 5/9] libcamera: base: log: Use `std::string_view` to avoid some copies

Barnabás Pőcze pobrn at protonmail.com
Thu Jan 30 20:58:41 CET 2025


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 <pobrn at protonmail.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart at ideasonboard.com>
---
 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 <atomic>
 #include <sstream>
+#include <string_view>
 
 #include <libcamera/base/private.h>
 
@@ -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 <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <string_view>
 #include <syslog.h>
 #include <time.h>
 #include <unordered_set>
@@ -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<const char *>(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)
 {
 }
-- 
2.48.1




More information about the libcamera-devel mailing list