Inline malloc fastpath into operator new.

This saves a small but non-negligible amount of CPU in C++ programs.
This commit is contained in:
David Goldblatt 2021-02-08 08:49:34 -08:00 committed by David Goldblatt
parent 79f81a3732
commit edbfe6912c
5 changed files with 141 additions and 119 deletions

View file

@ -86,10 +86,10 @@ handleOOM(std::size_t size, bool nothrow) {
}
template <bool IsNoExcept>
JEMALLOC_ALWAYS_INLINE
void *
newImpl(std::size_t size) noexcept(IsNoExcept) {
void *ptr = je_malloc(size);
JEMALLOC_NOINLINE
static void *
fallback_impl(std::size_t size) noexcept(IsNoExcept) {
void *ptr = malloc_default(size);
if (likely(ptr != nullptr)) {
return ptr;
}
@ -97,6 +97,13 @@ newImpl(std::size_t size) noexcept(IsNoExcept) {
return handleOOM(size, IsNoExcept);
}
template <bool IsNoExcept>
JEMALLOC_ALWAYS_INLINE
void *
newImpl(std::size_t size) noexcept(IsNoExcept) {
return imalloc_fastpath(size, &fallback_impl<IsNoExcept>);
}
void *
operator new(std::size_t size) {
return newImpl<false>(size);