Attached is a simple patch which has been extensively tested within Facebook, and is enabled by default in our code base.
Passing the size to the allocator allows it to optimize deallocation, and this was seen to significantly reduce the work required in jemalloc, with about 40% reduction in CPU cycles in the free path. Note jemalloc >= 5.1 is required to fix a bug with 0 sizes. cheers, Pádraig
From eb337e751bdeb5db7d41dd584c179c2cf19485a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A1draig=20Brady?= <pbr...@fb.com> Date: Fri, 22 Feb 2019 17:21:57 -0800 Subject: [PATCH] std::allocator::deallocate support sized-deallocation Pass the size to the allocator so that it may optimize deallocation. This was seen to significantly reduce the work required in jemalloc, with about 40% reduction in CPU cycles in the free path. Note jemalloc >= 5.1 is required to fix a bug with 0 sizes. * libstdc++-v3/include/ext/new_allocator.h (deallocate): Pass the size to the deallocator with -fsized-deallocation. --- libstdc++-v3/include/ext/new_allocator.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/libstdc++-v3/include/ext/new_allocator.h b/libstdc++-v3/include/ext/new_allocator.h index e245391..061d8ce 100644 --- a/libstdc++-v3/include/ext/new_allocator.h +++ b/libstdc++-v3/include/ext/new_allocator.h @@ -114,6 +114,22 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp))); } +#if __cpp_sized_deallocation + // __p is not permitted to be a null pointer. + void + deallocate(pointer __p, size_type __t) + { +#if __cpp_aligned_new + if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__) + { + ::operator delete(__p, __t * sizeof(_Tp), + std::align_val_t(alignof(_Tp))); + return; + } +#endif + ::operator delete(__p, __t * sizeof(_Tp)); + } +#else // __p is not permitted to be a null pointer. void deallocate(pointer __p, size_type) @@ -127,6 +143,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION #endif ::operator delete(__p); } +#endif size_type max_size() const _GLIBCXX_USE_NOEXCEPT -- 2.5.5