From efc7ef566a7edffd7a3777a788fafbdc23bb52fb Mon Sep 17 00:00:00 2001 From: workgroupengineering Date: Tue, 21 Nov 2023 23:52:31 +0100 Subject: [PATCH] perf: Cache the last matrix instead of iterating the children on each `TransformGroup.Value` property call (#13532) --- src/Avalonia.Base/Media/TransformGroup.cs | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/Avalonia.Base/Media/TransformGroup.cs b/src/Avalonia.Base/Media/TransformGroup.cs index 8f9ab67712..bce50a7e6b 100644 --- a/src/Avalonia.Base/Media/TransformGroup.cs +++ b/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(); } - /// /// Gets or sets the children. /// @@ -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; } }