Reuse matrix instance for transient transforms in _transformRect for fewer allocations (#35132)

This commit is contained in:
Yegor 2019-06-27 10:05:25 -07:00 committed by GitHub
parent 9166337698
commit c5f5b3c98a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -3599,15 +3599,24 @@ class _SemanticsGeometry {
}
}
// A matrix used to store transient transform data.
//
// Reusing this matrix avoids allocating a new matrix every time a temporary
// matrix is needed.
//
// This instance should never be returned to the caller. Otherwise, the data
// stored in it will be overwritten unpredictably by subsequent reuses.
static final Matrix4 _temporaryTransformHolder = Matrix4.zero();
/// From parent to child coordinate system.
static Rect _transformRect(Rect rect, RenderObject parent, RenderObject child) {
if (rect == null)
return null;
if (rect.isEmpty)
return Rect.zero;
final Matrix4 transform = Matrix4.identity();
parent.applyPaintTransform(child, transform);
return MatrixUtils.inverseTransformRect(transform, rect);
_temporaryTransformHolder.setIdentity(); // clears data from a previous call
parent.applyPaintTransform(child, _temporaryTransformHolder);
return MatrixUtils.inverseTransformRect(_temporaryTransformHolder, rect);
}
static Rect _intersectRects(Rect a, Rect b) {