Browse Source

YT-18375: Get rid of zero size padding

aleexfi 1 year ago
parent
commit
c2ca4487e6

+ 12 - 1
library/cpp/yt/small_containers/compact_vector.h

@@ -164,17 +164,28 @@ private:
         uint8_t SizePlusOne;
     } alias_hack;
 
+    // TODO(aleexfi): Use [[no_unique_address]] when clang will support it on windows.
+    template <class = void>
     struct TOnHeapMeta
     {
         char Padding[ByteSize - sizeof(uintptr_t)];
         TOnHeapStorage* Storage;
     } alias_hack;
 
+    template <class _>
+        requires (ByteSize == sizeof(uintptr_t))
+    struct TOnHeapMeta<_>
+    {
+        TOnHeapStorage* Storage;
+    } alias_hack;
+
+    static_assert(sizeof(TOnHeapMeta<>) == ByteSize);
+
     union
     {
         T InlineElements_[N];
         TInlineMeta InlineMeta_;
-        TOnHeapMeta OnHeapMeta_;
+        TOnHeapMeta<> OnHeapMeta_;
     };
 
     bool IsInline() const;

+ 11 - 0
library/cpp/yt/small_containers/unittests/compact_vector_ut.cpp

@@ -1080,6 +1080,17 @@ TEST(CompactVectorTest, AssignToLonger) {
   EXPECT_EQ("foo", lhs[0]);
 }
 
+TEST(CompactVectorTest, ZeroPaddingOnHeapMeta) {
+  TCompactVector<uint8_t, 6> vector;
+  std::vector<uint8_t> expected;
+  for (int i = 0; i < 10; ++i) {
+    vector.push_back(i);
+    expected.push_back(i);
+
+    ASSERT_THAT(vector, ::testing::ElementsAreArray(expected));
+  }
+}
+
 ////////////////////////////////////////////////////////////////////////////////
 
 } // namespace