Files
react-native/ReactCommon/react/renderer/mapbuffer/MapBuffer.h
T
David Vacca 866bf5f424 Primitive.h -> primitives.h
Summary:
Refactor Primitives.h -> primitives.h

changelog: [internal] internal

Reviewed By: JoshuaGross

Differential Revision: D27210461

fbshipit-source-id: 6b540e20c9123df6b10909564530acfe10845082
2021-03-22 09:16:57 -07:00

83 lines
2.0 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 <react/renderer/mapbuffer/primitives.h>
namespace facebook {
namespace react {
// 506 = 5 entries = 50*10 + 6 sizeof(header)
constexpr uint16_t INITIAL_SIZE = 506;
/**
* MapBuffer is an optimized map format for transferring data like props between
* C++ and other platforms The implementation of this map is optimized to:
* - be compact to optimize space when sparse (sparse is the common case).
* - be accessible through JNI with zero/minimal copying via ByteBuffer.
* - be Have excellent C++ single-write and many-read performance by maximizing
* CPU cache performance through compactness, data locality, and fixed offsets
* where possible.
* - be optimized for iteration and intersection against other maps, but with
* reasonably good random access as well.
* - Work recursively for nested maps/arrays.
* - Supports dynamic types that map to JSON.
* - Don't require mutability - single-write on creation.
* - have minimal APK size and build time impact.
*/
class MapBuffer {
private:
Header _header = {ALIGNMENT, 0, 0};
void makeSpace();
void putBytes(Key key, uint8_t *value, int valueSize);
// Buffer and its size
uint8_t *_data;
uint16_t _dataSize;
public:
MapBuffer() : MapBuffer(INITIAL_SIZE) {}
MapBuffer(uint16_t initialSize);
~MapBuffer();
void putInt(Key key, int value);
void putBool(Key key, bool value);
void putDouble(Key key, double value);
void putNull(Key key);
// TODO: create a MapBufferBuilder instead or add checks to verify
// if it's ok to read and write the Map
void finish();
int getInt(Key key);
bool getBool(Key key);
double getDouble(Key key);
uint16_t getBufferSize();
// TODO: review parameters of copy method
void copy(uint8_t *output);
bool isNull(Key key);
uint16_t getSize();
};
} // namespace react
} // namespace facebook