This commit is contained in:
mutouyun 2019-04-07 17:01:51 +08:00
parent e221780978
commit fe689c9208
5 changed files with 59 additions and 35 deletions

View File

@ -28,8 +28,7 @@ namespace {
using namespace ipc;
using msg_id_t = std::size_t;
template <std::size_t DataSize,
std::size_t AlignSize = (ipc::detail::min)(DataSize, alignof(std::max_align_t))>
template <std::size_t DataSize, std::size_t AlignSize>
struct msg_t;
template <std::size_t AlignSize>
@ -111,20 +110,31 @@ bool wait_for(W& waiter, F&& pred, std::size_t tm) {
return true;
}
template <typename Policy>
struct detail_impl {
template <typename Policy,
std::size_t DataSize,
std::size_t AlignSize = (ipc::detail::min)(DataSize, alignof(std::max_align_t))>
struct queue_generator {
using queue_t = ipc::queue<msg_t<data_length>, Policy>;
using queue_t = ipc::queue<msg_t<DataSize, AlignSize>, Policy>;
struct conn_info_t : conn_info_head {
struct conn_info_t : conn_info_head {
queue_t que_;
conn_info_t(char const * name)
: conn_info_head(name)
, que_((std::string{ "__QU_CONN__" } + name).c_str()) {
, que_(("__QU_CONN__" +
std::to_string(DataSize ) + "__" +
std::to_string(AlignSize) + "__" + name).c_str()) {
}
};
};
template <typename Policy>
struct detail_impl {
using queue_t = typename queue_generator<Policy, data_length>::queue_t;
using conn_info_t = typename queue_generator<Policy, data_length>::conn_info_t;
constexpr static void* head_of(queue_t* que) {
return static_cast<void*>(que->elems());
}

View File

@ -50,8 +50,7 @@ inline std::size_t classify(std::size_t size) {
template <template <std::size_t> class Fixed, typename F>
decltype(auto) choose(std::size_t size, F&& f) {
return ipc::detail::static_switch(classify(size), std::make_index_sequence<32> {
}, [&f](auto index) {
return ipc::detail::static_switch<32>(classify(size), [&f](auto index) {
return f(Fixed<(decltype(index)::value + 1) * base_size>::pool());
}, [&f] {
return f(static_alloc{});
@ -62,7 +61,7 @@ template <template <std::size_t> class Fixed>
class pool_alloc {
public:
static void clear() {
ipc::detail::static_for(std::make_index_sequence<32> {}, [](auto index) {
ipc::detail::static_for<32>([](auto index) {
Fixed<(decltype(index)::value + 1) * base_size>::pool().clear();
});
}

View File

@ -7,6 +7,7 @@
#include <tuple>
#include <atomic>
#include <algorithm>
#include <utility>
#include "def.h"
#include "export.h"
@ -37,7 +38,7 @@
# define IPC_STBIND_(A, B, ...) auto [A, B] = __VA_ARGS__
#else /*__cplusplus < 201703L*/
# define IPC_STBIND_(A, B, ...) \
auto tp = __VA_ARGS__ \
auto tp___ = __VA_ARGS__ \
auto A = std::get<0>(tp); \
auto B = std::get<1>(tp)
#endif/*__cplusplus < 201703L*/
@ -98,13 +99,18 @@ constexpr const T& (min)(const T& a, const T& b) {
template <typename F, typename D>
constexpr decltype(auto) static_switch(std::size_t /*i*/, std::index_sequence<>, F&& /*f*/, D&& def) {
return def();
return std::forward<D>(def)();
}
template <typename F, typename D, std::size_t N, std::size_t...I>
constexpr decltype(auto) static_switch(std::size_t i, std::index_sequence<N, I...>, F&& f, D&& def) {
return (i == N) ? f(std::integral_constant<size_t, N>{}) :
static_switch(i, std::index_sequence<I...>{}, f, def);
return (i == N) ? std::forward<F>(f)(std::integral_constant<size_t, N>{}) :
static_switch(i, std::index_sequence<I...>{}, std::forward<F>(f), std::forward<D>(def));
}
template <std::size_t N, typename F, typename D>
constexpr decltype(auto) static_switch(std::size_t i, F&& f, D&& def) {
return static_switch(i, std::make_index_sequence<N>{}, std::forward<F>(f), std::forward<D>(def));
}
template <typename F, std::size_t...I>
@ -113,7 +119,16 @@ constexpr void static_for(std::index_sequence<I...>, F&& f) {
#else /*__cplusplus < 201703L*/
inline void static_for(std::index_sequence<I...>, F&& f) {
#endif/*__cplusplus < 201703L*/
IPC_UNUSED_ auto expand = { (f(std::integral_constant<size_t, I>{}), 0)... };
IPC_UNUSED_ auto expand = { (std::forward<F>(f)(std::integral_constant<size_t, I>{}), 0)... };
}
template <std::size_t N, typename F>
#if __cplusplus >= 201703L
constexpr void static_for(F&& f) {
#else /*__cplusplus < 201703L*/
inline void static_for(F&& f) {
#endif/*__cplusplus < 201703L*/
static_for(std::make_index_sequence<N>{}, std::forward<F>(f));
}
} // namespace detail

View File

@ -341,11 +341,11 @@ void Unit::test_prod_cons_performance() {
sizeof(msg_t),
pc_t<ipc::relat::single, ipc::relat::multi, ipc::trans::unicast>
> el_arr_smu;
ipc::detail::static_for(std::make_index_sequence<8>{}, [&el_arr_smu](auto index) {
ipc::detail::static_for<8>([&el_arr_smu](auto index) {
benchmark_prod_cons<1, decltype(index)::value + 1, LoopCount, void>(&el_arr_smu);
});
ipc::detail::static_for(std::make_index_sequence<8>{}, [](auto index) {
ipc::detail::static_for<8>([](auto index) {
test_prod_cons<1, decltype(index)::value + 1, false>();
});
test_prod_cons<1, 8>(); // test & verify
@ -354,13 +354,13 @@ void Unit::test_prod_cons_performance() {
sizeof(msg_t),
pc_t<ipc::relat::multi, ipc::relat::multi, ipc::trans::unicast>
> el_arr_mmu;
ipc::detail::static_for(std::make_index_sequence<8>{}, [&el_arr_mmu](auto index) {
ipc::detail::static_for<8>([&el_arr_mmu](auto index) {
benchmark_prod_cons<1, decltype(index)::value + 1, LoopCount, void>(&el_arr_mmu);
});
ipc::detail::static_for(std::make_index_sequence<8>{}, [&el_arr_mmu](auto index) {
ipc::detail::static_for<8>([&el_arr_mmu](auto index) {
benchmark_prod_cons<decltype(index)::value + 1, 1, LoopCount, void>(&el_arr_mmu);
});
ipc::detail::static_for(std::make_index_sequence<8>{}, [&el_arr_mmu](auto index) {
ipc::detail::static_for<8>([&el_arr_mmu](auto index) {
benchmark_prod_cons<decltype(index)::value + 1, decltype(index)::value + 1, LoopCount, void>(&el_arr_mmu);
});
@ -368,13 +368,13 @@ void Unit::test_prod_cons_performance() {
sizeof(msg_t),
pc_t<ipc::relat::multi, ipc::relat::multi, ipc::trans::broadcast>
> el_arr_mmb;
ipc::detail::static_for(std::make_index_sequence<8>{}, [&el_arr_mmb](auto index) {
ipc::detail::static_for<8>([&el_arr_mmb](auto index) {
benchmark_prod_cons<1, decltype(index)::value + 1, LoopCount, void>(&el_arr_mmb);
});
ipc::detail::static_for(std::make_index_sequence<8>{}, [&el_arr_mmb](auto index) {
ipc::detail::static_for<8>([&el_arr_mmb](auto index) {
benchmark_prod_cons<decltype(index)::value + 1, 1, LoopCount, void>(&el_arr_mmb);
});
ipc::detail::static_for(std::make_index_sequence<8>{}, [&el_arr_mmb](auto index) {
ipc::detail::static_for<8>([&el_arr_mmb](auto index) {
benchmark_prod_cons<decltype(index)::value + 1, decltype(index)::value + 1, LoopCount, void>(&el_arr_mmb);
});
}
@ -392,7 +392,7 @@ void Unit::test_queue() {
QCOMPARE(msg, (msg_t {}));
QVERIFY(sizeof(decltype(queue)::elems_t) <= sizeof(*cq__));
ipc::detail::static_for(std::make_index_sequence<16>{}, [](auto index) {
ipc::detail::static_for<16>([](auto index) {
benchmark_prod_cons<1, decltype(index)::value + 1, LoopCount>((queue_t*)nullptr);
});
}

View File

@ -393,7 +393,7 @@ void Unit::test_route_rtt() {
void Unit::test_route_performance() {
//return;
ipc::detail::static_for(std::make_index_sequence<16>{}, [](auto index) {
ipc::detail::static_for<16>([](auto index) {
test_prod_cons<ipc::route, 1, decltype(index)::value + 1, false>();
});
test_prod_cons<ipc::route, 1, 8>(); // test & verify
@ -467,13 +467,13 @@ void Unit::test_channel_rtt() {
}
void Unit::test_channel_performance() {
ipc::detail::static_for(std::make_index_sequence<16>{}, [](auto index) {
ipc::detail::static_for<16>([](auto index) {
test_prod_cons<ipc::channel, 1, decltype(index)::value + 1, false>();
});
ipc::detail::static_for(std::make_index_sequence<16>{}, [](auto index) {
ipc::detail::static_for<16>([](auto index) {
test_prod_cons<ipc::channel, decltype(index)::value + 1, 1, false>();
});
ipc::detail::static_for(std::make_index_sequence<16>{}, [](auto index) {
ipc::detail::static_for<16>([](auto index) {
test_prod_cons<ipc::channel, decltype(index)::value + 1,
decltype(index)::value + 1, false>();
});