Skip to content

Commit f276214

Browse files
sherginfacebook-github-bot
authored andcommitted
Fabric: Fixes in Android TextLayoutManager for better caching performance
Summary: The is how it works: * Text is a quite special component with special properties and constraints. Some of them are: It's expensive to measure (layout) text. It's expensive to measure and expensive to pass AttributedString via JNI. * When we measure text, we don't concerned about maximum height, only maximum height is important. (Even though theoretically, there are text layout systems that can balance these constraints (max height and width) trying to find a perfect result, we don't use such complex (and expensive) layout engines for building UIs). Yoga, as a flexbox engine, does not aware of such constraints, so it requests remeasuring of text components quite often, so we have an RN built-in text measure cache system just for text measurements that suit these constraints. This way when Yoga requests a text measuring, we always measure with `Inf` height and store that result in the cache. And when Yoga requests another measure with the same width but a different height we retrieve the value from the cache and then just clamp it. Changelog: [Internal] Fabric-specific internal change. Reviewed By: mdvacca Differential Revision: D26696637 fbshipit-source-id: f65b275d33c77073bc2359cbf0a741ddcf05d8d4
1 parent 5f1012c commit f276214

File tree

1 file changed

+8
-1
lines changed
  • ReactCommon/react/renderer/textlayoutmanager/platform/android/react/renderer/textlayoutmanager

1 file changed

+8
-1
lines changed

ReactCommon/react/renderer/textlayoutmanager/platform/android/react/renderer/textlayoutmanager/TextLayoutManager.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
#include "TextLayoutManager.h"
99

10+
#include <limits>
11+
1012
#include <react/jni/ReadableNativeMap.h>
1113
#include <react/renderer/attributedstring/conversions.h>
1214
#include <react/renderer/core/conversions.h>
@@ -29,12 +31,15 @@ TextMeasurement TextLayoutManager::measure(
2931
LayoutConstraints layoutConstraints) const {
3032
auto &attributedString = attributedStringBox.getValue();
3133

32-
return measureCache_.get(
34+
auto measurement = measureCache_.get(
3335
{attributedString, paragraphAttributes, layoutConstraints},
3436
[&](TextMeasureCacheKey const &key) {
3537
return doMeasure(
3638
attributedString, paragraphAttributes, layoutConstraints);
3739
});
40+
41+
measurement.size = layoutConstraints.clamp(measurement.size);
42+
return measurement;
3843
}
3944

4045
TextMeasurement TextLayoutManager::measureCachedSpannableById(
@@ -126,6 +131,8 @@ TextMeasurement TextLayoutManager::doMeasure(
126131
AttributedString attributedString,
127132
ParagraphAttributes paragraphAttributes,
128133
LayoutConstraints layoutConstraints) const {
134+
layoutConstraints.maximumSize.height = std::numeric_limits<Float>::infinity();
135+
129136
int attachmentsCount = 0;
130137
for (auto fragment : attributedString.getFragments()) {
131138
if (fragment.isAttachment()) {

0 commit comments

Comments
 (0)