From ef3998055d770f032caaf2e026b40e404a01abce Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Mon, 6 Sep 2021 01:06:49 +0200 Subject: [PATCH 1/4] Prevented unnecessary allocations --- RGB.NET.Core/RGBSurface.cs | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/RGB.NET.Core/RGBSurface.cs b/RGB.NET.Core/RGBSurface.cs index 6b9d726..f39ddc3 100644 --- a/RGB.NET.Core/RGBSurface.cs +++ b/RGB.NET.Core/RGBSurface.cs @@ -27,19 +27,12 @@ namespace RGB.NET.Core /// /// Gets a readonly list containing all loaded . /// - public IEnumerable Devices - { - get - { - lock (_devices) - return new ReadOnlyCollection(_devices); - } - } + public IReadOnlyCollection Devices { get; } /// /// Gets a readonly list containing all registered . /// - public IEnumerable UpdateTriggers => new ReadOnlyCollection(_updateTriggers); + public IReadOnlyCollection UpdateTriggers { get; } /// /// Gets a copy of the representing this . @@ -124,6 +117,9 @@ namespace RGB.NET.Core public RGBSurface() { _deltaTimeCounter = Stopwatch.StartNew(); + + Devices = new ReadOnlyCollection(_devices); + UpdateTriggers = new ReadOnlyCollection(_updateTriggers); } #endregion From ef12e402eaf53c4fbc6547db87a46adcac4e8323 Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Mon, 6 Sep 2021 01:12:36 +0200 Subject: [PATCH 2/4] Changed IReadOnlyCollections to IReadOnlyLists --- RGB.NET.Core/Decorators/AbstractDecorateable.cs | 2 +- RGB.NET.Core/Decorators/IDecoratable.cs | 2 +- RGB.NET.Core/Devices/AbstractRGBDeviceProvider.cs | 2 +- RGB.NET.Core/Devices/IRGBDeviceProvider.cs | 3 +-- RGB.NET.Core/RGBSurface.cs | 4 ++-- 5 files changed, 6 insertions(+), 7 deletions(-) diff --git a/RGB.NET.Core/Decorators/AbstractDecorateable.cs b/RGB.NET.Core/Decorators/AbstractDecorateable.cs index 50602ae..1120bc4 100644 --- a/RGB.NET.Core/Decorators/AbstractDecorateable.cs +++ b/RGB.NET.Core/Decorators/AbstractDecorateable.cs @@ -14,7 +14,7 @@ namespace RGB.NET.Core private readonly List _decorators = new(); /// - public IReadOnlyCollection Decorators { get; } + public IReadOnlyList Decorators { get; } #endregion diff --git a/RGB.NET.Core/Decorators/IDecoratable.cs b/RGB.NET.Core/Decorators/IDecoratable.cs index 2892638..ae8def2 100644 --- a/RGB.NET.Core/Decorators/IDecoratable.cs +++ b/RGB.NET.Core/Decorators/IDecoratable.cs @@ -20,7 +20,7 @@ namespace RGB.NET.Core /// /// Gets a readonly-list of all attached to this . /// - IReadOnlyCollection Decorators { get; } + IReadOnlyList Decorators { get; } /// /// Adds an to the . diff --git a/RGB.NET.Core/Devices/AbstractRGBDeviceProvider.cs b/RGB.NET.Core/Devices/AbstractRGBDeviceProvider.cs index 81ac6f2..6b4ee57 100644 --- a/RGB.NET.Core/Devices/AbstractRGBDeviceProvider.cs +++ b/RGB.NET.Core/Devices/AbstractRGBDeviceProvider.cs @@ -30,7 +30,7 @@ namespace RGB.NET.Core protected Dictionary UpdateTriggerMapping { get; } = new(); /// - public ReadOnlyCollection<(int id, IDeviceUpdateTrigger trigger)> UpdateTriggers => new(UpdateTriggerMapping.Select(x => (x.Key, x.Value)).ToList()); + public IReadOnlyList<(int id, IDeviceUpdateTrigger trigger)> UpdateTriggers => new ReadOnlyCollection<(int id, IDeviceUpdateTrigger trigger)>(UpdateTriggerMapping.Select(x => (x.Key, x.Value)).ToList()); #endregion diff --git a/RGB.NET.Core/Devices/IRGBDeviceProvider.cs b/RGB.NET.Core/Devices/IRGBDeviceProvider.cs index d2a8d1d..1193a65 100644 --- a/RGB.NET.Core/Devices/IRGBDeviceProvider.cs +++ b/RGB.NET.Core/Devices/IRGBDeviceProvider.cs @@ -2,7 +2,6 @@ using System; using System.Collections.Generic; -using System.Collections.ObjectModel; namespace RGB.NET.Core { @@ -31,7 +30,7 @@ namespace RGB.NET.Core /// /// Gets a collection registered to this device provider. /// - ReadOnlyCollection<(int id, IDeviceUpdateTrigger trigger)> UpdateTriggers { get; } + IReadOnlyList<(int id, IDeviceUpdateTrigger trigger)> UpdateTriggers { get; } #endregion diff --git a/RGB.NET.Core/RGBSurface.cs b/RGB.NET.Core/RGBSurface.cs index f39ddc3..b51766d 100644 --- a/RGB.NET.Core/RGBSurface.cs +++ b/RGB.NET.Core/RGBSurface.cs @@ -27,12 +27,12 @@ namespace RGB.NET.Core /// /// Gets a readonly list containing all loaded . /// - public IReadOnlyCollection Devices { get; } + public IReadOnlyList Devices { get; } /// /// Gets a readonly list containing all registered . /// - public IReadOnlyCollection UpdateTriggers { get; } + public IReadOnlyList UpdateTriggers { get; } /// /// Gets a copy of the representing this . From 924b121f6407926e016fc358d28417a6462d6a12 Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Mon, 6 Sep 2021 01:15:46 +0200 Subject: [PATCH 3/4] Fixed locks in RGBSurface --- RGB.NET.Core/RGBSurface.cs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/RGB.NET.Core/RGBSurface.cs b/RGB.NET.Core/RGBSurface.cs index b51766d..831eaf4 100644 --- a/RGB.NET.Core/RGBSurface.cs +++ b/RGB.NET.Core/RGBSurface.cs @@ -13,6 +13,7 @@ namespace RGB.NET.Core /// /// /// Represents a RGB-surface containing multiple devices. + /// Represents a RGB-surface containing multiple devices. /// public sealed class RGBSurface : AbstractBindable, IDisposable { @@ -46,7 +47,7 @@ namespace RGB.NET.Core { get { - lock (_devices) + lock (Devices) return _devices.SelectMany(x => x); } } @@ -142,8 +143,8 @@ namespace RGB.NET.Core bool render = customData["render"] as bool? ?? true; bool updateDevices = customData["updateDevices"] as bool? ?? true; - lock (_updateTriggers) - lock (_devices) + lock (UpdateTriggers) + lock (Devices) { OnUpdating(updateTrigger, customData); @@ -174,7 +175,7 @@ namespace RGB.NET.Core public void Dispose() { List devices; - lock (_devices) + lock (Devices) devices = new List(_devices); foreach (IRGBDevice device in devices) @@ -263,7 +264,7 @@ namespace RGB.NET.Core /// The to attach. public void Attach(IRGBDevice device) { - lock (_devices) + lock (Devices) { if (string.IsNullOrWhiteSpace(device.DeviceInfo.DeviceName)) throw new RGBDeviceException($"The device '{device.DeviceInfo.Manufacturer} {device.DeviceInfo.Model}' has no valid name."); if (device.Surface != null) throw new RGBSurfaceException($"The device '{device.DeviceInfo.DeviceName}' is already attached to a surface."); @@ -283,7 +284,7 @@ namespace RGB.NET.Core /// true if the could be detached; false otherwise. public void Detach(IRGBDevice device) { - lock (_devices) + lock (Devices) { if (!_devices.Contains(device)) throw new RGBSurfaceException($"The device '{device.DeviceInfo.DeviceName}' is not attached to this surface."); @@ -310,7 +311,7 @@ namespace RGB.NET.Core private void UpdateSurfaceRectangle() { - lock (_devices) + lock (Devices) { Rectangle devicesRectangle = new(_devices.Select(d => d.Boundary)); Boundary = Boundary.SetSize(new Size(devicesRectangle.Location.X + devicesRectangle.Size.Width, devicesRectangle.Location.Y + devicesRectangle.Size.Height)); From cdae699c8dc71e0f873289f425b642802502e427 Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Mon, 6 Sep 2021 01:18:16 +0200 Subject: [PATCH 4/4] Improved docs --- RGB.NET.Core/RGBSurface.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/RGB.NET.Core/RGBSurface.cs b/RGB.NET.Core/RGBSurface.cs index 831eaf4..3cebaa7 100644 --- a/RGB.NET.Core/RGBSurface.cs +++ b/RGB.NET.Core/RGBSurface.cs @@ -13,7 +13,6 @@ namespace RGB.NET.Core /// /// /// Represents a RGB-surface containing multiple devices. - /// Represents a RGB-surface containing multiple devices. /// public sealed class RGBSurface : AbstractBindable, IDisposable { @@ -27,11 +26,13 @@ namespace RGB.NET.Core /// /// Gets a readonly list containing all loaded . + /// This collection should be locked when enumerated in a multi-threaded application. /// public IReadOnlyList Devices { get; } /// /// Gets a readonly list containing all registered . + /// This collection should be locked when enumerated in a multi-threaded application. /// public IReadOnlyList UpdateTriggers { get; }