mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
022936bcf5
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
83 lines
2.0 KiB
C++
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)
|
|
const int 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(int 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
|