From 9f20f52b2212c913ebc7651495e467130f21f860 Mon Sep 17 00:00:00 2001 From: Diogo Trindade Date: Sat, 4 Feb 2023 19:27:49 +0000 Subject: [PATCH] Core - Reduced memory allocations --- src/Artemis.Core/MVVM/CorePropertyChanged.cs | 5 +++-- .../Plugins/LayerBrushes/PerLedLayerBrush.cs | 14 ++++++-------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/Artemis.Core/MVVM/CorePropertyChanged.cs b/src/Artemis.Core/MVVM/CorePropertyChanged.cs index 8ab50083a..5e7b6b417 100644 --- a/src/Artemis.Core/MVVM/CorePropertyChanged.cs +++ b/src/Artemis.Core/MVVM/CorePropertyChanged.cs @@ -1,4 +1,5 @@ -using System.ComponentModel; +using System.Collections.Generic; +using System.ComponentModel; using System.Runtime.CompilerServices; using Artemis.Core.Properties; @@ -26,7 +27,7 @@ public abstract class CorePropertyChanged : INotifyPropertyChanged [MethodImpl(MethodImplOptions.AggressiveInlining)] protected bool RequiresUpdate(ref T storage, T value) { - return !Equals(storage, value); + return !EqualityComparer.Default.Equals(storage, value); } /// diff --git a/src/Artemis.Core/Plugins/LayerBrushes/PerLedLayerBrush.cs b/src/Artemis.Core/Plugins/LayerBrushes/PerLedLayerBrush.cs index e1623b3fc..f0ace6a43 100644 --- a/src/Artemis.Core/Plugins/LayerBrushes/PerLedLayerBrush.cs +++ b/src/Artemis.Core/Plugins/LayerBrushes/PerLedLayerBrush.cs @@ -28,6 +28,8 @@ public abstract class PerLedLayerBrush : PropertiesLayerBrush where T : La /// The color the LED will receive public abstract SKColor GetColor(ArtemisLed led, SKPoint renderPoint); + private readonly SKPoint[] _points = new SKPoint[2]; + internal override void InternalRender(SKCanvas canvas, SKRect bounds, SKPaint paint) { // We don't want rotation on this canvas because that'll displace the LEDs, translations are applied to the points of each LED instead @@ -37,25 +39,21 @@ public abstract class PerLedLayerBrush : PropertiesLayerBrush where T : La using SKPath pointsPath = new(); foreach (ArtemisLed artemisLed in Layer.Leds) { - pointsPath.AddPoly(new[] - { - new SKPoint(0, 0), - new SKPoint(artemisLed.AbsoluteRectangle.Left - Layer.Bounds.Left, artemisLed.AbsoluteRectangle.Top - Layer.Bounds.Top) - }); + _points[0] = new SKPoint(0, 0); + _points[1] = new SKPoint(artemisLed.AbsoluteRectangle.Left - Layer.Bounds.Left, artemisLed.AbsoluteRectangle.Top - Layer.Bounds.Top); + pointsPath.AddPoly(_points); } // Apply the translation to the points of each LED instead if (Layer.General.TransformMode.CurrentValue == LayerTransformMode.Normal && SupportsTransformation) pointsPath.Transform(Layer.GetTransformMatrix(true, true, true, true).Invert()); - SKPoint[] points = pointsPath.Points; - TryOrBreak(() => { for (int index = 0; index < Layer.Leds.Count; index++) { ArtemisLed artemisLed = Layer.Leds[index]; - SKPoint renderPoint = points[index * 2 + 1]; + SKPoint renderPoint = pointsPath.GetPoint(index * 2 + 1); if (!float.IsFinite(renderPoint.X) || !float.IsFinite(renderPoint.Y)) continue;