Don't recreate the layout context on every measure #676
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Details
In the current implementation,
layoutContext
is created for every measure of the markdown text input. To construct the context, we need the font size multiplier, which should be read from the UI thread (which we cannot really do by the time we need the result).This PR updates the implementation to use a thread-local instance of the layout context, which is then updated inside
layout
method, which receives up-to-date info from React Native. A similar mechanism is used in the RN itself: https://github.com/facebook/react-native/blob/12b2b5610263cb145d1ade8eaf06d8a6e015b10e/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp#L63. This way, the font size multiplier is read only once.A potential downside to this approach is that when the multiplier changes, the layout context is updated after the measure, potentially leaving a single tree version where the input is measured with the wrong text scale. It's hard to confirm this, though, since React Native doesn't currently support responding to font scale changes correctly (it will be handled in 0.80 behind a feature flag).
Related Issues
GH_LINK
Manual Tests
Linked PRs