Files
react-native/ReactCommon/react/renderer/mapbuffer/Primitives.h
T
David Vacca 022936bcf5 Initial implementation of MapBuffer
Summary:
This diff defines an initial implementation of MapBuffer class. This is an unfinished implementation and the API and internals is going to change considerably in the next days.

The purpose of this stack is to experiment with ByteBuffers moving data from C++ into Java and learn about what're the performance implications of this model.

The format of serialization is going to change in the next few days. I'm going to follow a format similar to https://fb.quip.com/3ENaA782rkkC

I'm expecting to iterate on this API as we expand the development of the new JNI system, PLEASE read all the TODOs as you are reviewing the code.

changelog: [internal] internal

Reviewed By: sammy-SC

Differential Revision: D26364354

fbshipit-source-id: 94e434f699a4250dd240342386eddeaa6acd3ba2
2021-02-17 15:47:07 -08:00

84 lines
2.1 KiB
C++

/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <glog/logging.h>
// TODO: Enable CHECK_CONSISTENCY only in debug mode or test environments (or
// just in demand)
#define CHECK_CONSISTENCY 1
constexpr static int NULL_VALUE = 0;
// Value used to verify if the data is serialized with LittleEndian order
constexpr static int ALIGNMENT = 0xFE;
using Key = uint16_t;
using Byte = uint8_t;
namespace facebook {
namespace react {
struct Header {
uint16_t alignment; // alignment of serialization
uint16_t count; // amount of items
uint16_t bufferSize; // Size of buffer that contains Strings and Objects
};
constexpr static int KEY_SIZE = sizeof(Key);
constexpr static int HEADER_SIZE = sizeof(Header);
constexpr static int INT_SIZE = sizeof(int);
constexpr static int DOUBLE_SIZE = sizeof(double);
constexpr static int UINT8_SIZE = sizeof(uint8_t);
constexpr static int UINT16_SIZE = sizeof(uint16_t);
constexpr static int UINT64_SIZE = sizeof(uint64_t);
// 10 bytes : 2 key + 8 value
constexpr static int BUCKET_SIZE = KEY_SIZE + UINT64_SIZE;
/**
* Returns the offset of the key received by parameter.
*/
inline int getKeyOffset(Key key) {
return HEADER_SIZE + BUCKET_SIZE * key;
}
/**
* Returns the offset of the value associated to the key received by parameter.
*/
inline int getValueOffset(Key key) {
return getKeyOffset(key) + KEY_SIZE;
}
inline void
checkKeyConsistency(const Header &header, const uint8_t *data, Key key) {
#ifdef CHECK_CONSISTENCY
if (key >= header.count) {
LOG(ERROR) << "Error: Key is higher than size of Map - key '" << key
<< "' - size: '" << header.count << "'";
exit(1);
}
Key storedKey = 0;
memcpy(
reinterpret_cast<Key *>(&storedKey),
reinterpret_cast<const Key *>(data + getKeyOffset(key)),
KEY_SIZE);
if (storedKey != key) {
LOG(ERROR) << "Error while reading key, expecting '" << key << "' found: '"
<< storedKey << "'";
exit(1);
}
#endif
}
} // namespace react
} // namespace facebook