From 7595907e1b1304579b8ce88bc61da7caaeca314b Mon Sep 17 00:00:00 2001 From: Thomas Horstink Date: Sat, 4 Jan 2025 10:05:43 +0100 Subject: [PATCH] less copies --- examples/flight/transition.hpp | 23 ++++++++++++++++------- symmetri/include/symmetri/symmetri.h | 2 +- symmetri/petri.cpp | 6 +++--- symmetri/petri.h | 5 ++--- symmetri/symmetri.cpp | 4 ++-- symmetri/tests/callback.cpp | 2 +- 6 files changed, 25 insertions(+), 17 deletions(-) diff --git a/examples/flight/transition.hpp b/examples/flight/transition.hpp index 375a991..5e5fecc 100644 --- a/examples/flight/transition.hpp +++ b/examples/flight/transition.hpp @@ -22,13 +22,22 @@ class Foo { public: Foo(std::string m, unsigned count = 5, int s = 1) - : name(m), count(count), interval(s), cancel_(false), is_paused_(false) {} - Foo(const Foo &o) - : name(o.name), - count(o.count), - interval(o.interval), - cancel_(false), - is_paused_(false) {} + : name(m), count(count), interval(s), cancel_(false), is_paused_(false) { + printf("ctor %s\n", m.c_str()); + } + Foo(Foo&& f) + : name(std::move(f.name)), + count(std::move(f.count)), + interval(std::move(f.interval)), + cancel_(f.cancel_.load()), + is_paused_(f.is_paused_.load()) { + printf("move %s\n", f.name.c_str()); + } + ~Foo() { printf("deconstructor %s\n", name.c_str()); } + + Foo(const Foo& o) = delete; + Foo& operator=(Foo&& other) = delete; + Foo& operator=(const Foo& other) = delete; bool fire() const { cancel_.store(false); diff --git a/symmetri/include/symmetri/symmetri.h b/symmetri/include/symmetri/symmetri.h index daaa1f9..20f29b4 100644 --- a/symmetri/include/symmetri/symmetri.h +++ b/symmetri/include/symmetri/symmetri.h @@ -77,7 +77,7 @@ class PetriNet final { * @param callback the callback */ void registerCallback(const std::string &transition, - const Callback &callback) const noexcept; + Callback &&callback) const noexcept; /** * @brief Get the Marking object. This function is thread-safe and be called diff --git a/symmetri/petri.cpp b/symmetri/petri.cpp index b170fee..577a8a6 100644 --- a/symmetri/petri.cpp +++ b/symmetri/petri.cpp @@ -12,7 +12,7 @@ convert(const Net &_net) { store.reserve(transition_count); for (const auto &[t, io] : _net) { transitions.push_back(t); - store.push_back(DirectMutation{}); + store.emplace_back(DirectMutation{}); for (const auto &p : io.first) { places.push_back(p.first); } @@ -24,7 +24,7 @@ convert(const Net &_net) { auto last = std::unique(places.begin(), places.end()); places.erase(last, places.end()); } - return {transitions, places, store}; + return {std::move(transitions), std::move(places), std::move(store)}; } std::tuple, std::vector> @@ -126,7 +126,7 @@ void Petri::fireAsynchronous(const size_t t) { scheduled_callbacks.push_back(t); log.push_back({t, Scheduled, Clock::now()}); - pool->push([=] { + pool->push([t, &task, this] { reducer_queue->enqueue(scheduleCallback(t, task, reducer_queue)); }); } diff --git a/symmetri/petri.h b/symmetri/petri.h index 37a114e..53bb4e6 100644 --- a/symmetri/petri.h +++ b/symmetri/petri.h @@ -240,11 +240,10 @@ struct Petri { */ std::vector store; - void registerCallback(const std::string &t, - const Callback &callback) noexcept { + void registerCallback(const std::string &t, Callback &&callback) noexcept { if (std::find(transition.begin(), transition.end(), t) != transition.end()) { - store[toIndex(transition, t)] = callback; + store[toIndex(transition, t)] = std::move(callback); } } } net; ///< Is a data-oriented design of a Petri net diff --git a/symmetri/symmetri.cpp b/symmetri/symmetri.cpp index 47aef33..2d3e48c 100644 --- a/symmetri/symmetri.cpp +++ b/symmetri/symmetri.cpp @@ -55,9 +55,9 @@ std::function PetriNet::getInputTransitionHandle( } void PetriNet::registerCallback(const std::string &transition, - const Callback &callback) const noexcept { + Callback &&callback) const noexcept { if (!impl->thread_id_.load().has_value()) { - impl->net.registerCallback(transition, callback); + impl->net.registerCallback(transition, std::forward(callback)); } } diff --git a/symmetri/tests/callback.cpp b/symmetri/tests/callback.cpp index f4f630e..fd43a41 100644 --- a/symmetri/tests/callback.cpp +++ b/symmetri/tests/callback.cpp @@ -44,7 +44,7 @@ TEST_CASE("Constructing is just as you expect") { TEST_CASE("Creating and and inserting is ok") { Callback f(Foo("hi")); std::vector p; - p.push_back(f); + p.emplace_back(std::move(f)); resume(p.back()); }