Skip to content

Commit 870676d

Browse files
authored
Merge pull request #66 from Flow-IPC/ipc-143_perf-dive
(SHM-jemalloc) Removed an INFO-severity logged message that is logged from `ipc::shm::arena_lend::jemalloc::Ipc_arena::get_collection_id()`, a public API but more likely invoked internally from `Shm_session::lend_object()` which can be oft-used directly and/or for each capnp-encoded message sent through a SHM-jemalloc-backed `ipc::transport::struc::Channel`. / Use move-semantics in a few places internally to avoid unnecessary `shared_ptr` ref-count arithmetic. / Effectively remove a few redundant internal `assert()`s. / Comment and/or doc changes.
2 parents 94c4490 + 1a2ce63 commit 870676d

File tree

5 files changed

+40
-42
lines changed

5 files changed

+40
-42
lines changed

src/ipc/shm/arena_lend/jemalloc/ipc_arena.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,14 +240,14 @@ void Ipc_arena::Event_listener_impl::notify_removed_shm_pool(const shared_ptr<Sh
240240
}
241241

242242
Ipc_arena::Ipc_object_deleter_no_cache::Ipc_object_deleter_no_cache(
243-
const shared_ptr<Shm_pool_collection>& pool_collection, Arena_id arena_id) :
244-
Object_deleter_no_cache(pool_collection, arena_id)
243+
shared_ptr<Shm_pool_collection>&& pool_collection, Arena_id arena_id) :
244+
Object_deleter_no_cache(std::move(pool_collection), arena_id)
245245
{
246246
}
247247

248248
Ipc_arena::Ipc_object_deleter_cache::Ipc_object_deleter_cache(
249-
const shared_ptr<Thread_cache>& thread_cache) :
250-
Object_deleter_cache(thread_cache)
249+
shared_ptr<Thread_cache>&& thread_cache) :
250+
Object_deleter_cache(std::move(thread_cache))
251251
{
252252
}
253253

