From 2b61eaa68071c87bc3a1782a4f1b8326fe8a8651 Mon Sep 17 00:00:00 2001 From: James Ide Date: Fri, 4 Dec 2026 01:47:58 -0800 Subject: [PATCH] Size the MapBuffer payload before writing each list `putMapBufferList` grows its payload vector for each child. Sum the child sizes and their length prefixes, resize the vector once, then write the list in the existing format. The vector still grows geometrically across successive lists. The C++ `MapBufferTest` suite passes. One optimized Apple arm64 binary compares both serializers. Median retired instructions per list fall from 4,372 to 2,757 for five children and from 21,463 to 11,911 for 100 children. When one `MapBufferBuilder` writes 128 consecutive two-child lists, the median falls from 399 to 334 instructions per list. Encoded sizes and checksums match. Signed-off-by: James Ide --- .../renderer/mapbuffer/MapBufferBuilder.cpp | 27 +++++++++++-------- .../mapbuffer/tests/MapBufferTest.cpp | 22 +++++++++++++++ 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/packages/react-native/ReactCommon/react/renderer/mapbuffer/MapBufferBuilder.cpp b/packages/react-native/ReactCommon/react/renderer/mapbuffer/MapBufferBuilder.cpp index f0ced11264f8..3619294b9ad9 100644 --- a/packages/react-native/ReactCommon/react/renderer/mapbuffer/MapBufferBuilder.cpp +++ b/packages/react-native/ReactCommon/react/renderer/mapbuffer/MapBufferBuilder.cpp @@ -135,21 +135,26 @@ void MapBufferBuilder::putMapBuffer(MapBuffer::Key key, const MapBuffer& map) { void MapBufferBuilder::putMapBufferList( MapBuffer::Key key, const std::vector& mapBufferList) { - auto offset = static_cast(dynamicData_.size()); + const auto offset = dynamicData_.size(); + size_t payloadSize = 0; + for (const auto& mapBuffer : mapBufferList) { + payloadSize += INT_SIZE + mapBuffer.size(); + } + dynamicData_.resize(offset + payloadSize); - // The bucket stores [offset][byteLength] for the whole list region; within it - // each child stays framed as [int32 childSize][child bytes] so the children - // remain individually delimited. - for (const MapBuffer& mapBuffer : mapBufferList) { - auto mapBufferSize = static_cast(mapBuffer.size()); - auto pos = static_cast(dynamicData_.size()); - dynamicData_.resize(pos + INT_SIZE + mapBufferSize, 0); - memcpy(dynamicData_.data() + pos, &mapBufferSize, INT_SIZE); + // Each child stays framed as [int32 childSize][child bytes]. + auto position = offset; + for (const auto& mapBuffer : mapBufferList) { + const auto mapBufferSize = static_cast(mapBuffer.size()); + memcpy(dynamicData_.data() + position, &mapBufferSize, INT_SIZE); memcpy( - dynamicData_.data() + pos + INT_SIZE, mapBuffer.data(), mapBufferSize); + dynamicData_.data() + position + INT_SIZE, + mapBuffer.data(), + mapBufferSize); + position += INT_SIZE + mapBufferSize; } - auto totalSize = static_cast(dynamicData_.size()) - offset; + const auto totalSize = static_cast(payloadSize); uint64_t data = packOffsetAndLength(offset, totalSize); // Uses the dedicated MapBufferList type so the entry is self-describing and // distinguishable from a single Map. diff --git a/packages/react-native/ReactCommon/react/renderer/mapbuffer/tests/MapBufferTest.cpp b/packages/react-native/ReactCommon/react/renderer/mapbuffer/tests/MapBufferTest.cpp index 05c70d203f59..aba19b669562 100644 --- a/packages/react-native/ReactCommon/react/renderer/mapbuffer/tests/MapBufferTest.cpp +++ b/packages/react-native/ReactCommon/react/renderer/mapbuffer/tests/MapBufferTest.cpp @@ -236,6 +236,28 @@ TEST(MapBufferTest, testMapListEntriesAtNonZeroOffset) { EXPECT_EQ(readList[0].getInt(1), 42); } +TEST(MapBufferTest, testMapListEntriesWithDifferentPayloadSizes) { + auto child = MapBufferBuilder(); + child.putString(3, "a child with dynamic data"); + + std::vector children; + children.push_back(MapBufferBuilder::EMPTY()); + children.push_back(child.build()); + + auto builder = MapBufferBuilder(); + builder.putString(0, "before"); + builder.putMapBufferList(1, children); + builder.putString(2, "after"); + auto map = builder.build(); + + EXPECT_EQ(map.getString(0), "before"); + auto decoded = map.getMapBufferList(1); + ASSERT_EQ(decoded.size(), 2); + EXPECT_EQ(decoded[0].count(), 0); + EXPECT_EQ(decoded[1].getString(3), "a child with dynamic data"); + EXPECT_EQ(map.getString(2), "after"); +} + TEST(MapBufferTest, testIntBufferEntries) { auto builder = MapBufferBuilder();