From 0254e3e97f1b9a2cfaf6c3bc4a20b34322108931 Mon Sep 17 00:00:00 2001 From: Dustin Shahidehpour Date: Fri, 14 Oct 2016 14:49:42 -0700 Subject: [PATCH] Don't call sizeThatFits: if measure modes are exact. Summary: `sizeThatFits:` can be expensive, this optimizes our measuring function so that we do not call it if we know that we are going to use the exact height and width that were passed-in. Reviewed By: rnystrom Differential Revision: D4023715 fbshipit-source-id: dc02703b50bafd168ffab62ed98a7f6342100cc9 --- uikit/CSSLayout/UIView+CSSLayout.m | 50 ++++++++++++++++++------------ 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/uikit/CSSLayout/UIView+CSSLayout.m b/uikit/CSSLayout/UIView+CSSLayout.m index 8fc582c5..ca03cfc3 100644 --- a/uikit/CSSLayout/UIView+CSSLayout.m +++ b/uikit/CSSLayout/UIView+CSSLayout.m @@ -11,25 +11,6 @@ #import -static CSSSize _measure( - void *context, - float width, - CSSMeasureMode widthMode, - float height, - CSSMeasureMode heightMode) -{ - UIView *view = (__bridge UIView*) context; - CGSize sizeThatFits = [view sizeThatFits:(CGSize) { - .width = widthMode == CSSMeasureModeUndefined ? CGFLOAT_MAX : width, - .height = heightMode == CSSMeasureModeUndefined ? CGFLOAT_MAX : height, - }]; - - return (CSSSize) { - .width = widthMode == CSSMeasureModeExactly ? width : sizeThatFits.width, - .height = heightMode == CSSMeasureModeExactly ? height : sizeThatFits.height, - }; -} - static void _attachNodesRecursive(UIView *view); static void _updateFrameRecursive(UIView *view); @@ -223,6 +204,37 @@ static void _updateFrameRecursive(UIView *view); _updateFrameRecursive(self); } +#pragma mark - Private + +static CSSSize _measure( + void *context, + float width, + CSSMeasureMode widthMode, + float height, + CSSMeasureMode heightMode) +{ + const BOOL useExactWidth = (widthMode == CSSMeasureModeExactly); + const BOOL useExactHeight = (heightMode == CSSMeasureModeExactly); + + if (useExactHeight && useExactWidth) { + return (CSSSize) { + .width = width, + .height = height, + }; + } + + UIView *view = (__bridge UIView*) context; + const CGSize sizeThatFits = [view sizeThatFits:(CGSize) { + .width = widthMode == CSSMeasureModeUndefined ? CGFLOAT_MAX : width, + .height = heightMode == CSSMeasureModeUndefined ? CGFLOAT_MAX : height, + }]; + + return (CSSSize) { + .width = useExactWidth ? width : sizeThatFits.width, + .height = useExactHeight ? height : sizeThatFits.height, + }; +} + @end static void _attachNodesRecursive(UIView *view) {