Fixed: Performance improvement on visibleRect when updating tracking areas (#2881)

Calculations of the visible rect can be a heavy operation as it depends of the superviews
visible rect. They have to be transformed and intersected together to create the result.
This calculation is a recursive operation that travels up the view hierarchy all the way
to the top.

Updating the tracking areas is also a recursive operation. But it travels down the view hierarchy
instead. It will calculate the new tracking area using the visible rect for the current view. For
each view it updates it will get the visible rect that has to travel all the way up the view
hierarchy to calculate it by transform and intersect them together. This results in that the visible
rect will be calculated for each view multible times. This makes it a very slow operation, specially
for deeper view hierarchies.

This fix will send along the visible rect for the superview when traveling down the
view hierarchy. This means that the visible rect does not need to travel up the view hierarchy
to be calculated. It will just need transform and intersect the provided rect for its superview
with the one from itself.
This commit is contained in:
Martin Carlberg authored and GitHub committed 2020-01-06 12:48:40 +01:00
1 parent 36f1abdbe6
commit 109003ae12
2 files changed
+34 -5

No files matched your search

+10 -1
View File
@@ -126,7 +126,16 @@ CPTrackingOwnerImplementsCursorUpdate = 1 << 4;
- (void)_updateWindowRect
{
_windowRect = [_referencingView convertRect:((_options & CPTrackingInVisibleRect) ? [_referencingView visibleRect] : _viewRect) toView:[[_referencingView window] _windowView]];
[self _updateWindowRectWithReferencingSuperViewVisibleRect:nil];
}
/*!
To speed up things we allow the caller to pass the visible rect of the referencing view's superview.
This allows the visible rect calculations to be optimized when traveling down the view hierarchy.
*/
- (void)_updateWindowRectWithReferencingSuperViewVisibleRect:(CGRect)referencingSuperviewVisibleRect
{
_windowRect = [_referencingView convertRect:((_options & CPTrackingInVisibleRect) ? [_referencingView _visibleRectWithSuperviewVisibleRect:referencingSuperviewVisibleRect] : _viewRect) toView:[[_referencingView window] _windowView]];
}
@end
+24 -4
View File
@@ -2857,11 +2857,16 @@ setBoundsOrigin:
Returns the rectangle of the receiver not clipped by its superview.
*/
- (CGRect)visibleRect
{
return [self _visibleRectWithSuperviewVisibleRect:nil];
}
- (CGRect)_visibleRectWithSuperviewVisibleRect:(CGRect)superviewVisibleRect
{
if (!_superview)
return _bounds;
return CGRectIntersection([self convertRect:[_superview visibleRect] fromView:_superview], _bounds);
return CGRectIntersection([self convertRect:superviewVisibleRect || [_superview _visibleRectWithSuperviewVisibleRect:nil] fromView:_superview], _bounds);
}
// Scrolling
@@ -3657,19 +3662,34 @@ var CPAppearanceVibrantDark = [CPAppearance appearanceNamed:CPAppearanceNameVibr
- (void)_updateTrackingAreasWithRecursion:(BOOL)shouldCallRecursively
{
_inhibitUpdateTrackingAreas = YES;
[self _updateTrackingAreasWithRecursion:shouldCallRecursively withReferencingSuperViewVisibleRect:[_superview visibleRect]];
}
[self _updateTrackingAreasForOwners:[self _calcTrackingAreaOwners]];
/*!
The referencingSuperViewVisibleRect is used to speed up the execution of this as the visibleRect method is a heavy operation.
It has to go up the view hierarchy and get every superviews visibleRect to transform and intersect them together.
Here we keep the superviews visible rect when we are going down the view hierarchy to make the operation much faster.
*/
- (void)_updateTrackingAreasWithRecursion:(BOOL)shouldCallRecursively withReferencingSuperViewVisibleRect:(CGRect)referencingSuperViewVisibleRect
{
[self _updateTrackingAreasForOwners:[self _calcTrackingAreaOwnersWithReferencingSuperViewVisibleRect:referencingSuperViewVisibleRect]];
if (shouldCallRecursively)
{
// Now, call _updateTrackingAreasWithRecursion on subviews
for (var i = 0; i < _subviews.length; i++)
[_subviews[i] _updateTrackingAreasWithRecursion:YES];
[_subviews[i] _updateTrackingAreasWithRecursion:YES withReferencingSuperViewVisibleRect:[self _visibleRectWithSuperviewVisibleRect:referencingSuperViewVisibleRect]];
}
}
- (CPArray)_calcTrackingAreaOwners
{
return [self _calcTrackingAreaOwnersWithReferencingSuperViewVisibleRect:nil];
}
- (CPArray)_calcTrackingAreaOwnersWithReferencingSuperViewVisibleRect:(CGRect)referencingSuperViewVisibleRect
{
// First search all owners that must be notified
// Remark: 99.99% of time, the only owner will be the view itself
@@ -3682,7 +3702,7 @@ var CPAppearanceVibrantDark = [CPAppearance appearanceNamed:CPAppearanceNameVibr
var trackingArea = _trackingAreas[i];
if ([trackingArea options] & CPTrackingInVisibleRect)
[trackingArea _updateWindowRect];
[trackingArea _updateWindowRectWithReferencingSuperViewVisibleRect:referencingSuperViewVisibleRect];
else
{