[libcamera-devel,v1] libcamera: framebuffer_allocator: Avoid double map lookup
diff mbox series

Message ID mailman.131.1669506152.939.libcamera-devel@lists.libcamera.org
State Accepted
Headers show
Series
  • [libcamera-devel,v1] libcamera: framebuffer_allocator: Avoid double map lookup
Related show

Commit Message

Barnabás Pőcze Nov. 26, 2022, 11:42 p.m. UTC
Use `try_emplace()` on the map instead of `count()`
and `operator[]` to avoid walking the tree twice.

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

--
2.38.1

Comments

Laurent Pinchart Nov. 28, 2022, 6:37 a.m. UTC | #1
Hi Barnabás,

Thank you for the patch.

On Sat, Nov 26, 2022 at 11:42:27PM +0000, Barnabás Pőcze via libcamera-devel wrote:
> Use `try_emplace()` on the map instead of `count()`
> and `operator[]` to avoid walking the tree twice.
> 
> Signed-off-by: Barnabás Pőcze <pobrn@protonmail.com>
> ---
>  src/libcamera/framebuffer_allocator.cpp | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/src/libcamera/framebuffer_allocator.cpp b/src/libcamera/framebuffer_allocator.cpp
> index 4df27cac..17c5db49 100644
> --- a/src/libcamera/framebuffer_allocator.cpp
> +++ b/src/libcamera/framebuffer_allocator.cpp
> @@ -88,12 +88,14 @@ FrameBufferAllocator::~FrameBufferAllocator()
>   */
>  int FrameBufferAllocator::allocate(Stream *stream)
>  {
> -	if (buffers_.count(stream)) {
> +	const auto &[ it, inserted ] = buffers_.try_emplace(stream);

No spaces within [ ... ]

Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

No need to resubmit if you agree with this change, I'll fix when
applying.

> +
> +	if (!inserted) {
>  		LOG(Allocator, Error) << "Buffers already allocated for stream";
>  		return -EBUSY;
>  	}
> 
> -	int ret = camera_->exportFrameBuffers(stream, &buffers_[stream]);
> +	int ret = camera_->exportFrameBuffers(stream, &it->second);
>  	if (ret == -EINVAL)
>  		LOG(Allocator, Error)
>  			<< "Stream is not part of " << camera_->id()
Kieran Bingham Nov. 28, 2022, 9:39 a.m. UTC | #2
Quoting Laurent Pinchart via libcamera-devel (2022-11-28 06:37:56)
> Hi Barnabás,
> 
> Thank you for the patch.
> 
> On Sat, Nov 26, 2022 at 11:42:27PM +0000, Barnabás Pőcze via libcamera-devel wrote:
> > Use `try_emplace()` on the map instead of `count()`
> > and `operator[]` to avoid walking the tree twice.
> > 
> > Signed-off-by: Barnabás Pőcze <pobrn@protonmail.com>
> > ---
> >  src/libcamera/framebuffer_allocator.cpp | 6 ++++--
> >  1 file changed, 4 insertions(+), 2 deletions(-)
> > 
> > diff --git a/src/libcamera/framebuffer_allocator.cpp b/src/libcamera/framebuffer_allocator.cpp
> > index 4df27cac..17c5db49 100644
> > --- a/src/libcamera/framebuffer_allocator.cpp
> > +++ b/src/libcamera/framebuffer_allocator.cpp
> > @@ -88,12 +88,14 @@ FrameBufferAllocator::~FrameBufferAllocator()
> >   */
> >  int FrameBufferAllocator::allocate(Stream *stream)
> >  {
> > -     if (buffers_.count(stream)) {
> > +     const auto &[ it, inserted ] = buffers_.try_emplace(stream);
> 
> No spaces within [ ... ]
> 
> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

LGTM too.


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

> 
> No need to resubmit if you agree with this change, I'll fix when
> applying.
> 
> > +
> > +     if (!inserted) {
> >               LOG(Allocator, Error) << "Buffers already allocated for stream";
> >               return -EBUSY;
> >       }
> > 
> > -     int ret = camera_->exportFrameBuffers(stream, &buffers_[stream]);
> > +     int ret = camera_->exportFrameBuffers(stream, &it->second);
> >       if (ret == -EINVAL)
> >               LOG(Allocator, Error)
> >                       << "Stream is not part of " << camera_->id()
> 
> -- 
> Regards,
> 
> Laurent Pinchart
Barnabás Pőcze Nov. 28, 2022, 1:59 p.m. UTC | #3
2022. november 28., hétfő 7:37 keltezéssel, Laurent Pinchart írta:


> Hi Barnabás,
> 
> Thank you for the patch.
> 
> On Sat, Nov 26, 2022 at 11:42:27PM +0000, Barnabás Pőcze via libcamera-devel wrote:
> 
> > Use `try_emplace()` on the map instead of `count()`
> > and `operator[]` to avoid walking the tree twice.
> > 
> > Signed-off-by: Barnabás Pőcze pobrn@protonmail.com
> > ---
> > src/libcamera/framebuffer_allocator.cpp | 6 ++++--
> > 1 file changed, 4 insertions(+), 2 deletions(-)
> > 
> > diff --git a/src/libcamera/framebuffer_allocator.cpp b/src/libcamera/framebuffer_allocator.cpp
> > index 4df27cac..17c5db49 100644
> > --- a/src/libcamera/framebuffer_allocator.cpp
> > +++ b/src/libcamera/framebuffer_allocator.cpp
> > @@ -88,12 +88,14 @@ FrameBufferAllocator::~FrameBufferAllocator()
> > */
> > int FrameBufferAllocator::allocate(Stream *stream)
> > {
> > - if (buffers_.count(stream)) {
> > + const auto &[ it, inserted ] = buffers_.try_emplace(stream);
> 
> 
> No spaces within [ ... ]
> 
> Reviewed-by: Laurent Pinchart laurent.pinchart@ideasonboard.com
> 
> 
> No need to resubmit if you agree with this change, I'll fix when
> applying.

Yes, please, fix it when applying.


Regards,
Barnabás Pőcze

> 
> > +
> > + if (!inserted) {
> > LOG(Allocator, Error) << "Buffers already allocated for stream";
> > return -EBUSY;
> > }
> > 
> > - int ret = camera_->exportFrameBuffers(stream, &buffers_[stream]);
> > + int ret = camera_->exportFrameBuffers(stream, &it->second);
> > if (ret == -EINVAL)
> > LOG(Allocator, Error)
> > << "Stream is not part of " << camera_->id()
> 
> 
> --
> Regards,
> 
> Laurent Pinchart

Patch
diff mbox series

diff --git a/src/libcamera/framebuffer_allocator.cpp b/src/libcamera/framebuffer_allocator.cpp
index 4df27cac..17c5db49 100644
--- a/src/libcamera/framebuffer_allocator.cpp
+++ b/src/libcamera/framebuffer_allocator.cpp
@@ -88,12 +88,14 @@  FrameBufferAllocator::~FrameBufferAllocator()
  */
 int FrameBufferAllocator::allocate(Stream *stream)
 {
-	if (buffers_.count(stream)) {
+	const auto &[ it, inserted ] = buffers_.try_emplace(stream);
+
+	if (!inserted) {
 		LOG(Allocator, Error) << "Buffers already allocated for stream";
 		return -EBUSY;
 	}

-	int ret = camera_->exportFrameBuffers(stream, &buffers_[stream]);
+	int ret = camera_->exportFrameBuffers(stream, &it->second);
 	if (ret == -EINVAL)
 		LOG(Allocator, Error)
 			<< "Stream is not part of " << camera_->id()