Browse Source

perf: Cache the last matrix instead of iterating the children on each `TransformGroup.Value` property call (#13532)

pull/13688/head
workgroupengineering 2 years ago
committed by GitHub
parent
commit
efc7ef566a
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 22
      src/Avalonia.Base/Media/TransformGroup.cs

22
src/Avalonia.Base/Media/TransformGroup.cs

@ -14,16 +14,20 @@ namespace Avalonia.Media
private IDisposable? _childrenNotificationSubscription; private IDisposable? _childrenNotificationSubscription;
private readonly EventHandler _childTransformChangedHandler; private readonly EventHandler _childTransformChangedHandler;
private Matrix? _lastMatrix;
[System.Diagnostics.CodeAnalysis.SuppressMessage("AvaloniaProperty", "AVP1012", [System.Diagnostics.CodeAnalysis.SuppressMessage("AvaloniaProperty", "AVP1012",
Justification = "Collection properties shouldn't be set with SetCurrentValue.")] Justification = "Collection properties shouldn't be set with SetCurrentValue.")]
public TransformGroup() public TransformGroup()
{ {
_childTransformChangedHandler = (_, _) => RaiseChanged(); _childTransformChangedHandler = (_, _) =>
{
_lastMatrix = null;
RaiseChanged();
};
Children = new Transforms(); Children = new Transforms();
} }
/// <summary> /// <summary>
/// Gets or sets the children. /// Gets or sets the children.
/// </summary> /// </summary>
@ -44,14 +48,16 @@ namespace Avalonia.Media
{ {
get get
{ {
Matrix result = Matrix.Identity; if (_lastMatrix is null)
foreach (var t in Children)
{ {
result *= t.Value; var matrix = Matrix.Identity;
foreach (var t in Children)
{
matrix *= t.Value;
}
_lastMatrix = matrix;
} }
return _lastMatrix.Value;
return result;
} }
} }

Loading…
Cancel
Save