Skip to content

Commit

Permalink
Backport ReactCommon Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
chiaramooney committed Feb 10, 2022
1 parent 319e4df commit 78452b5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
10 changes: 5 additions & 5 deletions ReactCommon/react/renderer/mapbuffer/MapBuffer.cpp
Expand Up @@ -33,11 +33,11 @@ MapBuffer::MapBuffer(std::vector<uint8_t> data) : bytes_(std::move(data)) {
}
}

uint32_t MapBuffer::getKeyBucket(Key key) const {
uint32_t lo = 0;
uint32_t hi = count_ - 1;
int32_t MapBuffer::getKeyBucket(Key key) const {
int32_t lo = 0;
int32_t hi = count_ - 1;
while (lo <= hi) {
uint32_t mid = (lo + hi) >> 1;
int32_t mid = (lo + hi) >> 1;

Key midVal =
*reinterpret_cast<Key const *>(bytes_.data() + bucketOffset(mid));
Expand Down Expand Up @@ -112,7 +112,7 @@ MapBuffer MapBuffer::getMapBuffer(Key key) const {
return MapBuffer(std::move(value));
}

uint32_t MapBuffer::size() const {
size_t MapBuffer::size() const {
return bytes_.size();
}

Expand Down
8 changes: 5 additions & 3 deletions ReactCommon/react/renderer/mapbuffer/MapBuffer.h
Expand Up @@ -80,14 +80,16 @@ class MapBuffer {
uint32_t bufferSize; // Amount of bytes used to store the map in memory
};

struct __attribute__((__packed__)) Bucket {
#pragma pack(push, 1)
struct Bucket {
Key key;
uint16_t type;
uint64_t data;

Bucket(Key key, uint16_t type, uint64_t data)
: key(key), type(type), data(data) {}
};
#pragma pack(pop)

static_assert(sizeof(Header) == 8, "MapBuffer header size is incorrect.");
static_assert(sizeof(Bucket) == 12, "MapBuffer bucket size is incorrect.");
Expand Down Expand Up @@ -124,7 +126,7 @@ class MapBuffer {
// TODO T83483191: review this declaration
MapBuffer getMapBuffer(MapBuffer::Key key) const;

uint32_t size() const;
size_t size() const;

uint8_t const *data() const;

Expand All @@ -140,7 +142,7 @@ class MapBuffer {
// returns the relative offset of the first byte of dynamic data
int32_t getDynamicDataOffset() const;

uint32_t getKeyBucket(MapBuffer::Key key) const;
int32_t getKeyBucket(MapBuffer::Key key) const;

friend ReadableMapBuffer;
};
Expand Down
12 changes: 7 additions & 5 deletions ReactCommon/react/renderer/mapbuffer/MapBufferBuilder.cpp
Expand Up @@ -14,7 +14,7 @@ namespace facebook {
namespace react {

constexpr uint32_t INT_SIZE = sizeof(uint32_t);
constexpr double DOUBLE_SIZE = sizeof(double);
constexpr uint32_t DOUBLE_SIZE = sizeof(double);
constexpr uint32_t MAX_BUCKET_VALUE_SIZE = sizeof(uint64_t);

MapBuffer MapBufferBuilder::EMPTY() {
Expand All @@ -23,6 +23,8 @@ MapBuffer MapBufferBuilder::EMPTY() {

MapBufferBuilder::MapBufferBuilder(uint32_t initialSize) {
buckets_.reserve(initialSize);
header_.count = 0;
header_.bufferSize = 0;
}

void MapBufferBuilder::storeKeyValue(
Expand Down Expand Up @@ -76,7 +78,7 @@ void MapBufferBuilder::putInt(MapBuffer::Key key, int32_t value) {
}

void MapBufferBuilder::putString(MapBuffer::Key key, std::string const &value) {
int32_t strSize = value.size();
auto strSize = value.size();
const char *strData = value.data();

// format [length of string (int)] + [Array of Characters in the string]
Expand All @@ -94,7 +96,7 @@ void MapBufferBuilder::putString(MapBuffer::Key key, std::string const &value) {
}

void MapBufferBuilder::putMapBuffer(MapBuffer::Key key, MapBuffer const &map) {
int32_t mapBufferSize = map.size();
auto mapBufferSize = map.size();

auto offset = dynamicData_.size();

Expand Down Expand Up @@ -122,9 +124,9 @@ MapBuffer MapBufferBuilder::build() {
// Create buffer: [header] + [key, values] + [dynamic data]
auto bucketSize = buckets_.size() * sizeof(MapBuffer::Bucket);
auto headerSize = sizeof(MapBuffer::Header);
uint32_t bufferSize = headerSize + bucketSize + dynamicData_.size();
auto bufferSize = headerSize + bucketSize + dynamicData_.size();

header_.bufferSize = bufferSize;
header_.bufferSize = static_cast<uint32_t>(bufferSize);

if (needsSort_) {
std::sort(buckets_.begin(), buckets_.end(), compareBuckets);
Expand Down
2 changes: 1 addition & 1 deletion ReactCommon/react/renderer/mapbuffer/MapBufferBuilder.h
Expand Up @@ -38,7 +38,7 @@ class MapBufferBuilder {
MapBuffer build();

private:
MapBuffer::Header header_ = {.count = 0, .bufferSize = 0};
MapBuffer::Header header_;

std::vector<MapBuffer::Bucket> buckets_{};

Expand Down

0 comments on commit 78452b5

Please sign in to comment.