Skip to content

Commit

Permalink
less copies
Browse files Browse the repository at this point in the history
  • Loading branch information
thorstink committed Jan 4, 2025
1 parent a324bd2 commit e5b678b
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 17 deletions.
23 changes: 16 additions & 7 deletions examples/flight/transition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions symmetri/include/symmetri/callback.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ class Callback {
*/
Callback(Transition callback)
: self_(std::make_shared<model<Transition>>(std::move(callback))) {}
Callback(const Callback &o) = delete;
Callback &operator=(Callback &&other) = default;
Callback &operator=(const Callback &other) = delete;

friend Token fire(const Callback &callback) {
return callback.self_->fire_();
Expand Down
2 changes: 1 addition & 1 deletion symmetri/include/symmetri/symmetri.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions symmetri/petri.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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<SmallVectorInput>, std::vector<SmallVectorInput>>
Expand Down Expand Up @@ -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));
});
}
Expand Down
5 changes: 2 additions & 3 deletions symmetri/petri.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,10 @@ struct Petri {
*/
std::vector<Callback> 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
Expand Down
4 changes: 2 additions & 2 deletions symmetri/symmetri.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ std::function<void()> 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>(callback));
}
}

Expand Down
2 changes: 1 addition & 1 deletion symmetri/tests/callback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Callback> p;
p.push_back(f);
p.emplace_back(std::move(f));
resume(p.back());
}

Expand Down

0 comments on commit e5b678b

Please sign in to comment.