1
0
mirror of https://github.com/DarthAffe/RGB.NET.git synced 2025-12-13 10:08:31 +00:00

Reduced some more allocations

This commit is contained in:
Darth Affe 2023-04-13 00:49:29 +02:00
parent 0cf4f39ccf
commit 70ccc4d575
5 changed files with 23 additions and 9 deletions

View File

@ -15,6 +15,8 @@ public class ListLedGroup : AbstractLedGroup
{ {
#region Properties & Fields #region Properties & Fields
private readonly ActionDisposable _unlockDisposable;
/// <summary> /// <summary>
/// Gets the list containing the <see cref="Led"/> of this <see cref="ListLedGroup"/>. /// Gets the list containing the <see cref="Led"/> of this <see cref="ListLedGroup"/>.
/// </summary> /// </summary>
@ -31,7 +33,9 @@ public class ListLedGroup : AbstractLedGroup
/// <param name="surface">Specifies the surface to attach this group to or <c>null</c> if the group should not be attached on creation.</param> /// <param name="surface">Specifies the surface to attach this group to or <c>null</c> if the group should not be attached on creation.</param>
public ListLedGroup(RGBSurface? surface) public ListLedGroup(RGBSurface? surface)
: base(surface) : base(surface)
{ } {
_unlockDisposable = new ActionDisposable(Unlock);
}
/// <inheritdoc /> /// <inheritdoc />
/// <summary> /// <summary>
@ -42,6 +46,8 @@ public class ListLedGroup : AbstractLedGroup
public ListLedGroup(RGBSurface? surface, IEnumerable<Led> leds) public ListLedGroup(RGBSurface? surface, IEnumerable<Led> leds)
: base(surface) : base(surface)
{ {
_unlockDisposable = new ActionDisposable(Unlock);
AddLeds(leds); AddLeds(leds);
} }
@ -54,6 +60,8 @@ public class ListLedGroup : AbstractLedGroup
public ListLedGroup(RGBSurface? surface, params Led[] leds) public ListLedGroup(RGBSurface? surface, params Led[] leds)
: base(surface) : base(surface)
{ {
_unlockDisposable = new ActionDisposable(Unlock);
AddLeds(leds); AddLeds(leds);
} }
@ -141,8 +149,10 @@ public class ListLedGroup : AbstractLedGroup
{ {
Monitor.Enter(GroupLeds); Monitor.Enter(GroupLeds);
leds = GroupLeds; leds = GroupLeds;
return new ActionDisposable(() => Monitor.Exit(GroupLeds)); return _unlockDisposable;
} }
private void Unlock() => Monitor.Exit(GroupLeds);
#endregion #endregion
} }

View File

@ -157,11 +157,13 @@ public class DeviceUpdateTrigger : AbstractUpdateTrigger, IDeviceUpdateTrigger
using (TimerHelper.RequestHighResolutionTimer()) using (TimerHelper.RequestHighResolutionTimer())
while (!UpdateToken.IsCancellationRequested) while (!UpdateToken.IsCancellationRequested)
if (HasDataEvent.WaitOne(Timeout)) 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)) else if ((HeartbeatTimer > 0) && (LastUpdateTimestamp > 0) && (TimerHelper.GetElapsedTime(LastUpdateTimestamp) > HeartbeatTimer))
OnUpdate(new CustomUpdateData().Heartbeat()); OnUpdate(new CustomUpdateData().Heartbeat());
} }
private void TimerExecute() => OnUpdate();
protected override void OnUpdate(CustomUpdateData? updateData = null) protected override void OnUpdate(CustomUpdateData? updateData = null)
{ {
base.OnUpdate(updateData); base.OnUpdate(updateData);

View File

@ -83,12 +83,12 @@ public sealed class ManualUpdateTrigger : AbstractUpdateTrigger
OnStartup(); OnStartup();
while (!UpdateToken.IsCancellationRequested) while (!UpdateToken.IsCancellationRequested)
{
if (_mutex.WaitOne(100)) if (_mutex.WaitOne(100))
LastUpdateTime = TimerHelper.Execute(() => OnUpdate(_customUpdateData)); LastUpdateTime = TimerHelper.Execute(TimerExecute);
}
} }
private void TimerExecute() => OnUpdate(_customUpdateData);
/// <inheritdoc /> /// <inheritdoc />
public override void Dispose() => Stop(); public override void Dispose() => Stop();

View File

@ -131,10 +131,11 @@ public class TimerUpdateTrigger : AbstractUpdateTrigger
using (TimerHelper.RequestHighResolutionTimer()) using (TimerHelper.RequestHighResolutionTimer())
while (!UpdateToken.IsCancellationRequested) while (!UpdateToken.IsCancellationRequested)
LastUpdateTime = TimerHelper.Execute(() => OnUpdate(_customUpdateData), UpdateFrequency * 1000); LastUpdateTime = TimerHelper.Execute(TimerExecute, UpdateFrequency * 1000);
} }
private void TimerExecute() => OnUpdate(_customUpdateData);
/// <inheritdoc /> /// <inheritdoc />
public override void Dispose() public override void Dispose()
{ {

View File

@ -2,6 +2,7 @@
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global // ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
// ReSharper disable UnusedMember.Global // ReSharper disable UnusedMember.Global
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using RGB.NET.Core; using RGB.NET.Core;
@ -121,7 +122,7 @@ public class RectangleLedGroup : AbstractLedGroup
/// Gets a list containing all <see cref="T:RGB.NET.Core.Led" /> of this <see cref="T:RGB.NET.Presets.Groups.RectangleLedGroup" />. /// Gets a list containing all <see cref="T:RGB.NET.Core.Led" /> of this <see cref="T:RGB.NET.Presets.Groups.RectangleLedGroup" />.
/// </summary> /// </summary>
/// <returns>The list containing all <see cref="T:RGB.NET.Core.Led" /> of this <see cref="T:RGB.NET.Presets.Groups.RectangleLedGroup" />.</returns> /// <returns>The list containing all <see cref="T:RGB.NET.Core.Led" /> of this <see cref="T:RGB.NET.Presets.Groups.RectangleLedGroup" />.</returns>
public override IList<Led> ToList() => _ledCache ??= (Surface?.Leds.Where(led => led.AbsoluteBoundary.CalculateIntersectPercentage(Rectangle) >= MinOverlayPercentage).ToList() ?? new List<Led>()); public override IList<Led> ToList() => _ledCache ??= ((IList<Led>?)Surface?.Leds.Where(led => led.AbsoluteBoundary.CalculateIntersectPercentage(Rectangle) >= MinOverlayPercentage).ToList() ?? Array.Empty<Led>());
private void InvalidateCache() => _ledCache = null; private void InvalidateCache() => _ledCache = null;