Apply outer and inner size improvements for the callable wrapper

* This will decrease the needed allocations heavily,
  since we don't have to allocate twice if a continuation is type erased
  since the outer type erasure may contain the inner one with zero costs.
This commit is contained in:
Denis Blank 2018-01-25 05:00:19 +01:00
parent 5d6b6116bf
commit 6e404b6eaa
2 changed files with 39 additions and 13 deletions

View File

@ -31,9 +31,11 @@
#ifndef CONTINUABLE_TRAIT_HPP_INCLUDED__
#define CONTINUABLE_TRAIT_HPP_INCLUDED__
#include <cstdint>
#include <continuable/continuable-api.hpp>
#include <continuable/continuable-base.hpp>
#include <continuable/continuable-promise-base.hpp>
#include <continuable/continuable-api.hpp>
#include <continuable/detail/hints.hpp>
#include <continuable/detail/types.hpp>
@ -49,18 +51,23 @@ namespace cti {
/// continuation data.
///
/// \tparam Args The current signature of the continuable.
template <template <typename...> class CallbackWrapper,
template <typename...> class ContinuationWrapper, typename... Args>
struct continuable_trait {
template <template <std::size_t, typename...> class CallbackWrapper,
template <std::size_t, typename...> class ContinuationWrapper,
typename... Args>
class continuable_trait {
using callback = CallbackWrapper<0U, void(Args...)&&,
void(detail::types::dispatch_error_tag,
detail::types::error_type) &&>;
public:
/// The promise type which is used to resolve continuations
using promise = promise_base<
CallbackWrapper<void(Args...)&&, void(detail::types::dispatch_error_tag,
detail::types::error_type) &&>,
detail::hints::signature_hint_tag<Args...>>;
using promise =
promise_base<callback, detail::hints::signature_hint_tag<Args...>>;
/// The continuable type for the given parameters.
using continuable =
continuable_base<ContinuationWrapper<void(promise)>,
continuable_base<ContinuationWrapper<sizeof(callback), void(promise)>,
detail::hints::signature_hint_tag<Args...>>;
};
} // namespace cti

View File

@ -31,6 +31,8 @@
#ifndef CONTINUABLE_HPP_INCLUDED__
#define CONTINUABLE_HPP_INCLUDED__
#include <cstdint>
#include <function2/function2.hpp>
#include <continuable/continuable-api.hpp>
@ -39,17 +41,34 @@
namespace cti {
// clang-format off
namespace detail {
/// A function which isn't size adjusted and copyable
template<std::size_t Size, typename... Args>
using function_adapter = fu2::function<Args...>;
/// A function which isn't size adjusted and move only
template<std::size_t, typename... Args>
using unique_function_adapter = fu2::unique_function<Args...>;
/// A function which is size adjusted and copyable
template<std::size_t Size, typename... Args>
using function_adjustable = fu2::function_base<true, true, Size,
true, false, Args...>;
/// A function which is size adjusted and move only
template<std::size_t Size, typename... Args>
using unique_function_adjustable = fu2::function_base<true, false, Size,
true, false, Args...>;
/// We adjust the internal capacity of the outer function wrapper so
/// we don't have to allocate twice when using `continuable<...>`.
template<typename... Args>
using trait_of = continuable_trait<
fu2::unique_function,
fu2::function,
unique_function_adapter,
function_adjustable,
Args...
>;
template<typename... Args>
using unique_trait_of = continuable_trait<
fu2::unique_function,
fu2::unique_function,
unique_function_adapter,
unique_function_adjustable,
Args...
>;
} // namespace detail