From d4e94d9c49719cc9a5349c1b51376fd11fd73408 Mon Sep 17 00:00:00 2001 From: Matthew Parkinson Date: Tue, 2 Jul 2019 11:17:16 +0100 Subject: [PATCH] Minor changes to mpscq fast path. --- CMakeLists.txt | 2 +- src/ds/mpscq.h | 9 +++++---- src/mem/alloc.h | 15 ++++++++------- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 69193b1..8348758 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -121,7 +121,7 @@ if(NOT DEFINED SNMALLOC_ONLY_HEADER_LIBRARY) endmacro() macro(add_shim name) - add_library(${name} SHARED src/override/malloc.cc) + add_library(${name} SHARED src/override/malloc.cc src/override/new.cc) target_link_libraries(${name} snmalloc_lib) target_compile_definitions(${name} PRIVATE "SNMALLOC_EXPORT=__attribute__((visibility(\"default\")))") set_target_properties(${name} PROPERTIES CXX_VISIBILITY_PRESET hidden) diff --git a/src/ds/mpscq.h b/src/ds/mpscq.h index f554473..39ee2f9 100644 --- a/src/ds/mpscq.h +++ b/src/ds/mpscq.h @@ -3,6 +3,8 @@ #include "bits.h" #include "helpers.h" +#include + namespace snmalloc { template @@ -59,7 +61,7 @@ namespace snmalloc prev->next.store(first, std::memory_order_relaxed); } - T* dequeue() + std::pair dequeue() { // Returns the front message, or null if not possible to return a message. invariant(); @@ -69,14 +71,13 @@ namespace snmalloc if (next != nullptr) { front = next; - assert(front); std::atomic_thread_fence(std::memory_order_acquire); invariant(); - return first; + return std::pair(first, true); } - return nullptr; + return std::pair(nullptr, false); } }; } // namespace snmalloc diff --git a/src/mem/alloc.h b/src/mem/alloc.h index ffd4425..25b5aca 100644 --- a/src/mem/alloc.h +++ b/src/mem/alloc.h @@ -649,7 +649,8 @@ namespace snmalloc return (id >> (initial_shift + (r * REMOTE_SLOT_BITS))) & REMOTE_MASK; } - void dealloc(alloc_id_t target_id, void* p, sizeclass_t sizeclass) + FAST_PATH void + dealloc(alloc_id_t target_id, void* p, sizeclass_t sizeclass) { this->size += sizeclass_to_size(sizeclass); @@ -838,9 +839,9 @@ namespace snmalloc message_queue().init(&stub); } - void handle_dealloc_remote(Remote* p) + FAST_PATH void handle_dealloc_remote(Remote* p) { - if (p != &stub) + if (likely(p != &stub)) { Superslab* super = Superslab::get(p); @@ -884,16 +885,16 @@ namespace snmalloc { for (size_t i = 0; i < REMOTE_BATCH; i++) { - Remote* r = message_queue().dequeue(); + auto r = message_queue().dequeue(); - if (r == nullptr) + if (unlikely(!r.second)) break; - handle_dealloc_remote(r); + handle_dealloc_remote(r.first); } // Our remote queues may be larger due to forwarding remote frees. - if (remote.size < REMOTE_CACHE) + if (likely(remote.size < REMOTE_CACHE)) return; stats().remote_post();