Optimize MapBuffer representation (#57361)

Summary:
Pull Request resolved: https://github.com/react/react-native/pull/57361

Broadens the former header-shrink change into a single representation-optimization commit for MapBuffer. It bundles three layout optimizations that were previously split: (1) the header is reduced to a single 2-byte `count` field; (2) multi-byte values are read via `memcpy` so unaligned access is well-defined on all platforms; (3) every dynamic-data entry (`String`, `Map`, `MapBufferList`, `IntBuffer`, `DoubleBuffer`) packs its `[offset][byteLength]` into the bucket's 8-byte value instead of writing an in-band length prefix into the dynamic data section.

Net effect: 4 fewer bytes per dynamic entry, one fewer indirection on read (the length is already in the bucket), and every dynamic entry becomes self-delimiting from its bucket alone. No public API change — only the internal serialized representation.

Changelog: [Internal]

landed-with-radar-review

Reviewed By: lenaic, zeyap

Differential Revision: D109848478

fbshipit-source-id: 30749aaa1c2d9fc9b1f8684a3b67815ef44632e8
This commit is contained in:
Pieter De Baets authored and meta-codesync[bot] committed 2026-07-03 01:41:41 -07:00
1 parent 6a957746f6
commit 803456d699
14 files changed
+181 -178

No files matched your search

@@ -51,13 +51,11 @@ private constructor(
ReadableMapBuffer(buffer.duplicate().apply { position(offset) }, offset)
private fun readHeader() {
// byte order
val storedAlignment = buffer.short
if (storedAlignment.toInt() != ALIGNMENT) {
buffer.order(ByteOrder.LITTLE_ENDIAN)
}
// count
count = readUnsignedShort(buffer.position()).toInt()
// The C++ writer always serializes in little-endian byte order. ByteBuffer
// defaults to big-endian and duplicate() resets the order, so set it
// explicitly on every instance, including nested clones.
buffer.order(ByteOrder.LITTLE_ENDIAN)
count = readUnsignedShort(offsetToMapBuffer).toInt()
}
/**
@@ -122,26 +120,27 @@ private constructor(
return readIntValue(bufferPosition) == 1
}
// Dynamic-data entries store [offset][byteLength] in the bucket's 8-byte
// value: getInt(bufferPosition) is the offset, getInt(bufferPosition + 4) is
// the byte length. The dynamic data section itself carries no length prefix.
private fun readStringValue(bufferPosition: Int): String {
val offset = offsetForDynamicData + buffer.getInt(bufferPosition)
val sizeOfString = buffer.getInt(offset)
val sizeOfString = buffer.getInt(bufferPosition + Int.SIZE_BYTES)
val result = ByteArray(sizeOfString)
val stringOffset = offset + Int.SIZE_BYTES
buffer.position(stringOffset)
buffer[result, 0, sizeOfString]
buffer.position(offset)
buffer.get(result, 0, sizeOfString)
return String(result)
}
private fun readMapBufferValue(position: Int): ReadableMapBuffer {
val offset = offsetForDynamicData + buffer.getInt(position)
return cloneWithOffset(offset + Int.SIZE_BYTES)
return cloneWithOffset(offset)
}
private fun readMapBufferListValue(position: Int): List<ReadableMapBuffer> {
val readMapBufferList = arrayListOf<ReadableMapBuffer>()
var offset = offsetForDynamicData + buffer.getInt(position)
val sizeMapBufferList = buffer.getInt(offset)
offset += Int.SIZE_BYTES
val offset = offsetForDynamicData + buffer.getInt(position)
val sizeMapBufferList = buffer.getInt(position + Int.SIZE_BYTES)
var curLen = 0
while (curLen < sizeMapBufferList) {
val sizeMapBuffer = buffer.getInt(offset + curLen)
@@ -153,18 +152,18 @@ private constructor(
}
private fun readIntBufferValue(bufferPosition: Int): IntArray {
var offset = offsetForDynamicData + buffer.getInt(bufferPosition)
val count = buffer.getInt(offset)
offset += Int.SIZE_BYTES
val offset = offsetForDynamicData + buffer.getInt(bufferPosition)
val byteLength = buffer.getInt(bufferPosition + Int.SIZE_BYTES)
val count = byteLength / Int.SIZE_BYTES
val result = IntArray(count)
buffer.duplicate().order(buffer.order()).apply { position(offset) }.asIntBuffer().get(result)
return result
}
private fun readDoubleBufferValue(bufferPosition: Int): DoubleArray {
var offset = offsetForDynamicData + buffer.getInt(bufferPosition)
val count = buffer.getInt(offset)
offset += Int.SIZE_BYTES
val offset = offsetForDynamicData + buffer.getInt(bufferPosition)
val byteLength = buffer.getInt(bufferPosition + Int.SIZE_BYTES)
val count = byteLength / Double.SIZE_BYTES
val result = DoubleArray(count)
buffer.duplicate().order(buffer.order()).apply { position(offset) }.asDoubleBuffer().get(result)
return result
@@ -359,13 +358,10 @@ private constructor(
}
public companion object {
// Value used to verify if the data is serialized with LittleEndian order.
private const val ALIGNMENT = 0xFE
// 2 bytes = 2 (count)
private const val HEADER_SIZE = 2
// 8 bytes = 2 (alignment) + 2 (count) + 4 (size)
private const val HEADER_SIZE = 8
// 10 bytes = 2 (key) + 2 (type) + 8 (value)
// 12 bytes = 2 (key) + 2 (type) + 8 (value)
private const val BUCKET_SIZE = 12
// 2 bytes = 2 (key)
@@ -8,8 +8,43 @@
#include "MapBuffer.h"
#include <react/renderer/mapbuffer/MapBufferBuilder.h>
#include <algorithm>
#include <cstring>
namespace facebook::react {
namespace {
// Reads a value of type T from a (possibly unaligned) offset in the buffer.
// MapBuffer's packed layout places multi-byte values at offsets that are not
// naturally aligned for their type (e.g. an 8-byte value at a 2-byte boundary),
// so dereferencing a reinterpret_cast pointer there is undefined behavior and
// can fault on 32-bit ARM. memcpy compiles to a single unaligned load on
// arm64/x86 and to alignment-safe loads on armv7.
template <typename T>
inline T readUnaligned(const uint8_t* data, int32_t offset) {
T value;
std::memcpy(&value, data + offset, sizeof(T));
return value;
}
// Debug-asserts on OOB (catches corrupt buffers early in dev) AND clamps in
// release so a corrupt bucket length can never drive an OOB memcpy read.
// react_native_assert is compiled out in release, so the runtime cost outside
// dev is the single min() call.
inline int32_t
clampToBufferBounds(int32_t offset, int32_t byteLength, size_t bufferSize) {
react_native_assert(offset >= 0 && byteLength >= 0);
react_native_assert(
static_cast<size_t>(offset) + static_cast<size_t>(byteLength) <=
bufferSize);
size_t maxLength = bufferSize > static_cast<size_t>(offset)
? bufferSize - static_cast<size_t>(offset)
: 0;
return static_cast<int32_t>(
std::min(static_cast<size_t>(std::max(byteLength, 0)), maxLength));
}
} // namespace
static inline int32_t bucketOffset(int32_t index) {
return sizeof(MapBuffer::Header) + sizeof(MapBuffer::Bucket) * index;
}
@@ -18,16 +53,20 @@ static inline int32_t valueOffset(int32_t bucketIndex) {
return bucketOffset(bucketIndex) + offsetof(MapBuffer::Bucket, data);
}
// Dynamic-data entries pack [offset (low 32 bits)][byteLength (high 32 bits)]
// into the bucket's 8-byte value, so the payload in the dynamic data section
// carries no in-band length prefix. This returns the position of the high
// 32 bits (the length).
static inline int32_t lengthOffset(int32_t bucketIndex) {
return valueOffset(bucketIndex) + static_cast<int32_t>(sizeof(int32_t));
}
// TODO T83483191: Extend MapBuffer C++ implementation to support basic random
// access
MapBuffer::MapBuffer(std::vector<uint8_t> data) : bytes_(std::move(data)) {
auto header = reinterpret_cast<const Header*>(bytes_.data());
count_ = header->count;
if (header->bufferSize != bytes_.size()) {
LOG(ERROR) << "Error: Data size does not match, expected "
<< header->bufferSize << " found: " << bytes_.size();
abort();
if (bytes_.size() >= sizeof(Header)) {
auto header = reinterpret_cast<const Header*>(bytes_.data());
count_ = header->count;
}
}
@@ -37,8 +76,7 @@ int32_t MapBuffer::getKeyBucket(Key key) const {
while (lo <= hi) {
int32_t mid = (lo + hi) >> 1;
Key midVal =
*reinterpret_cast<const Key*>(bytes_.data() + bucketOffset(mid));
Key midVal = readUnaligned<Key>(bytes_.data(), bucketOffset(mid));
if (midVal < key) {
lo = mid + 1;
@@ -53,8 +91,7 @@ int32_t MapBuffer::getKeyBucket(Key key) const {
}
inline int32_t MapBuffer::getIntAtBucket(int32_t bucketIndex) const {
return *reinterpret_cast<const int32_t*>(
bytes_.data() + valueOffset(bucketIndex));
return readUnaligned<int32_t>(bytes_.data(), valueOffset(bucketIndex));
}
int32_t MapBuffer::getInt(Key key) const {
@@ -74,8 +111,7 @@ int64_t MapBuffer::getLong(Key key) const {
return 0;
}
return *reinterpret_cast<const int64_t*>(
bytes_.data() + valueOffset(bucketIndex));
return readUnaligned<int64_t>(bytes_.data(), valueOffset(bucketIndex));
}
bool MapBuffer::getBool(Key key) const {
@@ -89,8 +125,7 @@ double MapBuffer::getDouble(Key key) const {
return 0;
}
return *reinterpret_cast<const double*>(
bytes_.data() + valueOffset(bucketIndex));
return readUnaligned<double>(bytes_.data(), valueOffset(bucketIndex));
}
int32_t MapBuffer::getDynamicDataOffset() const {
@@ -107,9 +142,10 @@ std::string MapBuffer::getString(Key key) const {
}
int32_t offset = getDynamicDataOffset() + getIntAtBucket(bucketIndex);
int32_t stringLength =
*reinterpret_cast<const int32_t*>(bytes_.data() + offset);
const uint8_t* stringPtr = bytes_.data() + offset + sizeof(int);
auto stringLength =
readUnaligned<int32_t>(bytes_.data(), lengthOffset(bucketIndex));
stringLength = clampToBufferBounds(offset, stringLength, bytes_.size());
const uint8_t* stringPtr = bytes_.data() + offset;
return {stringPtr, stringPtr + stringLength};
}
@@ -122,17 +158,13 @@ MapBuffer MapBuffer::getMapBuffer(Key key) const {
}
int32_t offset = getDynamicDataOffset() + getIntAtBucket(bucketIndex);
int32_t mapBufferLength =
*reinterpret_cast<const int32_t*>(bytes_.data() + offset);
size_t maxLength = bytes_.size() - offset - sizeof(int32_t);
if (mapBufferLength > maxLength) {
mapBufferLength = maxLength;
}
auto mapBufferLength =
readUnaligned<int32_t>(bytes_.data(), lengthOffset(bucketIndex));
mapBufferLength = clampToBufferBounds(offset, mapBufferLength, bytes_.size());
std::vector<uint8_t> value(mapBufferLength);
memcpy(
value.data(), bytes_.data() + offset + sizeof(int32_t), mapBufferLength);
memcpy(value.data(), bytes_.data() + offset, mapBufferLength);
return MapBuffer(std::move(value));
}
@@ -146,19 +178,27 @@ std::vector<MapBuffer> MapBuffer::getMapBufferList(MapBuffer::Key key) const {
std::vector<MapBuffer> mapBufferList;
int32_t offset = getDynamicDataOffset() + getIntAtBucket(bucketIndex);
int32_t mapBufferListLength =
*reinterpret_cast<const int32_t*>(bytes_.data() + offset);
offset = offset + sizeof(uint32_t);
auto mapBufferListLength =
readUnaligned<int32_t>(bytes_.data(), lengthOffset(bucketIndex));
mapBufferListLength =
clampToBufferBounds(offset, mapBufferListLength, bytes_.size());
int32_t curLen = 0;
while (curLen < mapBufferListLength) {
int32_t mapBufferLength =
*reinterpret_cast<const int32_t*>(bytes_.data() + offset + curLen);
curLen = curLen + sizeof(uint32_t);
if (curLen + sizeof(int32_t) > mapBufferListLength) {
break;
}
auto mapBufferLength =
readUnaligned<int32_t>(bytes_.data(), offset + curLen);
curLen += sizeof(int32_t);
mapBufferLength =
clampToBufferBounds(offset + curLen, mapBufferLength, bytes_.size());
std::vector<uint8_t> value(mapBufferLength);
memcpy(value.data(), bytes_.data() + offset + curLen, mapBufferLength);
mapBufferList.emplace_back(std::move(value));
curLen = curLen + mapBufferLength;
curLen += mapBufferLength;
}
return mapBufferList;
}
@@ -171,13 +211,18 @@ std::vector<int32_t> MapBuffer::getIntBuffer(MapBuffer::Key key) const {
}
int32_t offset = getDynamicDataOffset() + getIntAtBucket(bucketIndex);
int32_t count = *reinterpret_cast<const int32_t*>(bytes_.data() + offset);
auto byteLength =
readUnaligned<int32_t>(bytes_.data(), lengthOffset(bucketIndex));
byteLength = clampToBufferBounds(offset, byteLength, bytes_.size());
int32_t count = byteLength / static_cast<int32_t>(sizeof(int32_t));
std::vector<int32_t> result(count);
if (count > 0) {
// Copy only whole elements: a clamped byteLength may not be a multiple of
// sizeof(int32_t), and result holds exactly count elements.
memcpy(
result.data(),
bytes_.data() + offset + sizeof(int32_t),
bytes_.data() + offset,
static_cast<size_t>(count) * sizeof(int32_t));
}
return result;
@@ -191,13 +236,18 @@ std::vector<double> MapBuffer::getDoubleBuffer(MapBuffer::Key key) const {
}
int32_t offset = getDynamicDataOffset() + getIntAtBucket(bucketIndex);
int32_t count = *reinterpret_cast<const int32_t*>(bytes_.data() + offset);
auto byteLength =
readUnaligned<int32_t>(bytes_.data(), lengthOffset(bucketIndex));
byteLength = clampToBufferBounds(offset, byteLength, bytes_.size());
int32_t count = byteLength / static_cast<int32_t>(sizeof(double));
std::vector<double> result(count);
if (count > 0) {
// Copy only whole elements: a clamped byteLength may not be a multiple of
// sizeof(double), and result holds exactly count elements.
memcpy(
result.data(),
bytes_.data() + offset + sizeof(int32_t),
bytes_.data() + offset,
static_cast<size_t>(count) * sizeof(double));
}
return result;
@@ -40,11 +40,11 @@ class JReadableMapBuffer;
*
* MapBuffer data is stored in a continuous chunk of memory (bytes_ field below) with the following layout:
*
* ┌─────────────────────Header──────────────────────┐
* │ 10 bytes │
* ├─Alignment─┬─Item count─┬──────Buffer size───────┤
* │ 2 bytes │ 2 bytes │ 4 bytes │
* └───────────┴────────────┴────────────────────────┘
* ┌──────Header──────┐
* │ 2 bytes │
* ├────Item count────┤
* │ 2 bytes │
* └──────────────────┘
* ┌────────────────────────────────────────────────────────────────────────────────────────┐
* │ Buckets (one per item in the map) │
* │ │
@@ -69,14 +69,8 @@ class MapBuffer {
public:
using Key = uint16_t;
// The first value in the buffer, used to check correct encoding/endianness on
// JVM side.
constexpr static uint16_t HEADER_ALIGNMENT = 0xFE;
struct Header {
uint16_t alignment = HEADER_ALIGNMENT; // alignment of serialization
uint16_t count; // amount of items in the map
uint32_t bufferSize; // Amount of bytes used to store the map in memory
};
#pragma pack(push, 1)
@@ -89,7 +83,7 @@ class MapBuffer {
};
#pragma pack(pop)
static_assert(sizeof(Header) == 8, "MapBuffer header size is incorrect.");
static_assert(sizeof(Header) == 2, "MapBuffer header size is incorrect.");
static_assert(sizeof(Bucket) == 12, "MapBuffer bucket size is incorrect.");
/**
@@ -105,15 +99,17 @@ class MapBuffer {
String = 3,
Map = 4,
Long = 5,
// Homogeneous, length-prefixed arrays stored contiguously in the dynamic
// Homogeneous arrays of raw elements stored contiguously in the dynamic
// data section. Unlike Map, they carry no per-element key/type overhead, so
// a batch of N values costs ~N*elementSize bytes plus a single 4-byte count
// prefix instead of N*12-byte buckets. The bucket value is the offset of the
// array within the dynamic data section.
// a batch of N values costs ~N*elementSize bytes instead of N*12-byte
// buckets. The bucket value packs [offset][byteLength]; the element count is
// recovered as byteLength / elementSize.
IntBuffer = 6,
DoubleBuffer = 7,
// A homogeneous, ordered array of nested MapBuffers. Distinct from `Map` so
// that a list of MapBuffers is self-describing (a single Map and a list are
// A homogeneous, ordered array of nested MapBuffers. The bucket value packs
// [offset][byteLength] for the whole list region; within it each child stays
// framed as [int32 childSize][child bytes]. Distinct from `Map` so that a
// list of MapBuffers is self-describing (a single Map and a list are
// byte-distinct in payload but previously shared the `Map` type tag).
MapBufferList = 8,
};
@@ -15,6 +15,14 @@ constexpr uint32_t LONG_SIZE = sizeof(uint64_t);
constexpr uint32_t DOUBLE_SIZE = sizeof(double);
constexpr uint32_t MAX_BUCKET_VALUE_SIZE = sizeof(uint64_t);
// Dynamic-data entries store their location in the bucket's 8-byte value as
// [offset (low 32 bits)][byteLength (high 32 bits)], so the payload in the
// dynamic data section needs no in-band length prefix.
static inline uint64_t packOffsetAndLength(int32_t offset, int32_t length) {
return static_cast<uint64_t>(static_cast<uint32_t>(offset)) |
(static_cast<uint64_t>(static_cast<uint32_t>(length)) << 32);
}
MapBuffer MapBufferBuilder::EMPTY() {
return MapBufferBuilder(0).build();
}
@@ -22,7 +30,6 @@ MapBuffer MapBufferBuilder::EMPTY() {
MapBufferBuilder::MapBufferBuilder(uint32_t initialSize) {
buckets_.reserve(initialSize);
header_.count = 0;
header_.bufferSize = 0;
}
void MapBufferBuilder::storeKeyValue(
@@ -84,128 +91,111 @@ void MapBufferBuilder::putLong(MapBuffer::Key key, int64_t value) {
}
void MapBufferBuilder::putString(MapBuffer::Key key, const std::string& value) {
// The wire format encodes lengths and offsets as int32_t (see
// MapBuffer::getString). Without an explicit narrowing cast, `auto` deduces
// size_t (8 bytes on 64-bit) and `memcpy(&x, ..., INT_SIZE)` then copies only
// the first 4 bytes of an 8-byte value: silent truncation on little-endian,
// wrong (high) bytes on big-endian.
auto strSize = static_cast<int32_t>(value.size());
const char* strData = value.data();
// format [length of string (int)] + [Array of Characters in the string]
auto offset = static_cast<int32_t>(dynamicData_.size());
dynamicData_.resize(offset + INT_SIZE + strSize, 0);
memcpy(dynamicData_.data() + offset, &strSize, INT_SIZE);
memcpy(dynamicData_.data() + offset + INT_SIZE, strData, strSize);
// Store Key and pointer to the string
// The bucket stores [offset][byteLength]; the dynamic section holds only the
// raw string bytes.
dynamicData_.resize(offset + strSize, 0);
if (strSize > 0) {
memcpy(dynamicData_.data() + offset, value.data(), strSize);
}
uint64_t data = packOffsetAndLength(offset, strSize);
storeKeyValue(
key,
MapBuffer::DataType::String,
reinterpret_cast<const uint8_t*>(&offset),
INT_SIZE);
reinterpret_cast<const uint8_t*>(&data),
sizeof(data));
}
void MapBufferBuilder::putMapBuffer(MapBuffer::Key key, const MapBuffer& map) {
// Wire format encodes lengths and offsets as int32_t (see
// MapBuffer::getMapBuffer). Cast explicitly so memcpy(&x, ..., INT_SIZE)
// copies the full value, not the first 4 bytes of an 8-byte size_t.
auto mapBufferSize = static_cast<int32_t>(map.size());
auto offset = static_cast<int32_t>(dynamicData_.size());
// format [length of buffer (int)] + [bytes of MapBuffer]
dynamicData_.resize(offset + INT_SIZE + mapBufferSize, 0);
memcpy(dynamicData_.data() + offset, &mapBufferSize, INT_SIZE);
// Copy the content of the map into dynamicData_
memcpy(dynamicData_.data() + offset + INT_SIZE, map.data(), mapBufferSize);
// The bucket stores [offset][byteLength]; the dynamic section holds only the
// serialized child MapBuffer bytes.
dynamicData_.resize(offset + mapBufferSize, 0);
memcpy(dynamicData_.data() + offset, map.data(), mapBufferSize);
// Store Key and pointer to the string
uint64_t data = packOffsetAndLength(offset, mapBufferSize);
storeKeyValue(
key,
MapBuffer::DataType::Map,
reinterpret_cast<const uint8_t*>(&offset),
INT_SIZE);
reinterpret_cast<const uint8_t*>(&data),
sizeof(data));
}
void MapBufferBuilder::putMapBufferList(
MapBuffer::Key key,
const std::vector<MapBuffer>& mapBufferList) {
auto offset = static_cast<int32_t>(dynamicData_.size());
int32_t dataSize = 0;
for (const MapBuffer& mapBuffer : mapBufferList) {
dataSize = dataSize + INT_SIZE + static_cast<int32_t>(mapBuffer.size());
}
dynamicData_.resize(offset + INT_SIZE, 0);
memcpy(dynamicData_.data() + offset, &dataSize, INT_SIZE);
// 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<int32_t>(mapBuffer.size());
auto dynamicDataSize = static_cast<int32_t>(dynamicData_.size());
dynamicData_.resize(dynamicDataSize + INT_SIZE + mapBufferSize, 0);
// format [length of buffer (int)] + [bytes of MapBuffer]
memcpy(dynamicData_.data() + dynamicDataSize, &mapBufferSize, INT_SIZE);
// Copy the content of the map into dynamicData_
auto pos = static_cast<int32_t>(dynamicData_.size());
dynamicData_.resize(pos + INT_SIZE + mapBufferSize, 0);
memcpy(dynamicData_.data() + pos, &mapBufferSize, INT_SIZE);
memcpy(
dynamicData_.data() + dynamicDataSize + INT_SIZE,
mapBuffer.data(),
mapBufferSize);
dynamicData_.data() + pos + INT_SIZE, mapBuffer.data(), mapBufferSize);
}
// Store Key and pointer to the list. Uses the dedicated MapBufferList type so
// the entry is self-describing and distinguishable from a single Map.
auto totalSize = static_cast<int32_t>(dynamicData_.size()) - offset;
uint64_t data = packOffsetAndLength(offset, totalSize);
// Uses the dedicated MapBufferList type so the entry is self-describing and
// distinguishable from a single Map.
storeKeyValue(
key,
MapBuffer::DataType::MapBufferList,
reinterpret_cast<const uint8_t*>(&offset),
INT_SIZE);
reinterpret_cast<const uint8_t*>(&data),
sizeof(data));
}
void MapBufferBuilder::putIntBuffer(
MapBuffer::Key key,
const std::vector<int32_t>& value) {
// Wire format: [element count (int32)] + [count * int32]. The count is the
// number of elements, not bytes; see MapBuffer::getIntBuffer.
auto count = static_cast<int32_t>(value.size());
// The bucket stores [offset][byteLength]; the dynamic section holds the raw
// int32 elements. Element count is recovered as byteLength / sizeof(int32_t).
auto payloadSize = static_cast<int32_t>(value.size() * sizeof(int32_t));
auto offset = static_cast<int32_t>(dynamicData_.size());
dynamicData_.resize(offset + INT_SIZE + payloadSize, 0);
memcpy(dynamicData_.data() + offset, &count, INT_SIZE);
dynamicData_.resize(offset + payloadSize, 0);
if (payloadSize > 0) {
memcpy(dynamicData_.data() + offset + INT_SIZE, value.data(), payloadSize);
memcpy(dynamicData_.data() + offset, value.data(), payloadSize);
}
uint64_t data = packOffsetAndLength(offset, payloadSize);
storeKeyValue(
key,
MapBuffer::DataType::IntBuffer,
reinterpret_cast<const uint8_t*>(&offset),
INT_SIZE);
reinterpret_cast<const uint8_t*>(&data),
sizeof(data));
}
void MapBufferBuilder::putDoubleBuffer(
MapBuffer::Key key,
const std::vector<double>& value) {
// Wire format: [element count (int32)] + [count * double]. Doubles are copied
// byte-for-byte; the reader uses memcpy, so the payload needs no special
// alignment for correctness. A consumer that wants a zero-copy typed view on
// the JVM (ByteBuffer::asDoubleBuffer) must ensure 8-byte alignment itself.
auto count = static_cast<int32_t>(value.size());
// The bucket stores [offset][byteLength]; the dynamic section holds the raw
// double elements. Element count is recovered as byteLength / sizeof(double).
// Doubles are copied byte-for-byte; the reader uses memcpy, so the payload
// needs no special alignment for correctness. A consumer that wants a
// zero-copy typed view on the JVM (ByteBuffer::asDoubleBuffer) must ensure
// 8-byte alignment itself.
auto payloadSize = static_cast<int32_t>(value.size() * sizeof(double));
auto offset = static_cast<int32_t>(dynamicData_.size());
dynamicData_.resize(offset + INT_SIZE + payloadSize, 0);
memcpy(dynamicData_.data() + offset, &count, INT_SIZE);
dynamicData_.resize(offset + payloadSize, 0);
if (payloadSize > 0) {
memcpy(dynamicData_.data() + offset + INT_SIZE, value.data(), payloadSize);
memcpy(dynamicData_.data() + offset, value.data(), payloadSize);
}
uint64_t data = packOffsetAndLength(offset, payloadSize);
storeKeyValue(
key,
MapBuffer::DataType::DoubleBuffer,
reinterpret_cast<const uint8_t*>(&offset),
INT_SIZE);
reinterpret_cast<const uint8_t*>(&data),
sizeof(data));
}
static inline bool compareBuckets(
@@ -220,8 +210,6 @@ MapBuffer MapBufferBuilder::build() {
auto headerSize = sizeof(MapBuffer::Header);
auto bufferSize = headerSize + bucketSize + dynamicData_.size();
header_.bufferSize = static_cast<uint32_t>(bufferSize);
if (needsSort_) {
std::sort(buckets_.begin(), buckets_.end(), compareBuckets);
}
@@ -49,7 +49,7 @@ TEST(MapBufferTest, testSimpleLongMap) {
}
TEST(MapBufferTest, testMapBufferExtension) {
// 26 = 2 buckets: 2*10 + 6 sizeof(header)
// initialSize is a reserve hint for the number of buckets
int initialSize = 26;
auto buffer = MapBufferBuilder(initialSize);
@@ -3247,7 +3247,6 @@ class facebook::react::MapBuffer {
public int32_t getInt(facebook::react::MapBuffer::Key key) const;
public int64_t getLong(facebook::react::MapBuffer::Key key) const;
public size_t size() const;
public static constexpr uint16_t HEADER_ALIGNMENT;
public std::string getString(facebook::react::MapBuffer::Key key) const;
public std::vector<double> getDoubleBuffer(facebook::react::MapBuffer::Key key) const;
public std::vector<facebook::react::MapBuffer> getMapBufferList(facebook::react::MapBuffer::Key key) const;
@@ -3276,9 +3275,7 @@ struct facebook::react::MapBuffer::Bucket {
}
struct facebook::react::MapBuffer::Header {
public uint16_t alignment;
public uint16_t count;
public uint32_t bufferSize;
}
class facebook::react::MapBufferBuilder {
@@ -3156,7 +3156,6 @@ class facebook::react::MapBuffer {
public int32_t getInt(facebook::react::MapBuffer::Key key) const;
public int64_t getLong(facebook::react::MapBuffer::Key key) const;
public size_t size() const;
public static constexpr uint16_t HEADER_ALIGNMENT;
public std::string getString(facebook::react::MapBuffer::Key key) const;
public std::vector<double> getDoubleBuffer(facebook::react::MapBuffer::Key key) const;
public std::vector<facebook::react::MapBuffer> getMapBufferList(facebook::react::MapBuffer::Key key) const;
@@ -3185,9 +3184,7 @@ struct facebook::react::MapBuffer::Bucket {
}
struct facebook::react::MapBuffer::Header {
public uint16_t alignment;
public uint16_t count;
public uint32_t bufferSize;
}
class facebook::react::MapBufferBuilder {
@@ -3244,7 +3244,6 @@ class facebook::react::MapBuffer {
public int32_t getInt(facebook::react::MapBuffer::Key key) const;
public int64_t getLong(facebook::react::MapBuffer::Key key) const;
public size_t size() const;
public static constexpr uint16_t HEADER_ALIGNMENT;
public std::string getString(facebook::react::MapBuffer::Key key) const;
public std::vector<double> getDoubleBuffer(facebook::react::MapBuffer::Key key) const;
public std::vector<facebook::react::MapBuffer> getMapBufferList(facebook::react::MapBuffer::Key key) const;
@@ -3273,9 +3272,7 @@ struct facebook::react::MapBuffer::Bucket {
}
struct facebook::react::MapBuffer::Header {
public uint16_t alignment;
public uint16_t count;
public uint32_t bufferSize;
}
class facebook::react::MapBufferBuilder {
@@ -5482,7 +5482,6 @@ class facebook::react::MapBuffer {
public int32_t getInt(facebook::react::MapBuffer::Key key) const;
public int64_t getLong(facebook::react::MapBuffer::Key key) const;
public size_t size() const;
public static constexpr uint16_t HEADER_ALIGNMENT;
public std::string getString(facebook::react::MapBuffer::Key key) const;
public std::vector<double> getDoubleBuffer(facebook::react::MapBuffer::Key key) const;
public std::vector<facebook::react::MapBuffer> getMapBufferList(facebook::react::MapBuffer::Key key) const;
@@ -5511,9 +5510,7 @@ struct facebook::react::MapBuffer::Bucket {
}
struct facebook::react::MapBuffer::Header {
public uint16_t alignment;
public uint16_t count;
public uint32_t bufferSize;
}
class facebook::react::MapBufferBuilder {
@@ -5406,7 +5406,6 @@ class facebook::react::MapBuffer {
public int32_t getInt(facebook::react::MapBuffer::Key key) const;
public int64_t getLong(facebook::react::MapBuffer::Key key) const;
public size_t size() const;
public static constexpr uint16_t HEADER_ALIGNMENT;
public std::string getString(facebook::react::MapBuffer::Key key) const;
public std::vector<double> getDoubleBuffer(facebook::react::MapBuffer::Key key) const;
public std::vector<facebook::react::MapBuffer> getMapBufferList(facebook::react::MapBuffer::Key key) const;
@@ -5435,9 +5434,7 @@ struct facebook::react::MapBuffer::Bucket {
}
struct facebook::react::MapBuffer::Header {
public uint16_t alignment;
public uint16_t count;
public uint32_t bufferSize;
}
class facebook::react::MapBufferBuilder {
@@ -5479,7 +5479,6 @@ class facebook::react::MapBuffer {
public int32_t getInt(facebook::react::MapBuffer::Key key) const;
public int64_t getLong(facebook::react::MapBuffer::Key key) const;
public size_t size() const;
public static constexpr uint16_t HEADER_ALIGNMENT;
public std::string getString(facebook::react::MapBuffer::Key key) const;
public std::vector<double> getDoubleBuffer(facebook::react::MapBuffer::Key key) const;
public std::vector<facebook::react::MapBuffer> getMapBufferList(facebook::react::MapBuffer::Key key) const;
@@ -5508,9 +5507,7 @@ struct facebook::react::MapBuffer::Bucket {
}
struct facebook::react::MapBuffer::Header {
public uint16_t alignment;
public uint16_t count;
public uint32_t bufferSize;
}
class facebook::react::MapBufferBuilder {
@@ -2168,7 +2168,6 @@ class facebook::react::MapBuffer {
public int32_t getInt(facebook::react::MapBuffer::Key key) const;
public int64_t getLong(facebook::react::MapBuffer::Key key) const;
public size_t size() const;
public static constexpr uint16_t HEADER_ALIGNMENT;
public std::string getString(facebook::react::MapBuffer::Key key) const;
public std::vector<double> getDoubleBuffer(facebook::react::MapBuffer::Key key) const;
public std::vector<facebook::react::MapBuffer> getMapBufferList(facebook::react::MapBuffer::Key key) const;
@@ -2197,9 +2196,7 @@ struct facebook::react::MapBuffer::Bucket {
}
struct facebook::react::MapBuffer::Header {
public uint16_t alignment;
public uint16_t count;
public uint32_t bufferSize;
}
class facebook::react::MapBufferBuilder {
@@ -2104,7 +2104,6 @@ class facebook::react::MapBuffer {
public int32_t getInt(facebook::react::MapBuffer::Key key) const;
public int64_t getLong(facebook::react::MapBuffer::Key key) const;
public size_t size() const;
public static constexpr uint16_t HEADER_ALIGNMENT;
public std::string getString(facebook::react::MapBuffer::Key key) const;
public std::vector<double> getDoubleBuffer(facebook::react::MapBuffer::Key key) const;
public std::vector<facebook::react::MapBuffer> getMapBufferList(facebook::react::MapBuffer::Key key) const;
@@ -2133,9 +2132,7 @@ struct facebook::react::MapBuffer::Bucket {
}
struct facebook::react::MapBuffer::Header {
public uint16_t alignment;
public uint16_t count;
public uint32_t bufferSize;
}
class facebook::react::MapBufferBuilder {
@@ -2165,7 +2165,6 @@ class facebook::react::MapBuffer {
public int32_t getInt(facebook::react::MapBuffer::Key key) const;
public int64_t getLong(facebook::react::MapBuffer::Key key) const;
public size_t size() const;
public static constexpr uint16_t HEADER_ALIGNMENT;
public std::string getString(facebook::react::MapBuffer::Key key) const;
public std::vector<double> getDoubleBuffer(facebook::react::MapBuffer::Key key) const;
public std::vector<facebook::react::MapBuffer> getMapBufferList(facebook::react::MapBuffer::Key key) const;
@@ -2194,9 +2193,7 @@ struct facebook::react::MapBuffer::Bucket {
}
struct facebook::react::MapBuffer::Header {
public uint16_t alignment;
public uint16_t count;
public uint32_t bufferSize;
}
class facebook::react::MapBufferBuilder {