From 4994a4809269c30a28cdbb686c8c876fad7b163a Mon Sep 17 00:00:00 2001 From: Louis Williams Date: Thu, 5 Mar 2020 12:59:37 -0500 Subject: SERVER-44095 Initialize TypeBits buffer to a smaller size This lowers the memory overhead of index key generation by reducing the initial buffer size in TypeBits from 512 to 8 bytes. --- src/mongo/bson/util/builder.h | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) (limited to 'src/mongo/bson/util/builder.h') diff --git a/src/mongo/bson/util/builder.h b/src/mongo/bson/util/builder.h index a6c821c5b0f..64b578faafd 100644 --- a/src/mongo/bson/util/builder.h +++ b/src/mongo/bson/util/builder.h @@ -111,6 +111,8 @@ private: SharedBuffer _buf; }; +enum { StackSizeDefault = 512 }; +template class StackAllocator { StackAllocator(const StackAllocator&) = delete; StackAllocator& operator=(const StackAllocator&) = delete; @@ -121,7 +123,6 @@ public: free(); } - enum { SZ = 512 }; void malloc(size_t sz) { if (sz > SZ) _ptr = mongoMalloc(sz); @@ -340,7 +341,21 @@ private: DataView(grow(sizeof(t))).write(tagLittleEndian(t)); } /* "slow" portion of 'grow()' */ - void grow_reallocate(int minSize); + void grow_reallocate(int minSize) { + if (minSize > BufferMaxSize) { + std::stringstream ss; + ss << "BufBuilder attempted to grow() to " << minSize << " bytes, past the 64MB limit."; + msgasserted(13548, ss.str().c_str()); + } + + int a = 64; + while (a < minSize) + a = a * 2; + + _buf.realloc(a); + size = a; + } + BufferAllocator _buf; int l; @@ -360,11 +375,13 @@ MONGO_STATIC_ASSERT(std::is_move_constructible_v); nothing bad would happen. In fact in some circumstances this might make sense, say, embedded in some other object. */ -class StackBufBuilder : public BasicBufBuilder { +template +class StackBufBuilderBase : public BasicBufBuilder> { public: - StackBufBuilder() : BasicBufBuilder(StackAllocator::SZ) {} + StackBufBuilderBase() : BasicBufBuilder>(SZ) {} void release() = delete; // not allowed. not implemented. }; +using StackBufBuilder = StackBufBuilderBase; MONGO_STATIC_ASSERT(!std::is_move_constructible::value); /** std::stringstream deals with locale so this is a lot faster than std::stringstream for UTF8 */ @@ -524,11 +541,11 @@ private: }; using StringBuilder = StringBuilderImpl; -using StackStringBuilder = StringBuilderImpl; +using StackStringBuilder = StringBuilderImpl>; extern template class BasicBufBuilder; -extern template class BasicBufBuilder; +extern template class BasicBufBuilder>; extern template class StringBuilderImpl; -extern template class StringBuilderImpl; +extern template class StringBuilderImpl>; } // namespace mongo -- cgit v1.2.1