Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
import com.facebook.react.uimanager.events.RCTEventEmitter;
import com.facebook.react.uimanager.events.SynchronousEventReceiver;
import com.facebook.react.views.text.PreparedLayout;
import com.facebook.react.views.text.PreparedLayoutCache;
import com.facebook.react.views.text.ReactTextViewManager;
import com.facebook.react.views.text.ReactTextViewManagerCallback;
import com.facebook.react.views.text.ReactTypefaceUtils;
Expand Down Expand Up @@ -186,6 +187,7 @@ public class FabricUIManager
private final ViewManagerRegistry mViewManagerRegistry;

private final TextEffectRegistry mTextEffectRegistry = new TextEffectRegistry();
private volatile @Nullable PreparedLayoutCache mPreparedLayoutCache;

private final BatchEventDispatchedListener mBatchEventDispatchedListener;

Expand Down Expand Up @@ -504,6 +506,10 @@ public void invalidate() {
mDestroyed = true;
// Before the teardown hooks below, which can throw and would otherwise leak the context.
TextLayoutManager.destroySpannableCache(mReactApplicationContext);
PreparedLayoutCache preparedLayoutCache = mPreparedLayoutCache;
if (preparedLayoutCache != null) {
preparedLayoutCache.clear();
}

mEventDispatcher.removeBatchEventDispatchedListener(mBatchEventDispatchedListener);
mEventDispatcher.invalidate();
Expand Down Expand Up @@ -748,6 +754,77 @@ public float[] measurePreparedLayout(
getYogaMeasureMode(minHeight, maxHeight));
}

private PreparedLayoutCache getOrCreatePreparedLayoutCache() {
PreparedLayoutCache cache = mPreparedLayoutCache;
if (cache == null) {
synchronized (this) {
cache = mPreparedLayoutCache;
if (cache == null) {
cache =
new PreparedLayoutCache(
Math.max(0, (int) ReactNativeFeatureFlags.preparedTextCacheSize()));
mPreparedLayoutCache = cache;
}
}
}
return cache;
}

@AnyThread
@ThreadConfined(ANY)
private PreparedLayout prepareTextLayoutAndCache(
long cacheId,
ReadableMapBuffer attributedString,
ReadableMapBuffer paragraphAttributes,
float minWidth,
float maxWidth,
float minHeight,
float maxHeight,
@Nullable int[] reactTags) {
PreparedLayout preparedLayout =
prepareTextLayout(
attributedString, paragraphAttributes, minWidth, maxWidth, minHeight, maxHeight);
getOrCreatePreparedLayoutCache().put(cacheId, preparedLayout);
return reactTags != null ? preparedLayout.withReactTags(reactTags) : preparedLayout;
}

@AnyThread
@ThreadConfined(ANY)
private float[] prepareAndMeasureTextLayoutToCache(
long cacheId,
ReadableMapBuffer attributedString,
ReadableMapBuffer paragraphAttributes,
float minWidth,
float maxWidth,
float minHeight,
float maxHeight) {
PreparedLayout preparedLayout =
prepareTextLayout(
attributedString, paragraphAttributes, minWidth, maxWidth, minHeight, maxHeight);
getOrCreatePreparedLayoutCache().put(cacheId, preparedLayout);
return measurePreparedLayout(preparedLayout, minWidth, maxWidth, minHeight, maxHeight);
}

@AnyThread
@ThreadConfined(ANY)
private @Nullable float[] measurePreparedLayoutById(
long cacheId, float minWidth, float maxWidth, float minHeight, float maxHeight) {
PreparedLayoutCache cache = mPreparedLayoutCache;
PreparedLayout preparedLayout = cache != null ? cache.get(cacheId) : null;
if (preparedLayout == null) {
return null;
}
return measurePreparedLayout(preparedLayout, minWidth, maxWidth, minHeight, maxHeight);
}

@AnyThread
@ThreadConfined(ANY)
private @Nullable PreparedLayout getPreparedLayoutFromCache(
long cacheId, @Nullable int[] reactTags) {
PreparedLayoutCache cache = mPreparedLayoutCache;
return cache != null ? cache.getWithReactTags(cacheId, reactTags) : null;
}

