Message ID | 20250130115001.1129305-2-pobrn@protonmail.com |
---|---|
State | New |
Headers | show |
Series |
|
Related | show |
Hi Barnabás On Thu, Jan 30, 2025 at 11:50:08AM +0000, Barnabás Pőcze wrote: > Using a const lvalue reference to `std::function<>` is not ideal > because it forces a copy to happen. Use an rvalue reference and > `std::move()` to avoid that. > > Signed-off-by: Barnabás Pőcze <pobrn@protonmail.com> > Reviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com> > Reviewed-by: Paul Elder <paul.elder@ideasonboard.com> > --- > src/apps/common/event_loop.cpp | 16 ++++++++-------- > src/apps/common/event_loop.h | 8 ++++---- > 2 files changed, 12 insertions(+), 12 deletions(-) > > diff --git a/src/apps/common/event_loop.cpp b/src/apps/common/event_loop.cpp > index f7f9afa0c..bc8cf17ab 100644 > --- a/src/apps/common/event_loop.cpp > +++ b/src/apps/common/event_loop.cpp > @@ -50,20 +50,20 @@ void EventLoop::exit(int code) > event_base_loopbreak(base_); > } > > -void EventLoop::callLater(const std::function<void()> &func) > +void EventLoop::callLater(std::function<void()> &&func) > { > { > std::unique_lock<std::mutex> locker(lock_); > - calls_.push_back(func); > + calls_.push_back(std::move(func)); > } > > event_base_once(base_, -1, EV_TIMEOUT, dispatchCallback, this, nullptr); > } > > void EventLoop::addFdEvent(int fd, EventType type, > - const std::function<void()> &callback) > + std::function<void()> &&callback) > { > - std::unique_ptr<Event> event = std::make_unique<Event>(callback); > + std::unique_ptr<Event> event = std::make_unique<Event>(std::move(callback)); // rvalue reference variables are lvalues when used in expressions int&& x = 1; f(x); // calls f(int& x) f(std::move(x)); // calls f(int&& x) Does one ever stop being fooled by C++ ? ;) > short events = (type & Read ? EV_READ : 0) > | (type & Write ? EV_WRITE : 0) > | EV_PERSIST; > @@ -85,9 +85,9 @@ void EventLoop::addFdEvent(int fd, EventType type, > } > > void EventLoop::addTimerEvent(const std::chrono::microseconds period, > - const std::function<void()> &callback) > + std::function<void()> &&callback) > { > - std::unique_ptr<Event> event = std::make_unique<Event>(callback); > + std::unique_ptr<Event> event = std::make_unique<Event>(std::move(callback)); > event->event_ = event_new(base_, -1, EV_PERSIST, &EventLoop::Event::dispatch, > event.get()); > if (!event->event_) { > @@ -131,8 +131,8 @@ void EventLoop::dispatchCall() > call(); > } > > -EventLoop::Event::Event(const std::function<void()> &callback) > - : callback_(callback), event_(nullptr) > +EventLoop::Event::Event(std::function<void()> &&callback) > + : callback_(std::move(callback)), event_(nullptr) > { > } > > diff --git a/src/apps/common/event_loop.h b/src/apps/common/event_loop.h > index ef129b9aa..d7d012c76 100644 > --- a/src/apps/common/event_loop.h > +++ b/src/apps/common/event_loop.h > @@ -33,18 +33,18 @@ public: > int exec(); > void exit(int code = 0); > > - void callLater(const std::function<void()> &func); > + void callLater(std::function<void()> &&func); > > void addFdEvent(int fd, EventType type, > - const std::function<void()> &handler); > + std::function<void()> &&handler); > > using duration = std::chrono::steady_clock::duration; > void addTimerEvent(const std::chrono::microseconds period, > - const std::function<void()> &handler); > + std::function<void()> &&handler); > > private: > struct Event { > - Event(const std::function<void()> &callback); > + Event(std::function<void()> &&callback); > ~Event(); > > static void dispatch(int fd, short events, void *arg); > -- > 2.48.1 > >
diff --git a/src/apps/common/event_loop.cpp b/src/apps/common/event_loop.cpp index f7f9afa0c..bc8cf17ab 100644 --- a/src/apps/common/event_loop.cpp +++ b/src/apps/common/event_loop.cpp @@ -50,20 +50,20 @@ void EventLoop::exit(int code) event_base_loopbreak(base_); } -void EventLoop::callLater(const std::function<void()> &func) +void EventLoop::callLater(std::function<void()> &&func) { { std::unique_lock<std::mutex> locker(lock_); - calls_.push_back(func); + calls_.push_back(std::move(func)); } event_base_once(base_, -1, EV_TIMEOUT, dispatchCallback, this, nullptr); } void EventLoop::addFdEvent(int fd, EventType type, - const std::function<void()> &callback) + std::function<void()> &&callback) { - std::unique_ptr<Event> event = std::make_unique<Event>(callback); + std::unique_ptr<Event> event = std::make_unique<Event>(std::move(callback)); short events = (type & Read ? EV_READ : 0) | (type & Write ? EV_WRITE : 0) | EV_PERSIST; @@ -85,9 +85,9 @@ void EventLoop::addFdEvent(int fd, EventType type, } void EventLoop::addTimerEvent(const std::chrono::microseconds period, - const std::function<void()> &callback) + std::function<void()> &&callback) { - std::unique_ptr<Event> event = std::make_unique<Event>(callback); + std::unique_ptr<Event> event = std::make_unique<Event>(std::move(callback)); event->event_ = event_new(base_, -1, EV_PERSIST, &EventLoop::Event::dispatch, event.get()); if (!event->event_) { @@ -131,8 +131,8 @@ void EventLoop::dispatchCall() call(); } -EventLoop::Event::Event(const std::function<void()> &callback) - : callback_(callback), event_(nullptr) +EventLoop::Event::Event(std::function<void()> &&callback) + : callback_(std::move(callback)), event_(nullptr) { } diff --git a/src/apps/common/event_loop.h b/src/apps/common/event_loop.h index ef129b9aa..d7d012c76 100644 --- a/src/apps/common/event_loop.h +++ b/src/apps/common/event_loop.h @@ -33,18 +33,18 @@ public: int exec(); void exit(int code = 0); - void callLater(const std::function<void()> &func); + void callLater(std::function<void()> &&func); void addFdEvent(int fd, EventType type, - const std::function<void()> &handler); + std::function<void()> &&handler); using duration = std::chrono::steady_clock::duration; void addTimerEvent(const std::chrono::microseconds period, - const std::function<void()> &handler); + std::function<void()> &&handler); private: struct Event { - Event(const std::function<void()> &callback); + Event(std::function<void()> &&callback); ~Event(); static void dispatch(int fd, short events, void *arg);