mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-06 16:56:45 +08:00
optimize
This commit is contained in:
parent
e221780978
commit
fe689c9208
34
src/ipc.cpp
34
src/ipc.cpp
@ -28,8 +28,7 @@ namespace {
|
|||||||
using namespace ipc;
|
using namespace ipc;
|
||||||
using msg_id_t = std::size_t;
|
using msg_id_t = std::size_t;
|
||||||
|
|
||||||
template <std::size_t DataSize,
|
template <std::size_t DataSize, std::size_t AlignSize>
|
||||||
std::size_t AlignSize = (ipc::detail::min)(DataSize, alignof(std::max_align_t))>
|
|
||||||
struct msg_t;
|
struct msg_t;
|
||||||
|
|
||||||
template <std::size_t AlignSize>
|
template <std::size_t AlignSize>
|
||||||
@ -111,19 +110,30 @@ bool wait_for(W& waiter, F&& pred, std::size_t tm) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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<DataSize, AlignSize>, Policy>;
|
||||||
|
|
||||||
|
struct conn_info_t : conn_info_head {
|
||||||
|
queue_t que_;
|
||||||
|
|
||||||
|
conn_info_t(char const * name)
|
||||||
|
: conn_info_head(name)
|
||||||
|
, que_(("__QU_CONN__" +
|
||||||
|
std::to_string(DataSize ) + "__" +
|
||||||
|
std::to_string(AlignSize) + "__" + name).c_str()) {
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
template <typename Policy>
|
template <typename Policy>
|
||||||
struct detail_impl {
|
struct detail_impl {
|
||||||
|
|
||||||
using queue_t = ipc::queue<msg_t<data_length>, Policy>;
|
using queue_t = typename queue_generator<Policy, data_length>::queue_t;
|
||||||
|
using conn_info_t = typename queue_generator<Policy, data_length>::conn_info_t;
|
||||||
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()) {
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
constexpr static void* head_of(queue_t* que) {
|
constexpr static void* head_of(queue_t* que) {
|
||||||
return static_cast<void*>(que->elems());
|
return static_cast<void*>(que->elems());
|
||||||
|
|||||||
@ -50,8 +50,7 @@ inline std::size_t classify(std::size_t size) {
|
|||||||
|
|
||||||
template <template <std::size_t> class Fixed, typename F>
|
template <template <std::size_t> class Fixed, typename F>
|
||||||
decltype(auto) choose(std::size_t size, F&& f) {
|
decltype(auto) choose(std::size_t size, F&& f) {
|
||||||
return ipc::detail::static_switch(classify(size), std::make_index_sequence<32> {
|
return ipc::detail::static_switch<32>(classify(size), [&f](auto index) {
|
||||||
}, [&f](auto index) {
|
|
||||||
return f(Fixed<(decltype(index)::value + 1) * base_size>::pool());
|
return f(Fixed<(decltype(index)::value + 1) * base_size>::pool());
|
||||||
}, [&f] {
|
}, [&f] {
|
||||||
return f(static_alloc{});
|
return f(static_alloc{});
|
||||||
@ -62,7 +61,7 @@ template <template <std::size_t> class Fixed>
|
|||||||
class pool_alloc {
|
class pool_alloc {
|
||||||
public:
|
public:
|
||||||
static void clear() {
|
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();
|
Fixed<(decltype(index)::value + 1) * base_size>::pool().clear();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,6 +7,7 @@
|
|||||||
#include <tuple>
|
#include <tuple>
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
#include "def.h"
|
#include "def.h"
|
||||||
#include "export.h"
|
#include "export.h"
|
||||||
@ -37,9 +38,9 @@
|
|||||||
# define IPC_STBIND_(A, B, ...) auto [A, B] = __VA_ARGS__
|
# define IPC_STBIND_(A, B, ...) auto [A, B] = __VA_ARGS__
|
||||||
#else /*__cplusplus < 201703L*/
|
#else /*__cplusplus < 201703L*/
|
||||||
# define IPC_STBIND_(A, B, ...) \
|
# define IPC_STBIND_(A, B, ...) \
|
||||||
auto tp = __VA_ARGS__ \
|
auto tp___ = __VA_ARGS__ \
|
||||||
auto A = std::get<0>(tp); \
|
auto A = std::get<0>(tp); \
|
||||||
auto B = std::get<1>(tp)
|
auto B = std::get<1>(tp)
|
||||||
#endif/*__cplusplus < 201703L*/
|
#endif/*__cplusplus < 201703L*/
|
||||||
|
|
||||||
#if __cplusplus >= 201703L
|
#if __cplusplus >= 201703L
|
||||||
@ -98,13 +99,18 @@ constexpr const T& (min)(const T& a, const T& b) {
|
|||||||
|
|
||||||
template <typename F, typename D>
|
template <typename F, typename D>
|
||||||
constexpr decltype(auto) static_switch(std::size_t /*i*/, std::index_sequence<>, F&& /*f*/, D&& def) {
|
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>
|
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) {
|
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>{}) :
|
return (i == N) ? std::forward<F>(f)(std::integral_constant<size_t, N>{}) :
|
||||||
static_switch(i, std::index_sequence<I...>{}, f, def);
|
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>
|
template <typename F, std::size_t...I>
|
||||||
@ -113,7 +119,16 @@ constexpr void static_for(std::index_sequence<I...>, F&& f) {
|
|||||||
#else /*__cplusplus < 201703L*/
|
#else /*__cplusplus < 201703L*/
|
||||||
inline void static_for(std::index_sequence<I...>, F&& f) {
|
inline void static_for(std::index_sequence<I...>, F&& f) {
|
||||||
#endif/*__cplusplus < 201703L*/
|
#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
|
} // namespace detail
|
||||||
|
|||||||
@ -341,11 +341,11 @@ void Unit::test_prod_cons_performance() {
|
|||||||
sizeof(msg_t),
|
sizeof(msg_t),
|
||||||
pc_t<ipc::relat::single, ipc::relat::multi, ipc::trans::unicast>
|
pc_t<ipc::relat::single, ipc::relat::multi, ipc::trans::unicast>
|
||||||
> el_arr_smu;
|
> 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);
|
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, decltype(index)::value + 1, false>();
|
||||||
});
|
});
|
||||||
test_prod_cons<1, 8>(); // test & verify
|
test_prod_cons<1, 8>(); // test & verify
|
||||||
@ -354,13 +354,13 @@ void Unit::test_prod_cons_performance() {
|
|||||||
sizeof(msg_t),
|
sizeof(msg_t),
|
||||||
pc_t<ipc::relat::multi, ipc::relat::multi, ipc::trans::unicast>
|
pc_t<ipc::relat::multi, ipc::relat::multi, ipc::trans::unicast>
|
||||||
> el_arr_mmu;
|
> 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);
|
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);
|
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);
|
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),
|
sizeof(msg_t),
|
||||||
pc_t<ipc::relat::multi, ipc::relat::multi, ipc::trans::broadcast>
|
pc_t<ipc::relat::multi, ipc::relat::multi, ipc::trans::broadcast>
|
||||||
> el_arr_mmb;
|
> 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);
|
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);
|
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);
|
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 {}));
|
QCOMPARE(msg, (msg_t {}));
|
||||||
QVERIFY(sizeof(decltype(queue)::elems_t) <= sizeof(*cq__));
|
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);
|
benchmark_prod_cons<1, decltype(index)::value + 1, LoopCount>((queue_t*)nullptr);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@ -393,7 +393,7 @@ void Unit::test_route_rtt() {
|
|||||||
|
|
||||||
void Unit::test_route_performance() {
|
void Unit::test_route_performance() {
|
||||||
//return;
|
//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, decltype(index)::value + 1, false>();
|
||||||
});
|
});
|
||||||
test_prod_cons<ipc::route, 1, 8>(); // test & verify
|
test_prod_cons<ipc::route, 1, 8>(); // test & verify
|
||||||
@ -467,13 +467,13 @@ void Unit::test_channel_rtt() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Unit::test_channel_performance() {
|
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>();
|
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>();
|
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,
|
test_prod_cons<ipc::channel, decltype(index)::value + 1,
|
||||||
decltype(index)::value + 1, false>();
|
decltype(index)::value + 1, false>();
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user