diff options
author | Abseil Team <absl-team@google.com> | 2022-11-09 10:59:02 -0800 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2022-11-09 11:00:09 -0800 |
commit | 66bfca85c825a0c53254fa7f7787784099395d69 (patch) | |
tree | 3f53016a11f81669bc504143c5068e1bc3102a2e /absl/container/internal/inlined_vector.h | |
parent | 64f00b1f4a064e9e140fc4642ccd55c9c2e2e365 (diff) | |
download | abseil-66bfca85c825a0c53254fa7f7787784099395d69.tar.gz abseil-66bfca85c825a0c53254fa7f7787784099395d69.tar.bz2 abseil-66bfca85c825a0c53254fa7f7787784099395d69.zip |
Auto increase inlined capacity whenever it does not affect class' size.
PiperOrigin-RevId: 487292771
Change-Id: I2454e28fe91017fb2954a4ad194712fcafe893d2
Diffstat (limited to 'absl/container/internal/inlined_vector.h')
-rw-r--r-- | absl/container/internal/inlined_vector.h | 13 |
1 files changed, 11 insertions, 2 deletions
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<A> GetInlinedCapacity() const { return static_cast<SizeType<A>>(N); } + SizeType<A> GetInlinedCapacity() const { + return static_cast<SizeType<A>>(kOptimalInlinedSize); + } StorageView<A> MakeStorageView() { return GetIsAllocated() ? StorageView<A>{GetAllocatedData(), GetSize(), @@ -483,8 +485,15 @@ class Storage { SizeType<A> 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<A>)); + struct Inlined { - alignas(ValueType<A>) char inlined_data[sizeof(ValueType<A>[N])]; + alignas(ValueType<A>) char inlined_data[sizeof( + ValueType<A>[kOptimalInlinedSize])]; }; union Data { |