Skip to content

Commit ca11efb

Browse files
committed
Merge pull request facebookarchive#951 from facebook/ASTextNodeRendererInvalidationEfficiency
[ASTextNode] Renderer should not be invalidated if bounds size matches calculatedSize, even if it doesn't match constrainedSize.
2 parents ed1c71d + 1872ac0 commit ca11efb

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

AsyncDisplayKit/ASTextNode.mm

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#import "ASInternalHelpers.h"
2525
#import "ASEqualityHelpers.h"
26+
#import "ASLayout.h"
2627

2728
static const NSTimeInterval ASTextNodeHighlightFadeOutDuration = 0.15;
2829
static const NSTimeInterval ASTextNodeHighlightFadeInDuration = 0.1;
@@ -290,7 +291,29 @@ - (void)_invalidateRendererIfNeeded:(CGSize)newSize
290291

291292
- (BOOL)_needInvalidateRenderer:(CGSize)newSize
292293
{
293-
return !CGSizeEqualToSize(newSize, _constrainedSize);
294+
if (!_renderer) {
295+
return YES;
296+
}
297+
298+
// If the size is not the same as the constraint we provided to the renderer, start out assuming we need
299+
// a new one. However, there are common cases where the constrained size doesn't need to be the same as calculated.
300+
CGSize oldSize = _renderer.constrainedSize;
301+
302+
if (CGSizeEqualToSize(newSize, oldSize)) {
303+
return NO;
304+
} else {
305+
// It is very common to have a constrainedSize with a concrete, specific width but +Inf height.
306+
// In this case, as long as the text node has bounds as large as the full calculatedLayout suggests,
307+
// it means that the text has all the room it needs (as it was not vertically bounded). So, we will not
308+
// experience truncation and don't need to recreate the renderer with the size it already calculated,
309+
// as this would essentially serve to set its constrainedSize to be its calculatedSize (unnecessary).
310+
ASLayout *layout = self.calculatedLayout;
311+
if (layout != nil && CGSizeEqualToSize(newSize, layout.size)) {
312+
return NO;
313+
} else {
314+
return YES;
315+
}
316+
}
294317
}
295318

296319
#pragma mark - Modifying User Text

0 commit comments

Comments
 (0)