From 3b7389fbc5a5c92b60302570c646559b870452dc Mon Sep 17 00:00:00 2001 From: Andrew Noyes Date: Mon, 21 Mar 2022 17:05:25 -0700 Subject: [PATCH] Use malloc for > 256 in FastAllocated, allocateFast, Arena --- flow/Arena.cpp | 22 +++++++++++----------- flow/FastAlloc.h | 41 ++++++++++++++--------------------------- 2 files changed, 25 insertions(+), 38 deletions(-) diff --git a/flow/Arena.cpp b/flow/Arena.cpp index 2ab44eab85..4fb563c721 100644 --- a/flow/Arena.cpp +++ b/flow/Arena.cpp @@ -342,23 +342,23 @@ ArenaBlock* ArenaBlock::create(int dataSize, Reference& next) { b->bigSize = 256; INSTRUMENT_ALLOCATE("Arena256"); } else if (reqSize <= 512) { - b = (ArenaBlock*)FastAllocator<512>::allocate(); + b = (ArenaBlock*)new uint8_t[512]; b->bigSize = 512; INSTRUMENT_ALLOCATE("Arena512"); } else if (reqSize <= 1024) { - b = (ArenaBlock*)FastAllocator<1024>::allocate(); + b = (ArenaBlock*)new uint8_t[1024]; b->bigSize = 1024; INSTRUMENT_ALLOCATE("Arena1024"); } else if (reqSize <= 2048) { - b = (ArenaBlock*)FastAllocator<2048>::allocate(); + b = (ArenaBlock*)new uint8_t[2048]; b->bigSize = 2048; INSTRUMENT_ALLOCATE("Arena2048"); } else if (reqSize <= 4096) { - b = (ArenaBlock*)FastAllocator<4096>::allocate(); + b = (ArenaBlock*)new uint8_t[4096]; b->bigSize = 4096; INSTRUMENT_ALLOCATE("Arena4096"); } else { - b = (ArenaBlock*)FastAllocator<8192>::allocate(); + b = (ArenaBlock*)new uint8_t[8192]; b->bigSize = 8192; INSTRUMENT_ALLOCATE("Arena8192"); } @@ -460,26 +460,26 @@ void ArenaBlock::destroyLeaf() { FastAllocator<256>::release(this); INSTRUMENT_RELEASE("Arena256"); } else if (bigSize <= 512) { - FastAllocator<512>::release(this); + delete[] reinterpret_cast(this); INSTRUMENT_RELEASE("Arena512"); } else if (bigSize <= 1024) { - FastAllocator<1024>::release(this); + delete[] reinterpret_cast(this); INSTRUMENT_RELEASE("Arena1024"); } else if (bigSize <= 2048) { - FastAllocator<2048>::release(this); + delete[] reinterpret_cast(this); INSTRUMENT_RELEASE("Arena2048"); } else if (bigSize <= 4096) { - FastAllocator<4096>::release(this); + delete[] reinterpret_cast(this); INSTRUMENT_RELEASE("Arena4096"); } else if (bigSize <= 8192) { - FastAllocator<8192>::release(this); + delete[] reinterpret_cast(this); INSTRUMENT_RELEASE("Arena8192"); } else { #ifdef ALLOC_INSTRUMENTATION allocInstr["ArenaHugeKB"].dealloc((bigSize + 1023) >> 10); #endif g_hugeArenaMemory.fetch_sub(bigSize); - delete[](uint8_t*) this; + delete[] reinterpret_cast(this); } } } diff --git a/flow/FastAlloc.h b/flow/FastAlloc.h index 31c7f037f0..993b003603 100644 --- a/flow/FastAlloc.h +++ b/flow/FastAlloc.h @@ -210,13 +210,24 @@ public: if (s != sizeof(Object)) abort(); INSTRUMENT_ALLOCATE(typeid(Object).name()); - void* p = FastAllocator < sizeof(Object) <= 64 ? 64 : nextFastAllocatedSize(sizeof(Object)) > ::allocate(); - return p; + + if constexpr (sizeof(Object) <= 256) { + void* p = FastAllocator < sizeof(Object) <= 64 ? 64 : nextFastAllocatedSize(sizeof(Object)) > ::allocate(); + return p; + } else { + void* p = new uint8_t[nextFastAllocatedSize(sizeof(Object))]; + return p; + } } static void operator delete(void* s) { INSTRUMENT_RELEASE(typeid(Object).name()); - FastAllocator::release(s); + + if constexpr (sizeof(Object) <= 256) { + FastAllocator::release(s); + } else { + delete[] reinterpret_cast(s); + } } // Redefine placement new so you can still use it static void* operator new(size_t, void* p) { return p; } @@ -236,18 +247,6 @@ public: return FastAllocator<128>::allocate(); if (size <= 256) return FastAllocator<256>::allocate(); - if (size <= 512) - return FastAllocator<512>::allocate(); - if (size <= 1024) - return FastAllocator<1024>::allocate(); - if (size <= 2048) - return FastAllocator<2048>::allocate(); - if (size <= 4096) - return FastAllocator<4096>::allocate(); - if (size <= 8192) - return FastAllocator<8192>::allocate(); - if (size <= 16384) - return FastAllocator<16384>::allocate(); return new uint8_t[size]; } @@ -264,18 +263,6 @@ inline void freeFast(int size, void* ptr) { return FastAllocator<128>::release(ptr); if (size <= 256) return FastAllocator<256>::release(ptr); - if (size <= 512) - return FastAllocator<512>::release(ptr); - if (size <= 1024) - return FastAllocator<1024>::release(ptr); - if (size <= 2048) - return FastAllocator<2048>::release(ptr); - if (size <= 4096) - return FastAllocator<4096>::release(ptr); - if (size <= 8192) - return FastAllocator<8192>::release(ptr); - if (size <= 16384) - return FastAllocator<16384>::release(ptr); delete[](uint8_t*) ptr; }