From patchwork Tue Oct 1 16:03:32 2024 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: 21464 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 67FDFBD80A for ; Tue, 1 Oct 2024 16:03:41 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 0B59E6351F; Tue, 1 Oct 2024 18:03:40 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=protonmail.com header.i=@protonmail.com header.b="sLLqa/FZ"; dkim-atps=neutral Received: from mail-40131.protonmail.ch (mail-40131.protonmail.ch [185.70.40.131]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id DC5FE60553 for ; Tue, 1 Oct 2024 18:03:37 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1727798617; x=1728057817; bh=7mvWqoyVvh6aH3WnfwLU45+LJ8ciuxTe4XhsXdm+Mwk=; h=Date:To:From:Subject:Message-ID:Feedback-ID:From:To:Cc:Date: Subject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector; b=sLLqa/FZVr5eGkE+TgytV/pUNT6r7M1cTS2KHLTQO8QiJ+brPjXFSi4L9szHSQCxp K52fl7slztZGjo3P1xnp7rgl/RleNNXGM5SI+IQFTWmpAIhKmpzbDluxdVJ8vFKTA2 5RnHwSTMG67UVDYmx2qQIFqfNcNBZ78FkcTlrvg74kCciFLn2z7aUN/YzwDXhfdeU3 Sa1/gcBRz7ofpdIdk0Mo6mv4FBIbTxxXaXDpnyNOGaFY5tOc6xfcGy6VuwBAa41vuh Cej1KHuItY+bh/DflZW/es7FtROmYlg9c4so1QataBX8B5ZyFqT+5abN3X2yF/V5HS 5Tv7u+c+rODkA== Date: Tue, 01 Oct 2024 16:03:32 +0000 To: libcamera-devel@lists.libcamera.org From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= Subject: [PATCH v1] libcamera: yaml_parser: Take string keys in `std::string_view` Message-ID: <20241001160329.725278-1-pobrn@protonmail.com> Feedback-ID: 20568564:user:proton X-Pm-Message-ID: 2a2285386b5b833cd19048f6b90b2dafd709e3ee 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" In many cases a static string literal is used as key. Thus having the argument type be `const std::string&` is suboptimal since an `std::string` object needs to be constructed before the call. C++17 introduced `std::string_view`, using which the call can be done with less overhead, as the `std::string_view` is non-owning and may be passed in registers entirely. So make `YamlObject::{contains,operator[]}` take the string keys in `std::string_view`s. Unfortunately, that is not sufficient yet, because `std::map::find()` takes an reference to `const key_type`, which would be `const std::string&` in the case of `YamlParser`. However, with a transparent comparator such as `std::less<>` `std::map::find()` is able to accept any object as the argument, and it forwards it to the comparator. So make `YamlParser::dictionary_` use `std::less<>` as the comparator to enable the use of `std::map::find()` with any type of argument. Signed-off-by: Barnabás Pőcze Reviewed-by: Kieran Bingham Reviewed-by: Laurent Pinchart --- include/libcamera/internal/yaml_parser.h | 7 ++++--- src/libcamera/yaml_parser.cpp | 11 ++++------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/include/libcamera/internal/yaml_parser.h b/include/libcamera/internal/yaml_parser.h index 6211ff4a..de452844 100644 --- a/include/libcamera/internal/yaml_parser.h +++ b/include/libcamera/internal/yaml_parser.h @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -206,8 +207,8 @@ public: const YamlObject &operator[](std::size_t index) const; - bool contains(const std::string &key) const; - const YamlObject &operator[](const std::string &key) const; + bool contains(std::string_view key) const; + const YamlObject &operator[](std::string_view key) const; private: LIBCAMERA_DISABLE_COPY_AND_MOVE(YamlObject) @@ -232,7 +233,7 @@ private: std::string value_; Container list_; - std::map dictionary_; + std::map> dictionary_; }; class YamlParser final diff --git a/src/libcamera/yaml_parser.cpp b/src/libcamera/yaml_parser.cpp index 4784f2dc..7c0b341a 100644 --- a/src/libcamera/yaml_parser.cpp +++ b/src/libcamera/yaml_parser.cpp @@ -481,16 +481,13 @@ const YamlObject &YamlObject::operator[](std::size_t index) const * * \return True if an element exists, false otherwise */ -bool YamlObject::contains(const std::string &key) const +bool YamlObject::contains(std::string_view key) const { - if (dictionary_.find(std::ref(key)) == dictionary_.end()) - return false; - - return true; + return dictionary_.find(key) != dictionary_.end(); } /** - * \fn YamlObject::operator[](const std::string &key) const + * \fn YamlObject::operator[](std::string_view key) const * \brief Retrieve a member by name from the dictionary * * This function retrieve a member of a YamlObject by name. Only YamlObject @@ -500,7 +497,7 @@ bool YamlObject::contains(const std::string &key) const * * \return The YamlObject corresponding to the \a key member */ -const YamlObject &YamlObject::operator[](const std::string &key) const +const YamlObject &YamlObject::operator[](std::string_view key) const { if (type_ != Type::Dictionary) return empty;