Skip to content

Commit

Permalink
Extract mapbuffer from reactutils module
Browse files Browse the repository at this point in the history
Summary:
`MapBuffer` is not used in RN utils for anything shared for now, so we can remove it from the build config by reordering methods, shaving 20KB in APK size for each architecture.

Also applies clang-tidy rules to `MapBuffer`/`folly::dynamic` configurations.

Changelog: [Internal] - Remove `mapbuffer` dependency from `Android.mk` of reactutilsjni

Reviewed By: javache, cortinico

Differential Revision: D34620455

fbshipit-source-id: ad3717448f5c20fd071f71d436bb9dd00efe7eb0
  • Loading branch information
Andrei Shikov authored and pull[bot] committed Sep 5, 2022
1 parent 33cba91 commit f6901d0
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 158 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,139 @@

#include <limits>

#include <react/common/mapbuffer/ReadableMapBuffer.h>
#include <react/jni/ReadableNativeMap.h>
#include <react/renderer/attributedstring/conversions.h>
#include <react/renderer/core/conversions.h>
#include <react/renderer/mapbuffer/MapBuffer.h>
#include <react/renderer/mapbuffer/MapBufferBuilder.h>
#include <react/renderer/telemetry/TransactionTelemetry.h>
#include <react/utils/LayoutManager.h>

using namespace facebook::jni;

namespace facebook {
namespace react {

Size measureAndroidComponent(
ContextContainer::Shared const &contextContainer,
Tag rootTag,
std::string const &componentName,
folly::dynamic localData,
folly::dynamic props,
folly::dynamic state,
float minWidth,
float maxWidth,
float minHeight,
float maxHeight,
jfloatArray attachmentPositions) {
const jni::global_ref<jobject> &fabricUIManager =
contextContainer->at<jni::global_ref<jobject>>("FabricUIManager");

static auto measure =
jni::findClassStatic("com/facebook/react/fabric/FabricUIManager")
->getMethod<jlong(
jint,
jstring,
ReadableMap::javaobject,
ReadableMap::javaobject,
ReadableMap::javaobject,
jfloat,
jfloat,
jfloat,
jfloat,
jfloatArray)>("measure");

auto componentNameRef = make_jstring(componentName);
local_ref<ReadableNativeMap::javaobject> localDataRNM =
ReadableNativeMap::newObjectCxxArgs(std::move(localData));
local_ref<ReadableNativeMap::javaobject> propsRNM =
ReadableNativeMap::newObjectCxxArgs(std::move(props));
local_ref<ReadableNativeMap::javaobject> stateRNM =
ReadableNativeMap::newObjectCxxArgs(std::move(state));

local_ref<ReadableMap::javaobject> localDataRM =
make_local(reinterpret_cast<ReadableMap::javaobject>(localDataRNM.get()));
local_ref<ReadableMap::javaobject> propsRM =
make_local(reinterpret_cast<ReadableMap::javaobject>(propsRNM.get()));
local_ref<ReadableMap::javaobject> stateRM =
make_local(reinterpret_cast<ReadableMap::javaobject>(stateRNM.get()));

auto size = yogaMeassureToSize(measure(
fabricUIManager,
rootTag,
componentNameRef.get(),
localDataRM.get(),
propsRM.get(),
stateRM.get(),
minWidth,
maxWidth,
minHeight,
maxHeight,
attachmentPositions));

// Explicitly release smart pointers to free up space faster in JNI tables
componentNameRef.reset();
localDataRM.reset();
localDataRNM.reset();
propsRM.reset();
propsRNM.reset();
stateRM.reset();
stateRNM.reset();

return size;
}

Size measureAndroidComponentMapBuffer(
const ContextContainer::Shared &contextContainer,
Tag rootTag,
std::string const &componentName,
MapBuffer localData,
MapBuffer props,
float minWidth,
float maxWidth,
float minHeight,
float maxHeight,
jfloatArray attachmentPositions) {
const jni::global_ref<jobject> &fabricUIManager =
contextContainer->at<jni::global_ref<jobject>>("FabricUIManager");
auto componentNameRef = make_jstring(componentName);

static auto measure =
jni::findClassStatic("com/facebook/react/fabric/FabricUIManager")
->getMethod<jlong(
jint,
jstring,
ReadableMapBuffer::javaobject,
ReadableMapBuffer::javaobject,
jfloat,
jfloat,
jfloat,
jfloat,
jfloatArray)>("measureMapBuffer");

auto localDataMap =
ReadableMapBuffer::createWithContents(std::move(localData));
auto propsMap = ReadableMapBuffer::createWithContents(std::move(props));

auto size = yogaMeassureToSize(measure(
fabricUIManager,
rootTag,
componentNameRef.get(),
localDataMap.get(),
propsMap.get(),
minWidth,
maxWidth,
minHeight,
maxHeight,
attachmentPositions));

// Explicitly release smart pointers to free up space faster in JNI tables
componentNameRef.reset();
localDataMap.reset();
propsMap.reset();
return size;
}

TextLayoutManager::TextLayoutManager(
const ContextContainer::Shared &contextContainer)
: contextContainer_(contextContainer),
Expand Down Expand Up @@ -81,7 +203,7 @@ TextMeasurement TextLayoutManager::measureCachedSpannableById(
contextContainer_,
-1, // TODO: we should pass rootTag in
"RCTText",
cacheIdMap,
std::move(cacheIdMap),
toDynamic(paragraphAttributes),
nullptr,
minimumSize.width,
Expand Down Expand Up @@ -232,7 +354,7 @@ TextMeasurement TextLayoutManager::doMeasure(

auto attachments = TextMeasurement::Attachments{};
if (attachmentsCount > 0) {
folly::dynamic fragments = serializedAttributedString["fragments"];
folly::dynamic const &fragments = serializedAttributedString["fragments"];
int attachmentIndex = 0;
for (auto const &fragment : fragments) {
auto isAttachment = fragment.find("isAttachment");
Expand Down Expand Up @@ -284,8 +406,8 @@ TextMeasurement TextLayoutManager::doMeasureMapBuffer(
contextContainer_,
-1, // TODO: we should pass rootTag in
"RCTText",
attributedStringMap,
paragraphAttributesMap,
std::move(attributedStringMap),
std::move(paragraphAttributesMap),
minimumSize.width,
maximumSize.width,
minimumSize.height,
Expand Down
4 changes: 1 addition & 3 deletions ReactCommon/react/utils/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@ LOCAL_STATIC_LIBRARIES :=
LOCAL_SHARED_LIBRARIES := \
libglog \
libglog_init \
libreact_debug \
libreact_render_mapbuffer
libreact_debug

include $(BUILD_SHARED_LIBRARY)

$(call import-module,react/debug)
$(call import-module,fbgloginit)
$(call import-module,glog)
$(call import-module,react/renderer/mapbuffer)
150 changes: 0 additions & 150 deletions ReactCommon/react/utils/LayoutManager.h

This file was deleted.

0 comments on commit f6901d0

Please sign in to comment.