Browse Source

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

workgroupengineering 2 years ago
parent
commit
efc7ef566a
1 changed files with 14 additions and 8 deletions
  1. 14 8
      src/Avalonia.Base/Media/TransformGroup.cs

+ 14 - 8
src/Avalonia.Base/Media/TransformGroup.cs

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