src/ipc/shm/arena_lend/jemalloc/ipc_arena.hpp

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ class Ipc_arena :
230230
* @param pool_collection The object that allocated the underlying memory from which it should be returned
231231
* @param arena_id The arena in which the allocation was performed
232232
*/
233-
Ipc_object_deleter_no_cache(const std::shared_ptr<Shm_pool_collection>& pool_collection,
233+
Ipc_object_deleter_no_cache(std::shared_ptr<Shm_pool_collection>&& pool_collection,
234234
Arena_id arena_id);
235235

236236
/**
@@ -259,7 +259,7 @@ class Ipc_arena :
259259
*
260260
* @param thread_cache The thread cache to associate the allocation with.
261261
*/
262-
Ipc_object_deleter_cache(const std::shared_ptr<Thread_cache>& thread_cache);
262+
Ipc_object_deleter_cache(std::shared_ptr<Thread_cache>&& thread_cache);
263263

264264
/**
265265
* Release callback executed by shared pointer destruction. The destructor for the object will be called
@@ -402,10 +402,11 @@ Ipc_arena::Handle<T> Ipc_arena::construct_impl(bool use_cache, Args&&... args)
402402
Ipc_arena_activator ctx(this);
403403
if (use_cache)
404404
{
405-
std::shared_ptr<Thread_cache> thread_cache = get_or_create_thread_cache(arena_id);
405+
auto thread_cache = get_or_create_thread_cache(arena_id);
406+
const auto thread_cache_id = thread_cache->get_thread_cache_id();
406407
return construct_helper<T>(
407-
Shm_pool_collection::allocate(sizeof(T), arena_id, thread_cache->get_thread_cache_id()),
408-
Ipc_object_deleter_cache(thread_cache),
408+
Shm_pool_collection::allocate(sizeof(T), arena_id, thread_cache_id),
409+
Ipc_object_deleter_cache(std::move(thread_cache)),
409410
std::forward<Args>(args)...);
410411
}
411412
else
@@ -422,23 +423,15 @@ template <typename T>
422423
Collection_id Ipc_arena::get_collection_id(const std::shared_ptr<T>& object)
423424
{
424425
{
425-
const auto* deleter = std::get_deleter<Ipc_object_deleter_no_cache>(object);
426+
auto const * const deleter = std::get_deleter<Ipc_object_deleter_no_cache>(object);
426427
if (deleter != nullptr)
427428
{
428-
auto pool_collection = deleter->get_pool_collection();
429-
auto* logger = pool_collection->get_logger();
430-
if ((logger != nullptr) && logger->should_log(flow::log::Sev::S_INFO, Log_component::S_SESSION))
431-
{
432-
FLOW_LOG_SET_CONTEXT(logger, Log_component::S_SESSION);
433-
FLOW_LOG_INFO_WITHOUT_CHECKING("Pool collection " << pool_collection << ", count " <<
434-
pool_collection.use_count());
435-
}
436-
return pool_collection->get_id();
429+
return deleter->get_pool_collection()->get_id();
437430
}
438431
}
439432

440433
{
441-
const auto* deleter = std::get_deleter<Ipc_object_deleter_cache>(object);
434+
auto const * const deleter = std::get_deleter<Ipc_object_deleter_cache>(object);
442435
if (deleter != nullptr)
443436
{
444437
return deleter->get_thread_cache()->get_owner()->get_id();

src/ipc/shm/arena_lend/jemalloc/shm_pool_collection.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ void* Shm_pool_collection::allocate(size_t size)
230230
// Use the first arena
231231
auto iter = m_arenas.begin();
232232
assert(iter != m_arenas.end());
233-
return allocate(size, *iter);
233+
return get_jemalloc_memory_manager()->allocate(size, *iter);
234234
}
235235

236236
void* Shm_pool_collection::allocate(size_t size, Arena_id arena_id)
@@ -256,7 +256,7 @@ void Shm_pool_collection::deallocate(void* address)
256256
// Use the first arena
257257
auto iter = m_arenas.begin();
258258
assert(iter != m_arenas.end());
259-
deallocate(address, *iter);
259+
get_jemalloc_memory_manager()->deallocate(address, *iter);
260260
}
261261

262262
void Shm_pool_collection::deallocate(void* address, Arena_id arena_id)
@@ -614,17 +614,17 @@ Shm_pool_collection::get_or_create_thread_cache(Arena_id arena_id)
614614
}
615615

616616
Shm_pool_collection::Object_deleter_no_cache::Object_deleter_no_cache(
617-
const shared_ptr<Shm_pool_collection>& pool_collection, Arena_id arena_id) :
618-
m_pool_collection(pool_collection),
617+
shared_ptr<Shm_pool_collection>&& pool_collection, Arena_id arena_id) :
618+
m_pool_collection(std::move(pool_collection)),
619619
m_arena_id(arena_id)
620620
{
621621
}
622622

623623
Shm_pool_collection::Object_deleter_cache::Object_deleter_cache(
624-
const shared_ptr<Thread_cache>& thread_cache) :
625-
m_thread_cache(thread_cache)
624+
shared_ptr<Thread_cache>&& thread_cache) :
625+
m_thread_cache(std::move(thread_cache))
626626
{
627-
assert(thread_cache != nullptr);
627+
assert(m_thread_cache != nullptr);
628628
}
629629

630630
bool Shm_pool_collection::Thread_local_data::insert_cache(const shared_ptr<Thread_cache>& thread_cache)

src/ipc/shm/arena_lend/jemalloc/shm_pool_collection.hpp

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,16 @@ class Shm_pool_collection :
113113
{
114114
if (use_cache)
115115
{
116-
std::shared_ptr<Thread_cache> thread_cache = get_or_create_thread_cache(arena_id);
117-
return construct_helper<T>(allocate(sizeof(T), arena_id, thread_cache->get_thread_cache_id()),
118-
Object_deleter_cache(thread_cache),
119-
std::forward<Args>(args)...);
120-
}
121-
else
122-
{
123-
return construct_helper<T>(allocate(sizeof(T), arena_id),
124-
Object_deleter_no_cache(shared_from_this(), arena_id),
116+
auto thread_cache = get_or_create_thread_cache(arena_id);
117+
const auto thread_cache_id = thread_cache->get_thread_cache_id();
118+
return construct_helper<T>(allocate(sizeof(T), arena_id, thread_cache_id),
119+
Object_deleter_cache(std::move(thread_cache)),
125120
std::forward<Args>(args)...);
126121
}
122+
// else
123+
return construct_helper<T>(allocate(sizeof(T), arena_id),
124+
Object_deleter_no_cache(shared_from_this(), arena_id),
125+
std::forward<Args>(args)...);
127126
}
128127

129128
/**
@@ -542,7 +541,7 @@ class Shm_pool_collection :
542541
* @param pool_collection The object that allocated the underlying memory from which it should be returned.
543542
* @param arena_id The arena in which the allocation was performed.
544543
*/
545-
Object_deleter_no_cache(const std::shared_ptr<Shm_pool_collection>& pool_collection, Arena_id arena_id);
544+
Object_deleter_no_cache(std::shared_ptr<Shm_pool_collection>&& pool_collection, Arena_id arena_id);
546545

547546
/**
548547
* Release callback executed by shared pointer destruction. The destructor for the object will be called
@@ -554,7 +553,13 @@ class Shm_pool_collection :
554553
template <typename T>
555554
void operator()(T* p)
556555
{
557-
// Call object's destructor (works for primitives)
556+
/* Call object's destructor (works for primitives).
557+
* Key context: an allocator-equipped T (usually STL-compliant container) here will (in T::~T())
558+
* call-through to m_pool_collection->deallocate() for each buffer that T had allocated for itself
559+
* (e.g., for vector<> internal buffer; for list<> the various nodes). In the process more destructors
560+
* may be invoked which would quite possibly do more dtor calling and thus deallocate()ing. And so on.
561+
* Hence the present operator()() is called at just the outer layer, then the inner deallocations (if any)
562+
* happen, and then we call deallocate() on the outer object's memory, last-thing, just below. */
558563
p->~T();
559564
m_pool_collection->deallocate(p, m_arena_id);
560565
}
@@ -591,7 +596,7 @@ class Shm_pool_collection :
591596
*
592597
* @param thread_cache The thread cache to associate the allocation with.
593598
*/
594-
Object_deleter_cache(const std::shared_ptr<Thread_cache>& thread_cache);
599+
Object_deleter_cache(std::shared_ptr<Thread_cache>&& thread_cache);
595600

