[{"id":33131,"web_url":"https://patchwork.libcamera.org/comment/33131/","msgid":"<2rqylwjybzs3dcnypfx7vkschst4ub74vnaibdugk7bpjvseph@w5zbigxpduvb>","date":"2025-01-22T08:56:53","subject":"Re: [RFC PATCH v2 01/16] apps: common: event_loop: Take callbacks by\n\trvalue ref","submitter":{"id":143,"url":"https://patchwork.libcamera.org/api/people/143/","name":"Jacopo Mondi","email":"jacopo.mondi@ideasonboard.com"},"content":"Hi Barnabás\n\nOn Tue, Jan 14, 2025 at 06:21:51PM +0000, Barnabás Pőcze wrote:\n> Using a const lvalue reference to `std::function<>` is not ideal\n> because it forces a copy to happen. Use an rvalue reference and\n> `std::move()` to avoid that.\n>\n> Signed-off-by: Barnabás Pőcze <pobrn@protonmail.com>\n> ---\n>  src/apps/common/event_loop.cpp | 16 ++++++++--------\n>  src/apps/common/event_loop.h   |  8 ++++----\n>  2 files changed, 12 insertions(+), 12 deletions(-)\n>\n> diff --git a/src/apps/common/event_loop.cpp b/src/apps/common/event_loop.cpp\n> index f7f9afa0c..bc8cf17ab 100644\n> --- a/src/apps/common/event_loop.cpp\n> +++ b/src/apps/common/event_loop.cpp\n> @@ -50,20 +50,20 @@ void EventLoop::exit(int code)\n>  \tevent_base_loopbreak(base_);\n>  }\n>\n> -void EventLoop::callLater(const std::function<void()> &func)\n> +void EventLoop::callLater(std::function<void()> &&func)\n\nI was very puzzled as I thought you were pointing out copies here\n\n>  {\n>  \t{\n>  \t\tstd::unique_lock<std::mutex> locker(lock_);\n> -\t\tcalls_.push_back(func);\n> +\t\tcalls_.push_back(std::move(func));\n\nBut they're instead here\n\nI wonder how costly this actually is, if it's about copying a pointer\nover it is not different than moving ? (if not that it invalidates the\npassed-in object).\n\nHowever, this doesn't hurt\nReviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>\n\n>  \t}\n>\n>  \tevent_base_once(base_, -1, EV_TIMEOUT, dispatchCallback, this, nullptr);\n>  }\n>\n>  void EventLoop::addFdEvent(int fd, EventType type,\n> -\t\t\t   const std::function<void()> &callback)\n> +\t\t\t   std::function<void()> &&callback)\n>  {\n> -\tstd::unique_ptr<Event> event = std::make_unique<Event>(callback);\n> +\tstd::unique_ptr<Event> event = std::make_unique<Event>(std::move(callback));\n>  \tshort events = (type & Read ? EV_READ : 0)\n>  \t\t     | (type & Write ? EV_WRITE : 0)\n>  \t\t     | EV_PERSIST;\n> @@ -85,9 +85,9 @@ void EventLoop::addFdEvent(int fd, EventType type,\n>  }\n>\n>  void EventLoop::addTimerEvent(const std::chrono::microseconds period,\n> -\t\t\t      const std::function<void()> &callback)\n> +\t\t\t      std::function<void()> &&callback)\n>  {\n> -\tstd::unique_ptr<Event> event = std::make_unique<Event>(callback);\n> +\tstd::unique_ptr<Event> event = std::make_unique<Event>(std::move(callback));\n>  \tevent->event_ = event_new(base_, -1, EV_PERSIST, &EventLoop::Event::dispatch,\n>  \t\t\t\t  event.get());\n>  \tif (!event->event_) {\n> @@ -131,8 +131,8 @@ void EventLoop::dispatchCall()\n>  \tcall();\n>  }\n>\n> -EventLoop::Event::Event(const std::function<void()> &callback)\n> -\t: callback_(callback), event_(nullptr)\n> +EventLoop::Event::Event(std::function<void()> &&callback)\n> +\t: callback_(std::move(callback)), event_(nullptr)\n>  {\n>  }\n>\n> diff --git a/src/apps/common/event_loop.h b/src/apps/common/event_loop.h\n> index ef129b9aa..d7d012c76 100644\n> --- a/src/apps/common/event_loop.h\n> +++ b/src/apps/common/event_loop.h\n> @@ -33,18 +33,18 @@ public:\n>  \tint exec();\n>  \tvoid exit(int code = 0);\n>\n> -\tvoid callLater(const std::function<void()> &func);\n> +\tvoid callLater(std::function<void()> &&func);\n>\n>  \tvoid addFdEvent(int fd, EventType type,\n> -\t\t\tconst std::function<void()> &handler);\n> +\t\t\tstd::function<void()> &&handler);\n>\n>  \tusing duration = std::chrono::steady_clock::duration;\n>  \tvoid addTimerEvent(const std::chrono::microseconds period,\n> -\t\t\t   const std::function<void()> &handler);\n> +\t\t\t   std::function<void()> &&handler);\n>\n>  private:\n>  \tstruct Event {\n> -\t\tEvent(const std::function<void()> &callback);\n> +\t\tEvent(std::function<void()> &&callback);\n>  \t\t~Event();\n>\n>  \t\tstatic void dispatch(int fd, short events, void *arg);\n> --\n> 2.48.0\n>\n>","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 584ECBD16B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 22 Jan 2025 08:57:01 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 22C1468558;\n\tWed, 22 Jan 2025 09:57:00 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 7162F6187C\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 22 Jan 2025 09:56:57 +0100 (CET)","from ideasonboard.com (93-61-96-190.ip145.fastwebnet.it\n\t[93.61.96.190])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 83E29134C;\n\tWed, 22 Jan 2025 09:55:54 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"MpmBec2r\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1737536154;\n\tbh=4edAcQQs1PJJODWAxP2FdNvngC5T/4GMoEWX77rUC9g=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=MpmBec2rYjSGuB/vxdoUzPYNxbyoqYGOYk/V1kMOlj4qamw79knfYSKoFFcosrf8w\n\tg2nrOiJEhiY3Uq6eL4qFnsVroko8L3Yhyeek5FaAl8tCh72NoY70+hfTB793cQPKFT\n\tFM+6tf8zGXYLubctzlKVJlFTeSulXnfTyTWhfktM=","Date":"Wed, 22 Jan 2025 09:56:53 +0100","From":"Jacopo Mondi <jacopo.mondi@ideasonboard.com>","To":"=?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= <pobrn@protonmail.com>","Cc":"libcamera-devel@lists.libcamera.org","Subject":"Re: [RFC PATCH v2 01/16] apps: common: event_loop: Take callbacks by\n\trvalue ref","Message-ID":"<2rqylwjybzs3dcnypfx7vkschst4ub74vnaibdugk7bpjvseph@w5zbigxpduvb>","References":"<20250114182143.1773762-1-pobrn@protonmail.com>\n\t<20250114182143.1773762-2-pobrn@protonmail.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","Content-Transfer-Encoding":"8bit","In-Reply-To":"<20250114182143.1773762-2-pobrn@protonmail.com>","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>"}}]