From 70ccc4d5754ec86989e8cb3b83905f302f5740b2 Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Thu, 13 Apr 2023 00:49:29 +0200 Subject: [PATCH] Reduced some more allocations --- RGB.NET.Core/Groups/ListLedGroup.cs | 14 ++++++++++++-- RGB.NET.Core/Update/Devices/DeviceUpdateTrigger.cs | 4 +++- RGB.NET.Core/Update/ManualUpdateTrigger.cs | 6 +++--- RGB.NET.Core/Update/TimerUpdateTrigger.cs | 5 +++-- RGB.NET.Presets/Groups/RectangleLedGroup.cs | 3 ++- 5 files changed, 23 insertions(+), 9 deletions(-) diff --git a/RGB.NET.Core/Groups/ListLedGroup.cs b/RGB.NET.Core/Groups/ListLedGroup.cs index c6f7d54..a97b6ec 100644 --- a/RGB.NET.Core/Groups/ListLedGroup.cs +++ b/RGB.NET.Core/Groups/ListLedGroup.cs @@ -15,6 +15,8 @@ public class ListLedGroup : AbstractLedGroup { #region Properties & Fields + private readonly ActionDisposable _unlockDisposable; + /// /// Gets the list containing the of this . /// @@ -31,7 +33,9 @@ public class ListLedGroup : AbstractLedGroup /// Specifies the surface to attach this group to or null if the group should not be attached on creation. public ListLedGroup(RGBSurface? surface) : base(surface) - { } + { + _unlockDisposable = new ActionDisposable(Unlock); + } /// /// @@ -42,6 +46,8 @@ public class ListLedGroup : AbstractLedGroup public ListLedGroup(RGBSurface? surface, IEnumerable leds) : base(surface) { + _unlockDisposable = new ActionDisposable(Unlock); + AddLeds(leds); } @@ -54,6 +60,8 @@ public class ListLedGroup : AbstractLedGroup public ListLedGroup(RGBSurface? surface, params Led[] leds) : base(surface) { + _unlockDisposable = new ActionDisposable(Unlock); + AddLeds(leds); } @@ -141,8 +149,10 @@ public class ListLedGroup : AbstractLedGroup { Monitor.Enter(GroupLeds); leds = GroupLeds; - return new ActionDisposable(() => Monitor.Exit(GroupLeds)); + return _unlockDisposable; } + private void Unlock() => Monitor.Exit(GroupLeds); + #endregion } \ No newline at end of file diff --git a/RGB.NET.Core/Update/Devices/DeviceUpdateTrigger.cs b/RGB.NET.Core/Update/Devices/DeviceUpdateTrigger.cs index db22aa4..00abcc7 100644 --- a/RGB.NET.Core/Update/Devices/DeviceUpdateTrigger.cs +++ b/RGB.NET.Core/Update/Devices/DeviceUpdateTrigger.cs @@ -157,11 +157,13 @@ public class DeviceUpdateTrigger : AbstractUpdateTrigger, IDeviceUpdateTrigger using (TimerHelper.RequestHighResolutionTimer()) while (!UpdateToken.IsCancellationRequested) if (HasDataEvent.WaitOne(Timeout)) - LastUpdateTime = TimerHelper.Execute(() => OnUpdate(), UpdateFrequency * 1000); + LastUpdateTime = TimerHelper.Execute(TimerExecute, UpdateFrequency * 1000); else if ((HeartbeatTimer > 0) && (LastUpdateTimestamp > 0) && (TimerHelper.GetElapsedTime(LastUpdateTimestamp) > HeartbeatTimer)) OnUpdate(new CustomUpdateData().Heartbeat()); } + private void TimerExecute() => OnUpdate(); + protected override void OnUpdate(CustomUpdateData? updateData = null) { base.OnUpdate(updateData); diff --git a/RGB.NET.Core/Update/ManualUpdateTrigger.cs b/RGB.NET.Core/Update/ManualUpdateTrigger.cs index 67e8ca4..5cd4235 100644 --- a/RGB.NET.Core/Update/ManualUpdateTrigger.cs +++ b/RGB.NET.Core/Update/ManualUpdateTrigger.cs @@ -83,12 +83,12 @@ public sealed class ManualUpdateTrigger : AbstractUpdateTrigger OnStartup(); while (!UpdateToken.IsCancellationRequested) - { if (_mutex.WaitOne(100)) - LastUpdateTime = TimerHelper.Execute(() => OnUpdate(_customUpdateData)); - } + LastUpdateTime = TimerHelper.Execute(TimerExecute); } + private void TimerExecute() => OnUpdate(_customUpdateData); + /// public override void Dispose() => Stop(); diff --git a/RGB.NET.Core/Update/TimerUpdateTrigger.cs b/RGB.NET.Core/Update/TimerUpdateTrigger.cs index 14a7e9f..9f2b2b6 100644 --- a/RGB.NET.Core/Update/TimerUpdateTrigger.cs +++ b/RGB.NET.Core/Update/TimerUpdateTrigger.cs @@ -131,10 +131,11 @@ public class TimerUpdateTrigger : AbstractUpdateTrigger using (TimerHelper.RequestHighResolutionTimer()) while (!UpdateToken.IsCancellationRequested) - LastUpdateTime = TimerHelper.Execute(() => OnUpdate(_customUpdateData), UpdateFrequency * 1000); - + LastUpdateTime = TimerHelper.Execute(TimerExecute, UpdateFrequency * 1000); } + private void TimerExecute() => OnUpdate(_customUpdateData); + /// public override void Dispose() { diff --git a/RGB.NET.Presets/Groups/RectangleLedGroup.cs b/RGB.NET.Presets/Groups/RectangleLedGroup.cs index d6cb650..cf66848 100644 --- a/RGB.NET.Presets/Groups/RectangleLedGroup.cs +++ b/RGB.NET.Presets/Groups/RectangleLedGroup.cs @@ -2,6 +2,7 @@ // ReSharper disable AutoPropertyCanBeMadeGetOnly.Global // ReSharper disable UnusedMember.Global +using System; using System.Collections.Generic; using System.Linq; using RGB.NET.Core; @@ -121,7 +122,7 @@ public class RectangleLedGroup : AbstractLedGroup /// Gets a list containing all of this . /// /// The list containing all of this . - public override IList ToList() => _ledCache ??= (Surface?.Leds.Where(led => led.AbsoluteBoundary.CalculateIntersectPercentage(Rectangle) >= MinOverlayPercentage).ToList() ?? new List()); + public override IList ToList() => _ledCache ??= ((IList?)Surface?.Leds.Where(led => led.AbsoluteBoundary.CalculateIntersectPercentage(Rectangle) >= MinOverlayPercentage).ToList() ?? Array.Empty()); private void InvalidateCache() => _ledCache = null;