Skip to content

Commit

Permalink
fix: Use unique ptr with default deleter to reduce compile time (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzenker committed Apr 24, 2021
1 parent 379e712 commit 3169789
Show file tree
Hide file tree
Showing 8 changed files with 641 additions and 148 deletions.
1 change: 1 addition & 0 deletions benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ include(../cmake/test-flags.cmake)
set(
components_benchmarks
fill_dispatch_table_benchmark
fill_unexpected_event_handler_tables_benchmark
flatten_transition_table_benchmark
getCombinedStateTypeids_benchmark
collect_parent_state_typeids_benchmark
Expand Down
2 changes: 1 addition & 1 deletion benchmark/complex/hsm.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "../benchmark.hpp"
#include "ComplexStateMachine.h"
#include "benchmark.hpp"

using namespace hsm;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <iostream>

#include "ComplexStateMachine.h"

using namespace hsm;

int main()
{
auto rootState = state<ComplexStateMachine>;
auto statesMap = make_states_map(rootState);
auto unexpectedEventHandlerTables = make_unexpected_event_handler_tables(rootState);
auto unexpectedEventHandler = [](auto&...) {};
auto optionalDependency = boost::hana::make_tuple();

fill_unexpected_event_handler_tables(
state<ComplexStateMachine>,
statesMap,
unexpectedEventHandlerTables,
unexpectedEventHandler,
optionalDependency);

return 0;
}
20 changes: 11 additions & 9 deletions include/hsm/details/dispatch_table.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "hsm/details/idx.h"
#include "hsm/details/utils/dunique_ptr.h"

#include <boost/any.hpp>

Expand Down Expand Up @@ -140,14 +141,15 @@ constexpr auto make_transition(
{
using Event = typename decltype(eventTypeid)::type;

return std::make_unique<DispatchTableEntry<
transition.internal(),
Action,
Guard,
Source,
Target,
Event,
decltype(optionalDependency)>>(action, guard, source, target, optionalDependency);
return hsm::details::utils::dunique_ptr<IDispatchTableEntry<Event>>(
new DispatchTableEntry<
transition.internal(),
Action,
Guard,
Source,
Target,
Event,
decltype(optionalDependency)>(action, guard, source, target, optionalDependency));
}

template <class Event> struct NextState {
Expand All @@ -156,6 +158,6 @@ template <class Event> struct NextState {
bool defer {};
bool valid = false;
bool internal = false;
std::unique_ptr<IDispatchTableEntry<Event>> transition;
hsm::details::utils::dunique_ptr<IDispatchTableEntry<Event>> transition;
};
}
8 changes: 5 additions & 3 deletions include/hsm/details/fill_unexpected_event_handler_tables.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "hsm/details/flatten_transition_table.h"
#include "hsm/details/make_unexpected_event_handler_tables.h"
#include "hsm/details/to_map.h"
#include "hsm/details/utils/dunique_ptr.h"

#include <boost/hana/pair.hpp>
#include <boost/hana/transform.hpp>
Expand All @@ -26,9 +27,10 @@ constexpr auto make_unexpected_event_handler(
OptionalDependency optionalDependency)
{
using Event = typename decltype(eventTypeid)::type;
return std::make_unique<
UnexpectedEventHandler<Handler, CurrentStatePtr, Event, OptionalDependency>>(
handler, currentStatePtr, optionalDependency);

return hsm::details::utils::dunique_ptr<IUnexpectedEventHandler<Event>>(
new UnexpectedEventHandler<Handler, CurrentStatePtr, Event, OptionalDependency>(
handler, currentStatePtr, optionalDependency));
}

template <
Expand Down
5 changes: 4 additions & 1 deletion include/hsm/details/make_unexpected_event_handler_tables.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "hsm/details/fill_dispatch_table.h"
#include "hsm/details/flatten_transition_table.h"
#include "hsm/details/to_map.h"
#include "hsm/details/utils/dunique_ptr.h"

#include <boost/hana/pair.hpp>
#include <boost/hana/transform.hpp>
Expand Down Expand Up @@ -55,7 +56,9 @@ template <class RootState> constexpr auto make_unexpected_event_handler_tables(R
using Event = typename decltype(eventTypeid)::type;
return bh::make_pair(
eventTypeid,
std::array<std::unique_ptr<IUnexpectedEventHandler<Event>>, states>());
std::array<
hsm::details::utils::dunique_ptr<IUnexpectedEventHandler<Event>>,
states>());
}(nStates(rootState) * nParentStates(rootState));
});
}(collect_event_typeids_recursive(rootState)));
Expand Down
102 changes: 102 additions & 0 deletions include/hsm/details/utils/dunique_ptr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@

#pragma once

#include <memory>

namespace hsm::details::utils {

template <typename T, typename Delete = std::default_delete<T>> struct dunique_ptr;

template <typename T> struct dunique_ptr<T, std::default_delete<T>> {
dunique_ptr()
: ptr(nullptr)
{
}
explicit dunique_ptr(T* ptr)
: ptr(ptr)
{
}
dunique_ptr(const dunique_ptr&) = delete;
auto operator=(const dunique_ptr&) -> dunique_ptr& = delete;
dunique_ptr(dunique_ptr&& other) noexcept
: ptr(other.ptr)
{
other.ptr = nullptr;
}
auto operator=(dunique_ptr&& other) noexcept -> dunique_ptr&
{
swap(other);
return *this;
}
~dunique_ptr()
{
reset();
}
void reset(T* value = nullptr)
{
T* to_delete = ptr;
ptr = value;
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
delete to_delete;
}
auto release() -> T*
{
T* result = ptr;
ptr = nullptr;
return result;
}
explicit operator bool() const
{
return bool(ptr);
}
[[nodiscard]] auto get() const -> T*
{
return ptr;
}
auto operator*() const -> T&
{
return *ptr;
}
auto operator->() const -> T*
{
return ptr;
}
void swap(dunique_ptr& other)
{
std::swap(ptr, other.ptr);
}

auto operator==(const dunique_ptr& other) const -> bool
{
return ptr == other.ptr;
}
auto operator!=(const dunique_ptr& other) const -> bool
{
return !(*this == other);
}
auto operator<(const dunique_ptr& other) const -> bool
{
return ptr < other.ptr;
}
auto operator<=(const dunique_ptr& other) const -> bool
{
return !(other < *this);
}
auto operator>(const dunique_ptr& other) const -> bool
{
return other < *this;
}
auto operator>=(const dunique_ptr& other) const -> bool
{
return !(*this < other);
}

private:
T* ptr;
};

template <typename T, typename D> void swap(dunique_ptr<T, D>& lhs, dunique_ptr<T, D>& rhs)
{
lhs.swap(rhs);
}
}

0 comments on commit 3169789

Please sign in to comment.