[v1] libcamera: yaml_parser: Avoid double lookup in `operator[]`
diff mbox series

Message ID 20240520035232.205777-1-pobrn@protonmail.com
State Accepted
Commit 168bb3c97c5f338486251ae7752bccd4e61ffcc0
Headers show
Series
  • [v1] libcamera: yaml_parser: Avoid double lookup in `operator[]`
Related show

Commit Message

Barnabás Pőcze May 20, 2024, 3:52 a.m. UTC
`YamlObject::contains()` does the same search,
doing the lookup twice in unnecessary.

Signed-off-by: Barnabás Pőcze <pobrn@protonmail.com>
---
 src/libcamera/yaml_parser.cpp | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

Comments

Barnabás Pőcze May 20, 2024, 3:55 a.m. UTC | #1
2024. május 20., hétfő 5:52 keltezéssel, Barnabás Pőcze <pobrn@protonmail.com> írta:

> `YamlObject::contains()` does the same search,
> doing the lookup twice in unnecessary.

Oops, this should say "is" instead of "in".


> 
> Signed-off-by: Barnabás Pőcze <pobrn@protonmail.com>
> ---
>  src/libcamera/yaml_parser.cpp | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
> 
> diff --git a/src/libcamera/yaml_parser.cpp b/src/libcamera/yaml_parser.cpp
> index 55f81916..aac9a2bd 100644
> --- a/src/libcamera/yaml_parser.cpp
> +++ b/src/libcamera/yaml_parser.cpp
> @@ -468,10 +468,13 @@ bool YamlObject::contains(const std::string &key) const
>   */
>  const YamlObject &YamlObject::operator[](const std::string &key) const
>  {
> -	if (type_ != Type::Dictionary || !contains(key))
> +	if (type_ != Type::Dictionary)
>  		return empty;
>  
>  	auto iter = dictionary_.find(key);
> +	if (iter == dictionary_.end())
> +		return empty;
> +
>  	return *iter->second;
>  }
>  
> -- 
> 2.45.1
Kieran Bingham May 20, 2024, 10:09 a.m. UTC | #2
Quoting Barnabás Pőcze (2024-05-20 04:55:43)
> 2024. május 20., hétfő 5:52 keltezéssel, Barnabás Pőcze <pobrn@protonmail.com> írta:
> 
> > `YamlObject::contains()` does the same search,
> > doing the lookup twice in unnecessary.
> 
> Oops, this should say "is" instead of "in".

Can be fixed while applying to avoid the need for a v2.

Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>

> > Signed-off-by: Barnabás Pőcze <pobrn@protonmail.com>
> > ---
> >  src/libcamera/yaml_parser.cpp | 5 ++++-
> >  1 file changed, 4 insertions(+), 1 deletion(-)
> > 
> > diff --git a/src/libcamera/yaml_parser.cpp b/src/libcamera/yaml_parser.cpp
> > index 55f81916..aac9a2bd 100644
> > --- a/src/libcamera/yaml_parser.cpp
> > +++ b/src/libcamera/yaml_parser.cpp
> > @@ -468,10 +468,13 @@ bool YamlObject::contains(const std::string &key) const
> >   */
> >  const YamlObject &YamlObject::operator[](const std::string &key) const
> >  {
> > -     if (type_ != Type::Dictionary || !contains(key))
> > +     if (type_ != Type::Dictionary)
> >               return empty;
> >  
> >       auto iter = dictionary_.find(key);
> > +     if (iter == dictionary_.end())
> > +             return empty;
> > +
> >       return *iter->second;
> >  }
> >  
> > -- 
> > 2.45.1
Dan Scally May 21, 2024, 7:10 a.m. UTC | #3
Hi Barnabas - thanks for the patch

On 20/05/2024 04:52, Barnabás Pőcze wrote:
> `YamlObject::contains()` does the same search,
> doing the lookup twice in unnecessary.
>
> Signed-off-by: Barnabás Pőcze <pobrn@protonmail.com>


Reviewed-by: Daniel Scally <dan.scally@ideasonboard.com>

> ---
>   src/libcamera/yaml_parser.cpp | 5 ++++-
>   1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/src/libcamera/yaml_parser.cpp b/src/libcamera/yaml_parser.cpp
> index 55f81916..aac9a2bd 100644
> --- a/src/libcamera/yaml_parser.cpp
> +++ b/src/libcamera/yaml_parser.cpp
> @@ -468,10 +468,13 @@ bool YamlObject::contains(const std::string &key) const
>    */
>   const YamlObject &YamlObject::operator[](const std::string &key) const
>   {
> -	if (type_ != Type::Dictionary || !contains(key))
> +	if (type_ != Type::Dictionary)
>   		return empty;
>   
>   	auto iter = dictionary_.find(key);
> +	if (iter == dictionary_.end())
> +		return empty;
> +
>   	return *iter->second;
>   }
>

Patch
diff mbox series

diff --git a/src/libcamera/yaml_parser.cpp b/src/libcamera/yaml_parser.cpp
index 55f81916..aac9a2bd 100644
--- a/src/libcamera/yaml_parser.cpp
+++ b/src/libcamera/yaml_parser.cpp
@@ -468,10 +468,13 @@  bool YamlObject::contains(const std::string &key) const
  */
 const YamlObject &YamlObject::operator[](const std::string &key) const
 {
-	if (type_ != Type::Dictionary || !contains(key))
+	if (type_ != Type::Dictionary)
 		return empty;
 
 	auto iter = dictionary_.find(key);
+	if (iter == dictionary_.end())
+		return empty;
+
 	return *iter->second;
 }