596601
/**
597602
* Release callback executed by shared pointer destruction. The destructor for the object will be called
@@ -603,7 +608,7 @@ class Shm_pool_collection :
603608
template <typename T>
604609
void operator()(T* p)
605610
{
606-
// Call object's destructor (works for primitives)
611+
// Call object's destructor (works for primitives). See more key notes in Object_deleter_no_cache comment.
607612
p->~T();
608613
m_thread_cache->get_owner()->deallocate(p,
609614
m_thread_cache->get_arena_id(),

src/ipc/shm/arena_lend/owner_shm_pool_collection.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,9 @@ class Owner_shm_pool_collection :
266266
inline std::string generate_shm_object_name(pool_id_t shm_pool_id) const;
267267

268268
/**
269-
* Performs an allocation backed by shared memory, uses the allocation to construct an object and returns a shared
269+
* Construct an object at a pre-allocated location in shared memory and returns a shared
270270
* pointer to this object. When the shared pointer has no more references, it will be destructed by this pool
271-
* collection instance. The start() method must be executed prior to any calls to this.
271+
* collection instance via the given deleter. The start() method must be executed prior to any calls to this.
272272
*
273273
* @tparam T The object type to be created.
274274
* @tparam Deleter A copy-constructible class containing operator() that performs destruction of the memory.

0 commit comments

Comments
 (0)