From 826295a5e9d62e90d6773e1b1221d650de2f3fe4 Mon Sep 17 00:00:00 2001 From: mrambacher Date: Thu, 21 May 2020 14:46:09 -0700 Subject: [PATCH] Change autovector to have a reserved size in LITE mode (#6868) Summary: Previously in LITE mode, an autovector did not have a reserved size. When elements were added to the vector, the underlying array could be reallocated. There was a set of code that never expands the autovector and was doing &autovector::back(). When the vector is resized, the old addresses may become invalid, causing a later exception to be thrown. By reserving space in the autovector up front, this problem is eliminated for those uses where the vector will never exceed the initial size. the resize happens, these pointers become invalid, leading to SEGV or other exceptions. This change allows the autovector to be fully populated before we take the address of any of its elements, thereby elminating the potential for a resize. There is comparable code to this change in Version::MultiGet for dealing with the context objects. Pull Request resolved: https://github.com/facebook/rocksdb/pull/6868 Reviewed By: ajkr Differential Revision: D21693505 Pulled By: cheng-chang fbshipit-source-id: e71d516b15e08f202593cb80f2a42f048fc95768 --- util/autovector.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/util/autovector.h b/util/autovector.h index 1e6c4716b..7e33e5ca8 100644 --- a/util/autovector.h +++ b/util/autovector.h @@ -19,6 +19,12 @@ namespace ROCKSDB_NAMESPACE { template class autovector : public std::vector { using std::vector::vector; + + public: + autovector() { + // Make sure the initial vector has space for kSize elements + std::vector::reserve(kSize); + } }; #else // A vector that leverages pre-allocated stack-based array to achieve better