From 66bfca85c825a0c53254fa7f7787784099395d69 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Wed, 9 Nov 2022 10:59:02 -0800 Subject: Auto increase inlined capacity whenever it does not affect class' size. PiperOrigin-RevId: 487292771 Change-Id: I2454e28fe91017fb2954a4ad194712fcafe893d2 --- absl/container/internal/inlined_vector.h | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'absl/container/internal/inlined_vector.h') diff --git a/absl/container/internal/inlined_vector.h b/absl/container/internal/inlined_vector.h index 48f31c2e..0398f530 100644 --- a/absl/container/internal/inlined_vector.h +++ b/absl/container/internal/inlined_vector.h @@ -379,7 +379,9 @@ class Storage { return data_.allocated.allocated_capacity; } - SizeType GetInlinedCapacity() const { return static_cast>(N); } + SizeType GetInlinedCapacity() const { + return static_cast>(kOptimalInlinedSize); + } StorageView MakeStorageView() { return GetIsAllocated() ? StorageView{GetAllocatedData(), GetSize(), @@ -483,8 +485,15 @@ class Storage { SizeType allocated_capacity; }; + // `kOptimalInlinedSize` is an automatically adjusted inlined capacity of the + // `InlinedVector`. Sometimes, it is possible to increase the capacity (from + // the user requested `N`) without increasing the size of the `InlinedVector`. + static constexpr size_t kOptimalInlinedSize = + (std::max)(N, sizeof(Allocated) / sizeof(ValueType)); + struct Inlined { - alignas(ValueType) char inlined_data[sizeof(ValueType[N])]; + alignas(ValueType) char inlined_data[sizeof( + ValueType[kOptimalInlinedSize])]; }; union Data { -- cgit v1.2.3