mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-06 16:56:45 +08:00
optimize the memory allocator
This commit is contained in:
parent
659989fd31
commit
56484c0c8f
1
include/pool_alloc.h
Normal file → Executable file
1
include/pool_alloc.h
Normal file → Executable file
@ -11,7 +11,6 @@ namespace mem {
|
||||
|
||||
class IPC_EXPORT pool_alloc {
|
||||
public:
|
||||
static void clear();
|
||||
static void* alloc(std::size_t size);
|
||||
static void free (void* p, std::size_t size);
|
||||
};
|
||||
|
||||
BIN
performance.xlsx
BIN
performance.xlsx
Binary file not shown.
156
src/memory/alloc.h
Normal file → Executable file
156
src/memory/alloc.h
Normal file → Executable file
@ -12,13 +12,14 @@
|
||||
|
||||
#include "platform/detail.h"
|
||||
|
||||
#include "log.h"
|
||||
|
||||
namespace ipc {
|
||||
namespace mem {
|
||||
|
||||
class static_alloc {
|
||||
public:
|
||||
static void swap(static_alloc&) {}
|
||||
static void clear() {}
|
||||
|
||||
static void* alloc(std::size_t size) {
|
||||
return size ? std::malloc(size) : nullptr;
|
||||
@ -109,11 +110,6 @@ public:
|
||||
|
||||
~scope_alloc() { free_all(); }
|
||||
|
||||
template <typename A>
|
||||
void set_allocator(A && alc) {
|
||||
alloc_ = std::forward<A>(alc);
|
||||
}
|
||||
|
||||
void swap(scope_alloc& rhs) {
|
||||
alloc_.swap(rhs.alloc_);
|
||||
base_t::swap(rhs);
|
||||
@ -130,12 +126,6 @@ public:
|
||||
base_t::take(std::move(rhs));
|
||||
}
|
||||
|
||||
void clear() {
|
||||
free_all();
|
||||
tail_ = nullptr;
|
||||
alloc_.~alloc_policy();
|
||||
}
|
||||
|
||||
void* alloc(std::size_t size) {
|
||||
auto curr = static_cast<block_t*>(alloc_.alloc(size += aligned_block_size));
|
||||
curr->next_ = head_;
|
||||
@ -156,10 +146,12 @@ namespace detail {
|
||||
|
||||
class fixed_alloc_base {
|
||||
protected:
|
||||
std::size_t block_size_;
|
||||
std::size_t init_expand_;
|
||||
void * cursor_;
|
||||
|
||||
void init(std::size_t init_expand) {
|
||||
void init(std::size_t block_size, std::size_t init_expand) {
|
||||
block_size_ = block_size;
|
||||
init_expand_ = init_expand;
|
||||
cursor_ = nullptr;
|
||||
}
|
||||
@ -173,7 +165,12 @@ protected:
|
||||
}
|
||||
|
||||
public:
|
||||
void set_block_size(std::size_t block_size) {
|
||||
block_size_ = block_size;
|
||||
}
|
||||
|
||||
void swap(fixed_alloc_base& rhs) {
|
||||
std::swap(block_size_ , rhs.block_size_);
|
||||
std::swap(init_expand_, rhs.init_expand_);
|
||||
std::swap(cursor_ , rhs.cursor_);
|
||||
}
|
||||
@ -211,6 +208,63 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
template <typename AllocP, typename ExpandP>
|
||||
class fixed_alloc : public detail::fixed_alloc_base {
|
||||
public:
|
||||
using base_t = detail::fixed_alloc_base;
|
||||
using alloc_policy = AllocP;
|
||||
|
||||
private:
|
||||
alloc_policy alloc_;
|
||||
|
||||
void* try_expand() {
|
||||
if (empty()) {
|
||||
auto size = ExpandP::next(block_size_, init_expand_);
|
||||
auto p = node_p(cursor_ = alloc_.alloc(size));
|
||||
for (std::size_t i = 0; i < (size / block_size_) - 1; ++i)
|
||||
p = node_p((*p) = reinterpret_cast<byte_t*>(p) + block_size_);
|
||||
(*p) = nullptr;
|
||||
}
|
||||
return cursor_;
|
||||
}
|
||||
|
||||
public:
|
||||
explicit fixed_alloc(std::size_t block_size, std::size_t init_expand = 1) {
|
||||
init(block_size, init_expand);
|
||||
}
|
||||
|
||||
fixed_alloc(fixed_alloc && rhs) {
|
||||
init(0, 0);
|
||||
swap(rhs);
|
||||
}
|
||||
|
||||
fixed_alloc& operator=(fixed_alloc rhs) {
|
||||
swap(rhs);
|
||||
return (*this);
|
||||
}
|
||||
|
||||
void swap(fixed_alloc& rhs) {
|
||||
alloc_.swap(rhs.alloc_);
|
||||
base_t::swap(rhs);
|
||||
}
|
||||
|
||||
template <typename A = AllocP>
|
||||
auto take(fixed_alloc && rhs) -> ipc::require<detail::has_take<A>::value> {
|
||||
base_t::take(std::move(rhs));
|
||||
alloc_.take(std::move(rhs.alloc_));
|
||||
}
|
||||
|
||||
void* alloc() {
|
||||
void* p = try_expand();
|
||||
cursor_ = next(p);
|
||||
return p;
|
||||
}
|
||||
|
||||
void* alloc(std::size_t) {
|
||||
return alloc();
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
struct fixed_expand_policy {
|
||||
@ -227,9 +281,8 @@ struct fixed_expand_policy {
|
||||
return e * 2;
|
||||
}
|
||||
|
||||
template <std::size_t BlockSize>
|
||||
static std::size_t next(std::size_t & e) {
|
||||
auto n = ipc::detail::max<std::size_t>(BlockSize, base_size) * e;
|
||||
static std::size_t next(std::size_t block_size, std::size_t & e) {
|
||||
auto n = ipc::detail::max<std::size_t>(block_size, base_size) * e;
|
||||
e = next(e);
|
||||
return n;
|
||||
}
|
||||
@ -238,73 +291,31 @@ struct fixed_expand_policy {
|
||||
template <std::size_t BlockSize,
|
||||
typename AllocP = scope_alloc<>,
|
||||
typename ExpandP = fixed_expand_policy>
|
||||
class fixed_alloc : public detail::fixed_alloc_base {
|
||||
class fixed_alloc : public detail::fixed_alloc<AllocP, ExpandP> {
|
||||
public:
|
||||
using base_t = detail::fixed_alloc_base;
|
||||
using alloc_policy = AllocP;
|
||||
using base_t = detail::fixed_alloc<AllocP, ExpandP>;
|
||||
|
||||
enum : std::size_t {
|
||||
block_size = (ipc::detail::max)(BlockSize, sizeof(void*))
|
||||
};
|
||||
|
||||
private:
|
||||
alloc_policy alloc_;
|
||||
|
||||
void* try_expand() {
|
||||
if (empty()) {
|
||||
auto size = ExpandP::template next<block_size>(init_expand_);
|
||||
auto p = node_p(cursor_ = alloc_.alloc(size));
|
||||
for (std::size_t i = 0; i < (size / block_size) - 1; ++i)
|
||||
p = node_p((*p) = reinterpret_cast<byte_t*>(p) + block_size);
|
||||
(*p) = nullptr;
|
||||
}
|
||||
return cursor_;
|
||||
}
|
||||
|
||||
public:
|
||||
explicit fixed_alloc(std::size_t init_expand = 1) {
|
||||
init(init_expand);
|
||||
explicit fixed_alloc(std::size_t init_expand = 1)
|
||||
: base_t(block_size) {
|
||||
}
|
||||
|
||||
fixed_alloc(fixed_alloc&& rhs) : fixed_alloc() { swap(rhs); }
|
||||
fixed_alloc& operator=(fixed_alloc rhs) { swap(rhs); return (*this); }
|
||||
fixed_alloc(fixed_alloc && rhs)
|
||||
: base_t(std::move(rhs)) {
|
||||
}
|
||||
|
||||
template <typename A>
|
||||
void set_allocator(A && alc) {
|
||||
alloc_ = std::forward<A>(alc);
|
||||
fixed_alloc& operator=(fixed_alloc rhs) {
|
||||
swap(rhs);
|
||||
return (*this);
|
||||
}
|
||||
|
||||
void swap(fixed_alloc& rhs) {
|
||||
alloc_.swap(rhs.alloc_);
|
||||
base_t::swap(rhs);
|
||||
}
|
||||
|
||||
template <typename A = AllocP>
|
||||
auto take(fixed_alloc && rhs) -> ipc::require<detail::has_take<A>::value> {
|
||||
base_t::take(std::move(rhs));
|
||||
alloc_.take(std::move(rhs.alloc_));
|
||||
}
|
||||
|
||||
template <typename A = AllocP>
|
||||
auto take(fixed_alloc && rhs) -> ipc::require<!detail::has_take<A>::value> {
|
||||
base_t::take(std::move(rhs));
|
||||
}
|
||||
|
||||
void clear() {
|
||||
init_expand_ = ExpandP::prev(init_expand_);
|
||||
cursor_ = nullptr;
|
||||
alloc_.~alloc_policy();
|
||||
}
|
||||
|
||||
void* alloc() {
|
||||
void* p = try_expand();
|
||||
cursor_ = next(p);
|
||||
return p;
|
||||
}
|
||||
|
||||
void* alloc(std::size_t) {
|
||||
return alloc();
|
||||
}
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////
|
||||
@ -377,11 +388,6 @@ public:
|
||||
variable_alloc(variable_alloc && rhs) { swap(rhs); }
|
||||
variable_alloc& operator=(variable_alloc rhs) { swap(rhs); return (*this); }
|
||||
|
||||
template <typename A>
|
||||
void set_allocator(A && alc) {
|
||||
alloc_ = std::forward<A>(alc);
|
||||
}
|
||||
|
||||
void swap(variable_alloc& rhs) {
|
||||
alloc_.swap(rhs.alloc_);
|
||||
base_t::swap(rhs);
|
||||
@ -398,10 +404,6 @@ public:
|
||||
base_t::take(std::move(rhs));
|
||||
}
|
||||
|
||||
void clear() {
|
||||
alloc_.~alloc_policy();
|
||||
}
|
||||
|
||||
void* alloc(std::size_t size) {
|
||||
if (size >= ChunkSize) {
|
||||
return alloc_.alloc(size);
|
||||
|
||||
16
src/memory/resource.h
Normal file → Executable file
16
src/memory/resource.h
Normal file → Executable file
@ -21,19 +21,9 @@ using chunk_variable_alloc =
|
||||
static_wrapper<async_wrapper<variable_alloc<
|
||||
sizeof(void*) * 1024 * 256 /* 2MB(x64) */ >>>;
|
||||
|
||||
template <std::size_t Size>
|
||||
using static_async_fixed =
|
||||
static_wrapper<async_wrapper<fixed_alloc<
|
||||
Size, chunk_variable_alloc >>>;
|
||||
|
||||
using big_size_alloc = variable_wrapper<static_async_fixed,
|
||||
default_mapping_policy<
|
||||
default_mapping_policy<>::block_size(default_mapping_policy<>::classes_size),
|
||||
default_mapping_policy<>::iter_size * 2 >>;
|
||||
|
||||
using async_pool_alloc = variable_wrapper<static_async_fixed,
|
||||
default_mapping_policy<>,
|
||||
big_size_alloc>;
|
||||
using async_pool_alloc =
|
||||
static_variable_wrapper<async_wrapper<detail::fixed_alloc<
|
||||
chunk_variable_alloc, fixed_expand_policy>>>;
|
||||
|
||||
template <typename T>
|
||||
using allocator = allocator_wrapper<T, async_pool_alloc>;
|
||||
|
||||
173
src/memory/wrapper.h
Normal file → Executable file
173
src/memory/wrapper.h
Normal file → Executable file
@ -1,14 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include <limits>
|
||||
#include <new>
|
||||
#include <new> // ::new
|
||||
#include <tuple>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
#include <utility>
|
||||
#include <functional> // std::function
|
||||
#include <utility> // std::forward
|
||||
#include <cstddef>
|
||||
#include <type_traits>
|
||||
#include <cassert> // assert
|
||||
#include <type_traits> // std::aligned_storage_t
|
||||
|
||||
#include "def.h"
|
||||
#include "rw_lock.h"
|
||||
@ -80,27 +81,27 @@ public:
|
||||
struct rebind { typedef allocator_wrapper<U, AllocP> other; };
|
||||
|
||||
constexpr size_type max_size(void) const noexcept {
|
||||
return (std::numeric_limits<size_type>::max)() / sizeof(T);
|
||||
return (std::numeric_limits<size_type>::max)() / sizeof(value_type);
|
||||
}
|
||||
|
||||
public:
|
||||
pointer allocate(size_type count) noexcept {
|
||||
if (count == 0) return nullptr;
|
||||
if (count > this->max_size()) return nullptr;
|
||||
return static_cast<pointer>(alloc_.alloc(count * sizeof(T)));
|
||||
return static_cast<pointer>(alloc_.alloc(count * sizeof(value_type)));
|
||||
}
|
||||
|
||||
void deallocate(pointer p, size_type count) noexcept {
|
||||
alloc_.free(p, count * sizeof(T));
|
||||
alloc_.free(p, count * sizeof(value_type));
|
||||
}
|
||||
|
||||
template <typename... P>
|
||||
static void construct(pointer p, P && ... params) {
|
||||
::new (static_cast<void*>(p)) T(std::forward<P>(params)...);
|
||||
::new (static_cast<void*>(p)) value_type(std::forward<P>(params) ...);
|
||||
}
|
||||
|
||||
static void destroy(pointer p) {
|
||||
p->~T();
|
||||
p->~value_type();
|
||||
}
|
||||
};
|
||||
|
||||
@ -148,11 +149,6 @@ public:
|
||||
master_allocs_.swap(rhs.master_allocs_);
|
||||
}
|
||||
|
||||
void clear() {
|
||||
IPC_UNUSED_ auto guard = ipc::detail::unique_lock(master_lock_);
|
||||
master_allocs_.clear();
|
||||
}
|
||||
|
||||
void try_recover(alloc_policy & alc) {
|
||||
IPC_UNUSED_ auto guard = ipc::detail::unique_lock(master_lock_);
|
||||
if (!master_allocs_.empty()) {
|
||||
@ -174,18 +170,16 @@ public:
|
||||
|
||||
template <typename A = AllocP>
|
||||
auto try_replenish(alloc_policy & alc, std::size_t /*size*/)
|
||||
-> ipc::require<detail::has_take<A>::value && !has_remain<A>::value && has_empty<A>::value> {
|
||||
-> ipc::require<(!detail::has_take<A>::value || !has_remain<A>::value) && has_empty<A>::value> {
|
||||
if (!alc.empty()) return;
|
||||
IPC_UNUSED_ auto guard = ipc::detail::unique_lock(master_lock_);
|
||||
if (!master_allocs_.empty()) {
|
||||
alc.take(std::move(master_allocs_.back()));
|
||||
master_allocs_.pop_back();
|
||||
}
|
||||
try_recover(alc);
|
||||
}
|
||||
|
||||
template <typename A = AllocP>
|
||||
constexpr auto try_replenish(alloc_policy & /*alc*/, std::size_t /*size*/) const noexcept
|
||||
-> ipc::require<!detail::has_take<A>::value || (!has_remain<A>::value && !has_empty<A>::value)> {}
|
||||
-> ipc::require<(!detail::has_take<A>::value || !has_remain<A>::value) && !has_empty<A>::value> {
|
||||
// Do Nothing.
|
||||
}
|
||||
|
||||
void collect(alloc_policy && alc) {
|
||||
IPC_UNUSED_ auto guard = ipc::detail::unique_lock(master_lock_);
|
||||
@ -199,7 +193,6 @@ public:
|
||||
using alloc_policy = AllocP;
|
||||
|
||||
constexpr static void swap(empty_alloc_recycler&) noexcept {}
|
||||
constexpr static void clear() noexcept {}
|
||||
constexpr static void try_recover(alloc_policy&) noexcept {}
|
||||
constexpr static auto try_replenish(alloc_policy&, std::size_t) noexcept {}
|
||||
constexpr static void collect(alloc_policy&&) noexcept {}
|
||||
@ -218,51 +211,47 @@ private:
|
||||
async_wrapper * w_ = nullptr;
|
||||
|
||||
public:
|
||||
alloc_proxy(alloc_proxy && rhs)
|
||||
: AllocP(std::move(rhs))
|
||||
{}
|
||||
alloc_proxy(alloc_proxy && rhs) = default;
|
||||
|
||||
alloc_proxy(async_wrapper* w)
|
||||
: AllocP(), w_(w) {
|
||||
if (w_ == nullptr) return;
|
||||
template <typename ... P>
|
||||
alloc_proxy(async_wrapper* w, P && ... pars)
|
||||
: AllocP(std::forward<P>(pars) ...), w_(w) {
|
||||
assert(w_ != nullptr);
|
||||
w_->recycler_.try_recover(*this);
|
||||
}
|
||||
|
||||
~alloc_proxy() {
|
||||
if (w_ == nullptr) return;
|
||||
w_->recycler_.collect(std::move(*this));
|
||||
}
|
||||
|
||||
auto alloc(std::size_t size) {
|
||||
if (w_ != nullptr) {
|
||||
w_->recycler_.try_replenish(*this, size);
|
||||
}
|
||||
return AllocP::alloc(size);
|
||||
}
|
||||
// auto alloc(std::size_t size) {
|
||||
// w_->recycler_.try_replenish(*this, size);
|
||||
// return AllocP::alloc(size);
|
||||
// }
|
||||
};
|
||||
|
||||
friend class alloc_proxy;
|
||||
|
||||
auto& get_alloc() {
|
||||
static tls::pointer<alloc_proxy> tls_alc;
|
||||
return *tls_alc.create(this);
|
||||
}
|
||||
using ref_t = alloc_proxy&;
|
||||
using tls_t = tls::pointer<alloc_proxy>;
|
||||
|
||||
tls_t tls_;
|
||||
std::function<ref_t()> get_alloc_;
|
||||
|
||||
public:
|
||||
void swap(async_wrapper& rhs) {
|
||||
recycler_.swap(rhs.recycler_);
|
||||
}
|
||||
|
||||
void clear() {
|
||||
recycler_.clear();
|
||||
template <typename ... P>
|
||||
async_wrapper(P ... pars) {
|
||||
get_alloc_ = [this, pars ...]()->ref_t {
|
||||
return *tls_.create(this, pars ...);
|
||||
};
|
||||
}
|
||||
|
||||
void* alloc(std::size_t size) {
|
||||
return get_alloc().alloc(size);
|
||||
return get_alloc_().alloc(size);
|
||||
}
|
||||
|
||||
void free(void* p, std::size_t size) {
|
||||
get_alloc().free(p, size);
|
||||
get_alloc_().free(p, size);
|
||||
}
|
||||
};
|
||||
|
||||
@ -281,16 +270,16 @@ private:
|
||||
alloc_policy alloc_;
|
||||
|
||||
public:
|
||||
template <typename ... P>
|
||||
sync_wrapper(P && ... pars)
|
||||
: alloc_(std::forward<P>(pars) ...)
|
||||
{}
|
||||
|
||||
void swap(sync_wrapper& rhs) {
|
||||
IPC_UNUSED_ auto guard = ipc::detail::unique_lock(lock_);
|
||||
alloc_.swap(rhs.alloc_);
|
||||
}
|
||||
|
||||
void clear() {
|
||||
IPC_UNUSED_ auto guard = ipc::detail::unique_lock(lock_);
|
||||
alloc_.~alloc_policy();
|
||||
}
|
||||
|
||||
void* alloc(std::size_t size) {
|
||||
IPC_UNUSED_ auto guard = ipc::detail::unique_lock(lock_);
|
||||
return alloc_.alloc(size);
|
||||
@ -318,10 +307,6 @@ public:
|
||||
|
||||
static void swap(static_wrapper&) {}
|
||||
|
||||
static void clear() {
|
||||
instance().clear();
|
||||
}
|
||||
|
||||
static void* alloc(std::size_t size) {
|
||||
return instance().alloc(size);
|
||||
}
|
||||
@ -341,62 +326,56 @@ struct default_mapping_policy {
|
||||
enum : std::size_t {
|
||||
base_size = BaseSize,
|
||||
iter_size = IterSize,
|
||||
classes_size = 32
|
||||
classes_size = 64
|
||||
};
|
||||
|
||||
static const std::size_t table[classes_size];
|
||||
|
||||
IPC_CONSTEXPR_ static std::size_t classify(std::size_t size) noexcept {
|
||||
auto index = (size <= base_size) ? 0 : ((size - base_size - 1) / iter_size);
|
||||
return (index < classes_size) ?
|
||||
// always uses default_mapping_policy<>::table
|
||||
default_mapping_policy<>::table[index] : classes_size;
|
||||
template <typename F, typename ... P>
|
||||
IPC_CONSTEXPR_ static void foreach(F f, P ... params) {
|
||||
for (std::size_t i = 0; i < classes_size; ++i) f(i, params...);
|
||||
}
|
||||
|
||||
constexpr static std::size_t block_size(std::size_t value) noexcept {
|
||||
return base_size + (value + 1) * iter_size;
|
||||
IPC_CONSTEXPR_ static std::size_t block_size(std::size_t id) noexcept {
|
||||
return (id < classes_size) ? (base_size + (id + 1) * iter_size) : 0;
|
||||
}
|
||||
|
||||
template <typename F, typename D, typename ... P>
|
||||
IPC_CONSTEXPR_ static auto classify(F f, D d, std::size_t size, P ... params) {
|
||||
std::size_t id = (size - base_size - 1) / iter_size;
|
||||
return (id < classes_size) ? f(id, params..., size) : d(params..., size);
|
||||
}
|
||||
};
|
||||
|
||||
template <std::size_t B, std::size_t I>
|
||||
const std::size_t default_mapping_policy<B, I>::table[default_mapping_policy<B, I>::classes_size] = {
|
||||
/* 1 - 8 ~ 32 */
|
||||
0 , 1 , 2 , 3 ,
|
||||
/* 2 - 48 ~ 256 */
|
||||
5 , 5 , 7 , 7 , 9 , 9 , 11, 11, 13, 13, 15, 15, 17, 17,
|
||||
19, 19, 21, 21, 23, 23, 25, 25, 27, 27, 29, 29, 31, 31
|
||||
};
|
||||
|
||||
template <template <std::size_t> class Fixed,
|
||||
typename MappingP = default_mapping_policy<>,
|
||||
typename StaticAlloc = mem::static_alloc>
|
||||
class variable_wrapper {
|
||||
|
||||
template <typename F>
|
||||
constexpr static auto choose(std::size_t size, F&& f) {
|
||||
return ipc::detail::static_switch<MappingP::classes_size>(MappingP::classify(size), [&f](auto index) {
|
||||
return f(Fixed<MappingP::block_size(decltype(index)::value)>{});
|
||||
}, [&f] {
|
||||
return f(StaticAlloc{});
|
||||
});
|
||||
template <typename FixedAlloc,
|
||||
typename DefaultAlloc = mem::static_alloc,
|
||||
typename MappingP = default_mapping_policy<>>
|
||||
class static_variable_wrapper {
|
||||
private:
|
||||
static FixedAlloc& instance(std::size_t id) {
|
||||
static struct initiator {
|
||||
std::aligned_storage_t<sizeof (FixedAlloc),
|
||||
alignof(FixedAlloc)> arr_[MappingP::classes_size];
|
||||
initiator() {
|
||||
MappingP::foreach([](std::size_t id, initiator* t) {
|
||||
::new (&(t->arr_[id])) FixedAlloc(MappingP::block_size(id));
|
||||
}, this);
|
||||
}
|
||||
} init__;
|
||||
return reinterpret_cast<FixedAlloc&>(init__.arr_[id]);
|
||||
}
|
||||
|
||||
public:
|
||||
static void swap(variable_wrapper&) {}
|
||||
|
||||
static void clear() {
|
||||
ipc::detail::static_for<MappingP::classes_size>([](auto index) {
|
||||
Fixed<MappingP::block_size(decltype(index)::value)>::clear();
|
||||
});
|
||||
StaticAlloc::clear();
|
||||
}
|
||||
static void swap(static_variable_wrapper&) {}
|
||||
|
||||
static void* alloc(std::size_t size) {
|
||||
return choose(size, [size](auto&& alc) { return alc.alloc(size); });
|
||||
return MappingP::classify([](std::size_t id, std::size_t size) {
|
||||
return instance(id).alloc(size);
|
||||
}, DefaultAlloc::alloc, size);
|
||||
}
|
||||
|
||||
static void free(void* p, std::size_t size) {
|
||||
choose(size, [p, size](auto&& alc) { alc.free(p, size); });
|
||||
MappingP::classify([](std::size_t id, void* p, std::size_t size) {
|
||||
instance(id).free(p, size);
|
||||
}, static_cast<void(*)(void*, std::size_t)>(DefaultAlloc::free), size, p);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
4
src/pool_alloc.cpp
Normal file → Executable file
4
src/pool_alloc.cpp
Normal file → Executable file
@ -5,10 +5,6 @@
|
||||
namespace ipc {
|
||||
namespace mem {
|
||||
|
||||
void pool_alloc::clear() {
|
||||
async_pool_alloc::clear();
|
||||
}
|
||||
|
||||
void* pool_alloc::alloc(std::size_t size) {
|
||||
return async_pool_alloc::alloc(size);
|
||||
}
|
||||
|
||||
9
test/test.h
Normal file → Executable file
9
test/test.h
Normal file → Executable file
@ -40,12 +40,11 @@ struct test_stopwatch {
|
||||
}
|
||||
}
|
||||
|
||||
template <int Factor>
|
||||
void print_elapsed(int N, int M, int Loops) {
|
||||
auto ts = sw_.elapsed<std::chrono::microseconds>();
|
||||
template <int Factor, typename ToDur = std::chrono::microseconds>
|
||||
void print_elapsed(int N, int M, int Loops, const char * unit = " us/d") {
|
||||
auto ts = sw_.elapsed<ToDur>();
|
||||
std::cout << "[" << N << ":" << M << ", " << Loops << "] "
|
||||
<< "performance: " << (ts / 1000.0) << " ms, "
|
||||
<< (double(ts) / double(Factor ? (Loops * Factor) : (Loops * N))) << " us/d" << std::endl;
|
||||
<< (double(ts) / double(Factor ? (Loops * Factor) : (Loops * N))) << unit << std::endl;
|
||||
}
|
||||
|
||||
void print_elapsed(int N, int M, int Loops) {
|
||||
|
||||
3
test/test_circ.cpp
Normal file → Executable file
3
test/test_circ.cpp
Normal file → Executable file
@ -238,7 +238,8 @@ private slots:
|
||||
void test_prod_cons_1v3();
|
||||
void test_prod_cons_performance();
|
||||
void test_queue();
|
||||
} unit__;
|
||||
};
|
||||
// } unit__;
|
||||
|
||||
#include "test_circ.moc"
|
||||
|
||||
|
||||
6
test/test_ipc.cpp
Normal file → Executable file
6
test/test_ipc.cpp
Normal file → Executable file
@ -179,6 +179,7 @@ private slots:
|
||||
void test_channel();
|
||||
void test_channel_rtt();
|
||||
void test_channel_performance();
|
||||
// };
|
||||
} unit__;
|
||||
|
||||
#include "test_ipc.moc"
|
||||
@ -352,7 +353,6 @@ void Unit::test_route() {
|
||||
}
|
||||
|
||||
void Unit::test_route_rtt() {
|
||||
//return;
|
||||
test_stopwatch sw;
|
||||
|
||||
std::thread t1 {[&] {
|
||||
@ -392,15 +392,13 @@ void Unit::test_route_rtt() {
|
||||
}
|
||||
|
||||
void Unit::test_route_performance() {
|
||||
//return;
|
||||
ipc::detail::static_for<8>([](auto index) {
|
||||
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
|
||||
}
|
||||
|
||||
void Unit::test_channel() {
|
||||
//return;
|
||||
std::thread t1 {[&] {
|
||||
ipc::channel cc { "my-ipc-channel" };
|
||||
for (std::size_t i = 0;; ++i) {
|
||||
|
||||
216
test/test_mem.cpp
Normal file → Executable file
216
test/test_mem.cpp
Normal file → Executable file
@ -1,4 +1,5 @@
|
||||
#include <vector>
|
||||
#include <array>
|
||||
#include <thread>
|
||||
#include <atomic>
|
||||
#include <cstddef>
|
||||
@ -24,58 +25,77 @@ class Unit : public TestSuite {
|
||||
private slots:
|
||||
void initTestCase();
|
||||
|
||||
void test_alloc_free();
|
||||
} unit__;
|
||||
void test_static_alloc();
|
||||
void test_pool_alloc();
|
||||
void test_tc_alloc();
|
||||
};
|
||||
// } unit__;
|
||||
|
||||
#include "test_mem.moc"
|
||||
|
||||
constexpr int DataMin = 4;
|
||||
constexpr int DataMax = 256;
|
||||
constexpr int LoopCount = 100000;
|
||||
constexpr int LoopCount = 4194304;
|
||||
|
||||
// constexpr int DataMin = 256;
|
||||
// constexpr int DataMax = 512;
|
||||
// constexpr int LoopCount = 2097152;
|
||||
|
||||
std::vector<std::size_t> sizes__;
|
||||
|
||||
template <typename M>
|
||||
struct alloc_ix_t {
|
||||
static std::vector<int> ix_[2];
|
||||
static std::vector<int> ix_;
|
||||
static bool inited_;
|
||||
|
||||
alloc_ix_t() {
|
||||
if (inited_) return;
|
||||
inited_ = true;
|
||||
M::init(ix_);
|
||||
}
|
||||
|
||||
int index(std::size_t /*pid*/, std::size_t /*k*/, std::size_t n) {
|
||||
return ix_[n];
|
||||
}
|
||||
};
|
||||
|
||||
template <typename M>
|
||||
std::vector<int> alloc_ix_t<M>::ix_[2] = { std::vector<int>(LoopCount), std::vector<int>(LoopCount) };
|
||||
std::vector<int> alloc_ix_t<M>::ix_(LoopCount);
|
||||
template <typename M>
|
||||
bool alloc_ix_t<M>::inited_ = false;
|
||||
|
||||
struct alloc_random : alloc_ix_t<alloc_random> {
|
||||
alloc_random() {
|
||||
if (inited_) return;
|
||||
inited_ = true;
|
||||
template <std::size_t N>
|
||||
struct alloc_FIFO : alloc_ix_t<alloc_FIFO<N>> {
|
||||
static void init(std::vector<int>& ix) {
|
||||
for (int i = 0; i < LoopCount; ++i) {
|
||||
ix[static_cast<std::size_t>(i)] = i;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template <std::size_t N>
|
||||
struct alloc_LIFO : alloc_ix_t<alloc_LIFO<N>> {
|
||||
static void init(std::vector<int>& ix) {
|
||||
for (int i = 0; i < LoopCount; ++i) {
|
||||
ix[static_cast<std::size_t>(i)] = i;
|
||||
}
|
||||
}
|
||||
|
||||
int index(std::size_t pid, std::size_t k, std::size_t n) {
|
||||
constexpr static int CacheSize = LoopCount / N;
|
||||
if (k) {
|
||||
return this->ix_[(CacheSize * (2 * pid + 1)) - 1 - n];
|
||||
}
|
||||
else return this->ix_[n];
|
||||
}
|
||||
};
|
||||
|
||||
template <std::size_t N>
|
||||
struct alloc_random : alloc_ix_t<alloc_random<N>> {
|
||||
static void init(std::vector<int>& ix) {
|
||||
capo::random<> rdm_index(0, LoopCount - 1);
|
||||
for (int i = 0; i < LoopCount; ++i) {
|
||||
ix_[0][static_cast<std::size_t>(i)] =
|
||||
ix_[1][static_cast<std::size_t>(i)] = rdm_index();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
struct alloc_LIFO : alloc_ix_t<alloc_LIFO> {
|
||||
alloc_LIFO() {
|
||||
if (inited_) return;
|
||||
inited_ = true;
|
||||
for (int i = 0, n = LoopCount - 1; i < LoopCount; ++i, --n) {
|
||||
ix_[0][static_cast<std::size_t>(i)] =
|
||||
ix_[1][static_cast<std::size_t>(n)] = i;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
struct alloc_FIFO : alloc_ix_t<alloc_FIFO> {
|
||||
alloc_FIFO() {
|
||||
if (inited_) return;
|
||||
inited_ = true;
|
||||
for (int i = 0; i < LoopCount; ++i) {
|
||||
ix_[0][static_cast<std::size_t>(i)] =
|
||||
ix_[1][static_cast<std::size_t>(i)] = i;
|
||||
ix[static_cast<std::size_t>(i)] = rdm_index();
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -89,33 +109,13 @@ void Unit::initTestCase() {
|
||||
}
|
||||
}
|
||||
|
||||
template <typename AllocT>
|
||||
void benchmark_alloc() {
|
||||
std::cout << std::endl << type_name<AllocT>() << std::endl;
|
||||
|
||||
test_stopwatch sw;
|
||||
sw.start();
|
||||
|
||||
for (std::size_t k = 0; k < 100; ++k)
|
||||
for (std::size_t n = 0; n < LoopCount; ++n) {
|
||||
std::size_t s = sizes__[n];
|
||||
AllocT::free(AllocT::alloc(s), s);
|
||||
}
|
||||
|
||||
sw.print_elapsed<1>(DataMin, DataMax, LoopCount * 100);
|
||||
}
|
||||
|
||||
template <typename AllocT, typename ModeT, int ThreadsN>
|
||||
template <typename AllocT, int ThreadsN>
|
||||
void benchmark_alloc() {
|
||||
std::cout << std::endl
|
||||
<< "[Threads: " << ThreadsN << ", Mode: " << type_name<ModeT>() << "] "
|
||||
<< "[Threads: " << ThreadsN << "] "
|
||||
<< type_name<AllocT>() << std::endl;
|
||||
|
||||
std::vector<void*> ptrs[ThreadsN];
|
||||
for (auto& vec : ptrs) {
|
||||
vec.resize(LoopCount);
|
||||
}
|
||||
ModeT mode;
|
||||
constexpr static int CacheSize = LoopCount / ThreadsN;
|
||||
|
||||
std::atomic_int fini { 0 };
|
||||
test_stopwatch sw;
|
||||
@ -126,11 +126,50 @@ void benchmark_alloc() {
|
||||
for (auto& w : works) {
|
||||
w = std::thread {[&, pid] {
|
||||
sw.start();
|
||||
for (std::size_t k = 0; k < 10; ++k)
|
||||
for (std::size_t x = 0; x < 2; ++x) {
|
||||
for(std::size_t n = 0; n < LoopCount; ++n) {
|
||||
int m = mode.ix_[x][n];
|
||||
void*& p = ptrs[pid][static_cast<std::size_t>(m)];
|
||||
for (std::size_t k = 0; k < 100; ++k)
|
||||
for (std::size_t n = (CacheSize * pid); n < (CacheSize * (pid + 1)); ++n) {
|
||||
std::size_t s = sizes__[n];
|
||||
AllocT::free(AllocT::alloc(s), s);
|
||||
}
|
||||
if ((fini.fetch_add(1, std::memory_order_relaxed) + 1) == ThreadsN) {
|
||||
sw.print_elapsed<1, std::chrono::nanoseconds>(DataMin, DataMax, LoopCount * 100, " ns/d");
|
||||
}
|
||||
}};
|
||||
++pid;
|
||||
}
|
||||
|
||||
for (auto& w : works) w.join();
|
||||
}
|
||||
|
||||
template <typename AllocT, template <std::size_t> class ModeT, int ThreadsN>
|
||||
void benchmark_alloc() {
|
||||
std::cout << std::endl
|
||||
<< "[Threads: " << ThreadsN << ", Mode: " << type_name<ModeT<ThreadsN>>() << "] "
|
||||
<< type_name<AllocT>() << std::endl;
|
||||
|
||||
constexpr static int CacheSize = LoopCount / ThreadsN;
|
||||
|
||||
std::vector<void*> ptrs[ThreadsN];
|
||||
for (auto& vec : ptrs) {
|
||||
vec.resize(LoopCount);
|
||||
}
|
||||
|
||||
ModeT<ThreadsN> mode;
|
||||
|
||||
std::atomic_int fini { 0 };
|
||||
test_stopwatch sw;
|
||||
|
||||
std::thread works[ThreadsN];
|
||||
int pid = 0;
|
||||
|
||||
for (auto& w : works) {
|
||||
w = std::thread {[&, pid] {
|
||||
auto& vec = ptrs[pid];
|
||||
sw.start();
|
||||
for (std::size_t k = 0; k < 2; ++k)
|
||||
for (std::size_t n = (CacheSize * pid); n < (CacheSize * (pid + 1)); ++n) {
|
||||
int m = mode.index(pid, k, n);
|
||||
void*& p = vec[static_cast<std::size_t>(m)];
|
||||
std::size_t s = sizes__[static_cast<std::size_t>(m)];
|
||||
if (p == nullptr) {
|
||||
p = AllocT::alloc(s);
|
||||
@ -140,33 +179,48 @@ void benchmark_alloc() {
|
||||
p = nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
if ((fini.fetch_add(1, std::memory_order_relaxed) + 1) == ThreadsN) {
|
||||
sw.print_elapsed<1>(DataMin, DataMax, LoopCount * 10 * ThreadsN);
|
||||
sw.print_elapsed<1>(DataMin, DataMax, LoopCount);
|
||||
}
|
||||
}};
|
||||
++pid;
|
||||
}
|
||||
sw.start();
|
||||
|
||||
for (auto& w : works) w.join();
|
||||
}
|
||||
|
||||
template <typename AllocT, typename ModeT, int ThreadsN>
|
||||
template <typename AllocT, template <std::size_t> class ModeT, int ThreadsN>
|
||||
struct test_performance {
|
||||
static void start() {
|
||||
test_performance<AllocT, ModeT, ThreadsN - 1>::start();
|
||||
test_performance<AllocT, ModeT, ThreadsN / 2>::start();
|
||||
benchmark_alloc<AllocT, ModeT, ThreadsN>();
|
||||
}
|
||||
};
|
||||
|
||||
template <typename AllocT, typename ModeT>
|
||||
template <typename AllocT, template <std::size_t> class ModeT>
|
||||
struct test_performance<AllocT, ModeT, 1> {
|
||||
static void start() {
|
||||
benchmark_alloc<AllocT, ModeT, 1>();
|
||||
}
|
||||
};
|
||||
|
||||
template <std::size_t> struct dummy;
|
||||
|
||||
template <typename AllocT, int ThreadsN>
|
||||
struct test_performance<AllocT, dummy, ThreadsN> {
|
||||
static void start() {
|
||||
test_performance<AllocT, dummy, ThreadsN / 2>::start();
|
||||
benchmark_alloc<AllocT, ThreadsN>();
|
||||
}
|
||||
};
|
||||
|
||||
template <typename AllocT>
|
||||
struct test_performance<AllocT, dummy, 1> {
|
||||
static void start() {
|
||||
benchmark_alloc<AllocT, 1>();
|
||||
}
|
||||
};
|
||||
|
||||
// class tc_alloc {
|
||||
// public:
|
||||
// static void clear() {}
|
||||
@ -180,16 +234,28 @@ struct test_performance<AllocT, ModeT, 1> {
|
||||
// }
|
||||
// };
|
||||
|
||||
#define TEST_ALLOC_TYPE /*ipc::mem::static_alloc*/ ipc::mem::async_pool_alloc /*tc_alloc*/
|
||||
void Unit::test_static_alloc() {
|
||||
// test_performance<ipc::mem::static_alloc, dummy , 128>::start();
|
||||
// test_performance<ipc::mem::static_alloc, alloc_FIFO , 128>::start();
|
||||
// test_performance<ipc::mem::static_alloc, alloc_LIFO , 128>::start();
|
||||
// test_performance<ipc::mem::static_alloc, alloc_random, 128>::start();
|
||||
}
|
||||
|
||||
void Unit::test_alloc_free() {
|
||||
// benchmark_alloc <TEST_ALLOC_TYPE>();
|
||||
// test_performance<TEST_ALLOC_TYPE, alloc_FIFO , 24>::start();
|
||||
void Unit::test_pool_alloc() {
|
||||
test_performance<ipc::mem::async_pool_alloc, dummy , 128>::start();
|
||||
test_performance<ipc::mem::async_pool_alloc, alloc_FIFO , 128>::start();
|
||||
|
||||
benchmark_alloc <TEST_ALLOC_TYPE>();
|
||||
test_performance<TEST_ALLOC_TYPE, alloc_FIFO , 16>::start();
|
||||
test_performance<TEST_ALLOC_TYPE, alloc_LIFO , 16>::start();
|
||||
test_performance<TEST_ALLOC_TYPE, alloc_random, 16>::start();
|
||||
test_performance<ipc::mem::async_pool_alloc, dummy , 128>::start();
|
||||
test_performance<ipc::mem::async_pool_alloc, alloc_FIFO , 128>::start();
|
||||
test_performance<ipc::mem::async_pool_alloc, alloc_LIFO , 128>::start();
|
||||
test_performance<ipc::mem::async_pool_alloc, alloc_random, 128>::start();
|
||||
}
|
||||
|
||||
void Unit::test_tc_alloc() {
|
||||
// test_performance<tc_alloc, dummy , 128>::start();
|
||||
// test_performance<tc_alloc, alloc_FIFO , 128>::start();
|
||||
// test_performance<tc_alloc, alloc_LIFO , 128>::start();
|
||||
// test_performance<tc_alloc, alloc_random, 128>::start();
|
||||
}
|
||||
|
||||
} // internal-linkage
|
||||
|
||||
0
test/test_shm.cpp
Normal file → Executable file
0
test/test_shm.cpp
Normal file → Executable file
0
test/test_waiter.cpp
Normal file → Executable file
0
test/test_waiter.cpp
Normal file → Executable file
Loading…
x
Reference in New Issue
Block a user