@UnstableReactNativeAPI
public TextEffectRegistry getTextEffectRegistry() {
return mTextEffectRegistry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<3c2494d2482efe3dded741dd746d4309>>
* @generated SignedSource<<bece3cb310e4acba1e10d806da97c6ce>>
*/

/**
Expand Down Expand Up @@ -48,6 +48,12 @@ public object ReactNativeFeatureFlags {
@JvmStatic
public fun cxxNativeAnimatedEnabled(): Boolean = accessor.cxxNativeAnimatedEnabled()

/**
* When enabled, cached PreparedLayout objects are shared across ParagraphShadowNodes without creating new JNI global references for differing React tags; new React tags are attached via a local JNI reference on mount.
*/
@JvmStatic
public fun deferPreparedLayoutReactTagsToMountAndroid(): Boolean = accessor.deferPreparedLayoutReactTagsToMountAndroid()

/**
* When enabled, iOS TextInput applies edits that JS transforms (e.g. maxLength truncation) incrementally after the UIKit text change delegate call returns, avoiding synchronous text and selection rewrites that can deadlock UIKeyboardTaskQueue.
*/
Expand Down Expand Up @@ -582,6 +588,12 @@ public object ReactNativeFeatureFlags {
@JvmStatic
public fun useFabricInterop(): Boolean = accessor.useFabricInterop()

/**
* When enabled, PreparedLayout instances are cached in a bounded Java-side LRU cache keyed by opaque ID rather than held via JNI global references from C++.
*/
@JvmStatic
public fun useJavaPreparedLayoutCacheAndroid(): Boolean = accessor.useJavaPreparedLayoutCacheAndroid()

/**
* When enabled, the native view configs are used in bridgeless mode.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<14682373d73e29327ec3c39882e59af5>>
* @generated SignedSource<<d66435bdf0ee0eb6428c60c9aa5223f4>>
*/

/**
Expand All @@ -23,6 +23,7 @@ internal class ReactNativeFeatureFlagsCxxAccessor : ReactNativeFeatureFlagsAcces
private var commonTestFlagCache: Boolean? = null
private var cdpInteractionMetricsEnabledCache: Boolean? = null
private var cxxNativeAnimatedEnabledCache: Boolean? = null
private var deferPreparedLayoutReactTagsToMountAndroidCache: Boolean? = null
private var deferTextInputTruncationIOSCache: Boolean? = null
private var disableEarlyViewCommandExecutionCache: Boolean? = null
private var disableIdleEventDispatchFrameCallbackRearmAndroidCache: Boolean? = null
Expand Down Expand Up @@ -112,6 +113,7 @@ internal class ReactNativeFeatureFlagsCxxAccessor : ReactNativeFeatureFlagsAcces
private var updateRuntimeShadowNodeReferencesOnCommitThreadCache: Boolean? = null
private var useAlwaysAvailableJSErrorHandlingCache: Boolean? = null
private var useFabricInteropCache: Boolean? = null
private var useJavaPreparedLayoutCacheAndroidCache: Boolean? = null
private var useNativeViewConfigsInBridgelessModeCache: Boolean? = null
private var useNestedScrollViewAndroidCache: Boolean? = null
private var useSharedAnimatedBackendCache: Boolean? = null
Expand Down Expand Up @@ -150,6 +152,15 @@ internal class ReactNativeFeatureFlagsCxxAccessor : ReactNativeFeatureFlagsAcces
return cached
}

override fun deferPreparedLayoutReactTagsToMountAndroid(): Boolean {
var cached = deferPreparedLayoutReactTagsToMountAndroidCache
if (cached == null) {
cached = ReactNativeFeatureFlagsCxxInterop.deferPreparedLayoutReactTagsToMountAndroid()
deferPreparedLayoutReactTagsToMountAndroidCache = cached
}
return cached
}

override fun deferTextInputTruncationIOS(): Boolean {
var cached = deferTextInputTruncationIOSCache
if (cached == null) {
Expand Down Expand Up @@ -951,6 +962,15 @@ internal class ReactNativeFeatureFlagsCxxAccessor : ReactNativeFeatureFlagsAcces
return cached
}

override fun useJavaPreparedLayoutCacheAndroid(): Boolean {
var cached = useJavaPreparedLayoutCacheAndroidCache
if (cached == null) {
cached = ReactNativeFeatureFlagsCxxInterop.useJavaPreparedLayoutCacheAndroid()
useJavaPreparedLayoutCacheAndroidCache = cached
}
return cached
}

override fun useNativeViewConfigsInBridgelessMode(): Boolean {
var cached = useNativeViewConfigsInBridgelessModeCache
if (cached == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<bf090f6eea05b190d2f1ec51e5a0643a>>
* @generated SignedSource<<e7b75048ea5d431657ccfdc93b7afce0>>
*/

/**
Expand Down Expand Up @@ -34,6 +34,8 @@ public object ReactNativeFeatureFlagsCxxInterop {

@DoNotStrip @JvmStatic public external fun cxxNativeAnimatedEnabled(): Boolean

@DoNotStrip @JvmStatic public external fun deferPreparedLayoutReactTagsToMountAndroid(): Boolean

@DoNotStrip @JvmStatic public external fun deferTextInputTruncationIOS(): Boolean

@DoNotStrip @JvmStatic public external fun disableEarlyViewCommandExecution(): Boolean
Expand Down Expand Up @@ -212,6 +214,8 @@ public object ReactNativeFeatureFlagsCxxInterop {

@DoNotStrip @JvmStatic public external fun useFabricInterop(): Boolean

@DoNotStrip @JvmStatic public external fun useJavaPreparedLayoutCacheAndroid(): Boolean

@DoNotStrip @JvmStatic public external fun useNativeViewConfigsInBridgelessMode(): Boolean

@DoNotStrip @JvmStatic public external fun useNestedScrollViewAndroid(): Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<d7d3d71a9022be2e753263699b3e2ec0>>
* @generated SignedSource<<c37dbce2b33b0c308c264808ad889c24>>
*/

/**
Expand All @@ -29,6 +29,8 @@ public open class ReactNativeFeatureFlagsDefaults : ReactNativeFeatureFlagsProvi

override fun cxxNativeAnimatedEnabled(): Boolean = true

override fun deferPreparedLayoutReactTagsToMountAndroid(): Boolean = false

override fun deferTextInputTruncationIOS(): Boolean = false

override fun disableEarlyViewCommandExecution(): Boolean = false
Expand Down Expand Up @@ -207,6 +209,8 @@ public open class ReactNativeFeatureFlagsDefaults : ReactNativeFeatureFlagsProvi

override fun useFabricInterop(): Boolean = true

override fun useJavaPreparedLayoutCacheAndroid(): Boolean = false

override fun useNativeViewConfigsInBridgelessMode(): Boolean = false

override fun useNestedScrollViewAndroid(): Boolean = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<d09180773543b2c01eeeced9ec2205a7>>
* @generated SignedSource<<74fa4b118323e2c7bc3906229e1036a8>>
*/

/**
Expand All @@ -27,6 +27,7 @@ internal class ReactNativeFeatureFlagsLocalAccessor : ReactNativeFeatureFlagsAcc
private var commonTestFlagCache: Boolean? = null
private var cdpInteractionMetricsEnabledCache: Boolean? = null
private var cxxNativeAnimatedEnabledCache: Boolean? = null
private var deferPreparedLayoutReactTagsToMountAndroidCache: Boolean? = null
private var deferTextInputTruncationIOSCache: Boolean? = null
private var disableEarlyViewCommandExecutionCache: Boolean? = null
private var disableIdleEventDispatchFrameCallbackRearmAndroidCache: Boolean? = null
Expand Down Expand Up @@ -116,6 +117,7 @@ internal class ReactNativeFeatureFlagsLocalAccessor : ReactNativeFeatureFlagsAcc
private var updateRuntimeShadowNodeReferencesOnCommitThreadCache: Boolean? = null
private var useAlwaysAvailableJSErrorHandlingCache: Boolean? = null
private var useFabricInteropCache: Boolean? = null
private var useJavaPreparedLayoutCacheAndroidCache: Boolean? = null
private var useNativeViewConfigsInBridgelessModeCache: Boolean? = null
private var useNestedScrollViewAndroidCache: Boolean? = null
private var useSharedAnimatedBackendCache: Boolean? = null
Expand Down Expand Up @@ -157,6 +159,16 @@ internal class ReactNativeFeatureFlagsLocalAccessor : ReactNativeFeatureFlagsAcc
return cached
}

override fun deferPreparedLayoutReactTagsToMountAndroid(): Boolean {
var cached = deferPreparedLayoutReactTagsToMountAndroidCache
if (cached == null) {
cached = currentProvider.deferPreparedLayoutReactTagsToMountAndroid()
accessedFeatureFlags.add("deferPreparedLayoutReactTagsToMountAndroid")
deferPreparedLayoutReactTagsToMountAndroidCache = cached
}
return cached
}

override fun deferTextInputTruncationIOS(): Boolean {
var cached = deferTextInputTruncationIOSCache
if (cached == null) {
Expand Down Expand Up @@ -1047,6 +1059,16 @@ internal class ReactNativeFeatureFlagsLocalAccessor : ReactNativeFeatureFlagsAcc
return cached
}

override fun useJavaPreparedLayoutCacheAndroid(): Boolean {
var cached = useJavaPreparedLayoutCacheAndroidCache
if (cached == null) {
cached = currentProvider.useJavaPreparedLayoutCacheAndroid()
accessedFeatureFlags.add("useJavaPreparedLayoutCacheAndroid")
useJavaPreparedLayoutCacheAndroidCache = cached
}
return cached
}

override fun useNativeViewConfigsInBridgelessMode(): Boolean {
var cached = useNativeViewConfigsInBridgelessModeCache
if (cached == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<bedaafe067d7c527f3c3d7b834f31888>>
* @generated SignedSource<<cd5fa60023a09024d6689e4ea07b0230>>
*/

/**
Expand All @@ -29,6 +29,8 @@ public interface ReactNativeFeatureFlagsProvider {

@DoNotStrip public fun cxxNativeAnimatedEnabled(): Boolean

@DoNotStrip public fun deferPreparedLayoutReactTagsToMountAndroid(): Boolean

@DoNotStrip public fun deferTextInputTruncationIOS(): Boolean

@DoNotStrip public fun disableEarlyViewCommandExecution(): Boolean
Expand Down Expand Up @@ -207,6 +209,8 @@ public interface ReactNativeFeatureFlagsProvider {

@DoNotStrip public fun useFabricInterop(): Boolean

@DoNotStrip public fun useJavaPreparedLayoutCacheAndroid(): Boolean

@DoNotStrip public fun useNativeViewConfigsInBridgelessMode(): Boolean

@DoNotStrip public fun useNestedScrollViewAndroid(): Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,21 @@ internal class PreparedLayout(
// (truncated) text; every caller must consciously supply the complete string so accessibility
// and view-hierarchy consumers never fall back to the truncated text.
val sourceText: CharSequence,
)
) {
@DoNotStrip
fun withReactTags(reactTags: IntArray): PreparedLayout {
if (this.reactTags.contentEquals(reactTags)) {
return this
}
return PreparedLayout(
layout,
maximumNumberOfLines,
verticalOffset,
reactTags,
textBreakStrategy,
justificationMode,
baseTextAttributes,
sourceText,
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.react.views.text

import android.util.LruCache

internal class PreparedLayoutCache(maxSize: Int) {
private val cache: LruCache<Long, PreparedLayout>? = if (maxSize > 0) LruCache(maxSize) else null

fun put(cacheId: Long, layout: PreparedLayout) {
cache?.put(cacheId, layout)
}

fun get(cacheId: Long): PreparedLayout? = cache?.get(cacheId)

fun getWithReactTags(cacheId: Long, reactTags: IntArray?): PreparedLayout? {
val preparedLayout = get(cacheId) ?: return null
return if (reactTags != null) preparedLayout.withReactTags(reactTags) else preparedLayout
}

fun clear() {
cache?.evictAll()
}
}
Loading
Loading