From ea38b7876855fbccb33db25d5de449c284dd8a4b Mon Sep 17 00:00:00 2001 From: Evan Tschannen Date: Tue, 26 Mar 2019 12:06:39 -0700 Subject: [PATCH] BinaryWriter uses fastAllocator --- flow/serialize.h | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/flow/serialize.h b/flow/serialize.h index febb1308a4..6a56d7e02f 100644 --- a/flow/serialize.h +++ b/flow/serialize.h @@ -307,7 +307,7 @@ public: rhs.data = 0; } void operator=( BinaryWriter&& r) { - delete[] data; + deleteData(); data = r.data; size = r.size; allocated = r.allocated; @@ -316,7 +316,7 @@ public: r.allocated = 0; r.data = 0; } - ~BinaryWriter() { delete[] data; } + ~BinaryWriter() { deleteData(); } template static Standalone toValue( T const& t, VersionOptions vo ) { @@ -407,15 +407,30 @@ private: int size, allocated; uint64_t m_protocolVersion; + void deleteData() { + if(allocated == 4096) { + FastAllocator<4096>::release(data); + } else { + delete[] data; + } + } + void* writeBytes(int s) { int p = size; size += s; if (size > allocated) { - allocated = std::max(allocated*2, size); - uint8_t* newData = new uint8_t[allocated]; + int nextAllocated = std::max(allocated*2, size); + uint8_t* newData; + if(nextAllocated < 4096) { + nextAllocated = 4096; + newData = (uint8_t*)FastAllocator<4096>::allocate(); + } else { + newData = new uint8_t[nextAllocated]; + } memcpy(newData, data, p); - delete[] data; + deleteData(); data = newData; + allocated = nextAllocated; } return data+p; }