diff --git a/RGB.NET.Core/Devices/AbstractRGBDevice.cs b/RGB.NET.Core/Devices/AbstractRGBDevice.cs index 596625c..fc18794 100644 --- a/RGB.NET.Core/Devices/AbstractRGBDevice.cs +++ b/RGB.NET.Core/Devices/AbstractRGBDevice.cs @@ -3,6 +3,7 @@ // ReSharper disable AutoPropertyCanBeMadeGetOnly.Global using System; +using System.Buffers; using System.Collections; using System.Collections.Generic; using System.Linq; @@ -100,12 +101,7 @@ public abstract class AbstractRGBDevice : Placeable, IRGBDevice ledsToUpdate = GetLedsToUpdate(flushLeds).ToList(); - - foreach (Led led in ledsToUpdate) - led.Update(); - - UpdateLeds(ledsToUpdate); + UpdateLeds(GetLedsToUpdate(flushLeds)); } /// @@ -124,37 +120,38 @@ public abstract class AbstractRGBDevice : Placeable, IRGBDevice /// The enumerable of leds to convert. /// The enumerable of custom data and color tuples for the specified leds. - protected virtual IEnumerable<(object key, Color color)> GetUpdateData(IEnumerable leds) + protected (object key, Color color) GetUpdateData(Led led) { - if (ColorCorrections.Count > 0) - { - foreach (Led led in leds) - { - Color color = led.Color; - object key = led.CustomData ?? led.Id; + Color color = led.Color; + object key = led.CustomData ?? led.Id; - foreach (IColorCorrection colorCorrection in ColorCorrections) - colorCorrection.ApplyTo(ref color); + // ReSharper disable once ForCanBeConvertedToForeach - This causes an allocation that's not really needed here + for (int i = 0; i < ColorCorrections.Count; i++) + ColorCorrections[i].ApplyTo(ref color); - yield return (key, color); - } - } - else - { - foreach (Led led in leds) - { - Color color = led.Color; - object key = led.CustomData ?? led.Id; - - yield return (key, color); - } - } + return (key, color); } /// /// Sends all the updated to the device. /// - protected virtual void UpdateLeds(IEnumerable ledsToUpdate) => UpdateQueue.SetData(GetUpdateData(ledsToUpdate)); + protected virtual void UpdateLeds(IEnumerable ledsToUpdate) + { + (object key, Color color)[] buffer = ArrayPool<(object, Color)>.Shared.Rent(LedMapping.Count); + + int counter = 0; + foreach (Led led in ledsToUpdate) + { + led.Update(); + + buffer[counter] = GetUpdateData(led); + ++counter; + } + + UpdateQueue.SetData(new ReadOnlySpan<(object, Color)>(buffer)[..counter]); + + ArrayPool<(object, Color)>.Shared.Return(buffer); + } /// public virtual void Dispose() diff --git a/RGB.NET.Core/Update/Devices/IUpdateQueue.cs b/RGB.NET.Core/Update/Devices/IUpdateQueue.cs index bf4011c..2943379 100644 --- a/RGB.NET.Core/Update/Devices/IUpdateQueue.cs +++ b/RGB.NET.Core/Update/Devices/IUpdateQueue.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; namespace RGB.NET.Core; @@ -21,7 +20,7 @@ public interface IUpdateQueue : IReferenceCounting, IDisposa /// /// The set of data. // ReSharper disable once MemberCanBeProtected.Global - void SetData(IEnumerable<(TIdentifier, TData)> dataSet); + void SetData(ReadOnlySpan<(TIdentifier, TData)> dataSet); /// /// Resets the current data set. diff --git a/RGB.NET.Core/Update/Devices/UpdateQueue.cs b/RGB.NET.Core/Update/Devices/UpdateQueue.cs index 72cec7e..c17e3a9 100644 --- a/RGB.NET.Core/Update/Devices/UpdateQueue.cs +++ b/RGB.NET.Core/Update/Devices/UpdateQueue.cs @@ -1,7 +1,6 @@ using System; using System.Buffers; using System.Collections.Generic; -using System.Linq; namespace RGB.NET.Core; @@ -88,10 +87,9 @@ public abstract class UpdateQueue : AbstractReferenceCountin /// /// The set of data. // ReSharper disable once MemberCanBeProtected.Global - public virtual void SetData(IEnumerable<(TIdentifier, TData)> dataSet) + public virtual void SetData(ReadOnlySpan<(TIdentifier, TData)> data) { - IList<(TIdentifier, TData)> data = dataSet.ToList(); - if (data.Count == 0) return; + if (data.Length == 0) return; lock (_dataLock) { diff --git a/RGB.NET.Devices.Logitech/PerDevice/LogitechPerDeviceRGBDevice.cs b/RGB.NET.Devices.Logitech/PerDevice/LogitechPerDeviceRGBDevice.cs index 9fdbc79..7727006 100644 --- a/RGB.NET.Devices.Logitech/PerDevice/LogitechPerDeviceRGBDevice.cs +++ b/RGB.NET.Devices.Logitech/PerDevice/LogitechPerDeviceRGBDevice.cs @@ -1,6 +1,4 @@ -using System.Collections.Generic; -using System.Linq; -using RGB.NET.Core; +using RGB.NET.Core; namespace RGB.NET.Devices.Logitech; @@ -42,8 +40,5 @@ public class LogitechPerDeviceRGBDevice : LogitechRGBDevice protected override object GetLedCustomData(LedId ledId) => _ledMapping[ledId]; - /// - protected override void UpdateLeds(IEnumerable ledsToUpdate) => UpdateQueue.SetData(GetUpdateData(ledsToUpdate.Take(1))); - #endregion } \ No newline at end of file diff --git a/RGB.NET.Devices.Logitech/PerKey/LogitechPerKeyRGBDevice.cs b/RGB.NET.Devices.Logitech/PerKey/LogitechPerKeyRGBDevice.cs index 933603f..715079e 100644 --- a/RGB.NET.Devices.Logitech/PerKey/LogitechPerKeyRGBDevice.cs +++ b/RGB.NET.Devices.Logitech/PerKey/LogitechPerKeyRGBDevice.cs @@ -1,5 +1,4 @@ -using System.Collections.Generic; -using RGB.NET.Core; +using RGB.NET.Core; namespace RGB.NET.Devices.Logitech; @@ -33,9 +32,6 @@ public class LogitechPerKeyRGBDevice : LogitechRGBDevice, /// protected override object GetLedCustomData(LedId ledId) => _ledMapping.TryGetValue(ledId, out LogitechLedId logitechLedId) ? logitechLedId : -1; - - /// - protected override void UpdateLeds(IEnumerable ledsToUpdate) => UpdateQueue.SetData(GetUpdateData(ledsToUpdate)); - + #endregion } \ No newline at end of file diff --git a/RGB.NET.Devices.Logitech/Zone/LogitechZoneRGBDevice.cs b/RGB.NET.Devices.Logitech/Zone/LogitechZoneRGBDevice.cs index 8820834..4909dcc 100644 --- a/RGB.NET.Devices.Logitech/Zone/LogitechZoneRGBDevice.cs +++ b/RGB.NET.Devices.Logitech/Zone/LogitechZoneRGBDevice.cs @@ -1,5 +1,4 @@ -using System.Collections.Generic; -using RGB.NET.Core; +using RGB.NET.Core; namespace RGB.NET.Devices.Logitech; @@ -41,8 +40,5 @@ public class LogitechZoneRGBDevice : LogitechRGBDevice, I /// protected override object GetLedCustomData(LedId ledId) => _ledMapping[ledId]; - /// - protected override void UpdateLeds(IEnumerable ledsToUpdate) => UpdateQueue.SetData(GetUpdateData(ledsToUpdate)); - #endregion } \ No newline at end of file diff --git a/RGB.NET.Devices.Novation/Generic/NovationRGBDevice.cs b/RGB.NET.Devices.Novation/Generic/NovationRGBDevice.cs index b64c72a..7a8f2eb 100644 --- a/RGB.NET.Devices.Novation/Generic/NovationRGBDevice.cs +++ b/RGB.NET.Devices.Novation/Generic/NovationRGBDevice.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using RGB.NET.Core; namespace RGB.NET.Devices.Novation; @@ -34,9 +33,6 @@ public abstract class NovationRGBDevice : AbstractRGBDevice throw new ArgumentOutOfRangeException() }; - /// - protected override void UpdateLeds(IEnumerable ledsToUpdate) => UpdateQueue.SetData(GetUpdateData(ledsToUpdate)); - /// /// Resets the back to default. /// diff --git a/RGB.NET.Devices.Razer/Generic/RazerRGBDevice.cs b/RGB.NET.Devices.Razer/Generic/RazerRGBDevice.cs index 1a36595..c468948 100644 --- a/RGB.NET.Devices.Razer/Generic/RazerRGBDevice.cs +++ b/RGB.NET.Devices.Razer/Generic/RazerRGBDevice.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using RGB.NET.Core; namespace RGB.NET.Devices.Razer; @@ -27,10 +26,7 @@ public abstract class RazerRGBDevice : AbstractRGBDevice, IR #endregion #region Methods - - /// - protected override void UpdateLeds(IEnumerable ledsToUpdate) => UpdateQueue.SetData(GetUpdateData(ledsToUpdate)); - + /// public override void Dispose() { diff --git a/RGB.NET.Devices.SteelSeries/Generic/SteelSeriesRGBDevice.cs b/RGB.NET.Devices.SteelSeries/Generic/SteelSeriesRGBDevice.cs index debe441..f85213b 100644 --- a/RGB.NET.Devices.SteelSeries/Generic/SteelSeriesRGBDevice.cs +++ b/RGB.NET.Devices.SteelSeries/Generic/SteelSeriesRGBDevice.cs @@ -1,5 +1,4 @@ -using System.Collections.Generic; -using RGB.NET.Core; +using RGB.NET.Core; namespace RGB.NET.Devices.SteelSeries; @@ -43,8 +42,5 @@ public class SteelSeriesRGBDevice : AbstractRGBDevice, /// protected override object GetLedCustomData(LedId ledId) => _ledMapping[ledId]; - /// - protected override void UpdateLeds(IEnumerable ledsToUpdate) => UpdateQueue.SetData(GetUpdateData(ledsToUpdate)); - #endregion } \ No newline at end of file diff --git a/RGB.NET.Devices.WS281X/Arduino/ArduinoWS2812USBDevice.cs b/RGB.NET.Devices.WS281X/Arduino/ArduinoWS2812USBDevice.cs index 8d3d290..4f2bf09 100644 --- a/RGB.NET.Devices.WS281X/Arduino/ArduinoWS2812USBDevice.cs +++ b/RGB.NET.Devices.WS281X/Arduino/ArduinoWS2812USBDevice.cs @@ -53,9 +53,6 @@ public class ArduinoWS2812USBDevice : AbstractRGBDevice protected override IEnumerable GetLedsToUpdate(bool flushLeds) => (flushLeds || LedMapping.Values.Any(x => x.IsDirty)) ? LedMapping.Values : Enumerable.Empty(); - - /// - protected override void UpdateLeds(IEnumerable ledsToUpdate) => UpdateQueue.SetData(GetUpdateData(ledsToUpdate)); - + #endregion } \ No newline at end of file diff --git a/RGB.NET.Devices.WS281X/Bitwizard/BitwizardWS2812USBDevice.cs b/RGB.NET.Devices.WS281X/Bitwizard/BitwizardWS2812USBDevice.cs index a102126..1dc38cd 100644 --- a/RGB.NET.Devices.WS281X/Bitwizard/BitwizardWS2812USBDevice.cs +++ b/RGB.NET.Devices.WS281X/Bitwizard/BitwizardWS2812USBDevice.cs @@ -1,7 +1,6 @@ // ReSharper disable MemberCanBePrivate.Global // ReSharper disable UnusedMember.Global -using System.Collections.Generic; using RGB.NET.Core; namespace RGB.NET.Devices.WS281X.Bitwizard; @@ -47,9 +46,6 @@ public class BitwizardWS2812USBDevice : AbstractRGBDevice protected override object GetLedCustomData(LedId ledId) => _ledOffset + ((int)ledId - (int)LedId.LedStripe1); - - /// - protected override void UpdateLeds(IEnumerable ledsToUpdate) => UpdateQueue.SetData(GetUpdateData(ledsToUpdate)); - + #endregion } \ No newline at end of file diff --git a/RGB.NET.Devices.WS281X/NodeMCU/NodeMCUWS2812USBDevice.cs b/RGB.NET.Devices.WS281X/NodeMCU/NodeMCUWS2812USBDevice.cs index d1a5b98..8c55f6b 100644 --- a/RGB.NET.Devices.WS281X/NodeMCU/NodeMCUWS2812USBDevice.cs +++ b/RGB.NET.Devices.WS281X/NodeMCU/NodeMCUWS2812USBDevice.cs @@ -55,9 +55,6 @@ public class NodeMCUWS2812USBDevice : AbstractRGBDevice protected override IEnumerable GetLedsToUpdate(bool flushLeds) => (flushLeds || LedMapping.Values.Any(x => x.IsDirty)) ? LedMapping.Values : Enumerable.Empty(); - - /// - protected override void UpdateLeds(IEnumerable ledsToUpdate) => UpdateQueue.SetData(GetUpdateData(ledsToUpdate)); - + #endregion } \ No newline at end of file diff --git a/RGB.NET.Devices.Wooting/Keyboard/WootingKeyboardRGBDevice.cs b/RGB.NET.Devices.Wooting/Keyboard/WootingKeyboardRGBDevice.cs index eb0cc75..0f5882c 100644 --- a/RGB.NET.Devices.Wooting/Keyboard/WootingKeyboardRGBDevice.cs +++ b/RGB.NET.Devices.Wooting/Keyboard/WootingKeyboardRGBDevice.cs @@ -47,9 +47,6 @@ public class WootingKeyboardRGBDevice : WootingRGBDevice protected override object GetLedCustomData(LedId ledId) => WootingKeyboardLedMappings.Mapping[DeviceInfo.WootingDeviceType][ledId]; - /// - protected override void UpdateLeds(IEnumerable ledsToUpdate) => UpdateQueue.SetData(GetUpdateData(ledsToUpdate)); - public override void Dispose() { _WootingSDK.SelectDevice(DeviceInfo.WootingDeviceIndex);