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

Fixed potential performance issue with for-loops when Spans are passed by reference

This commit is contained in:
Darth Affe 2024-06-22 19:48:52 +02:00
parent 5f71ffdf1f
commit f21acbd767
36 changed files with 40 additions and 40 deletions

View File

@ -126,7 +126,7 @@ public abstract class PixelTexture<T> : ITexture
/// </summary> /// </summary>
/// <param name="pixel">The pixel-data to convert.</param> /// <param name="pixel">The pixel-data to convert.</param>
/// <returns>The color represented by the specified pixel-data.</returns> /// <returns>The color represented by the specified pixel-data.</returns>
protected abstract Color GetColor(in ReadOnlySpan<T> pixel); protected abstract Color GetColor(ReadOnlySpan<T> pixel);
/// <summary> /// <summary>
/// Gets the pixel-data at the specified location. /// Gets the pixel-data at the specified location.
@ -189,7 +189,7 @@ public sealed class PixelTexture : PixelTexture<Color>
/// <inheritdoc /> /// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
protected override Color GetColor(in ReadOnlySpan<Color> pixel) => pixel[0]; protected override Color GetColor(ReadOnlySpan<Color> pixel) => pixel[0];
#endregion #endregion
} }

View File

@ -22,7 +22,7 @@ public sealed class AverageColorSampler : ISampler<Color>
#region Methods #region Methods
/// <inheritdoc /> /// <inheritdoc />
public unsafe void Sample(in SamplerInfo<Color> info, in Span<Color> pixelData) public unsafe void Sample(in SamplerInfo<Color> info, Span<Color> pixelData)
{ {
int count = info.Width * info.Height; int count = info.Width * info.Height;
if (count == 0) return; if (count == 0) return;

View File

@ -13,5 +13,5 @@ public interface ISampler<T>
/// </summary> /// </summary>
/// <param name="info">The information containing the data to sample.</param> /// <param name="info">The information containing the data to sample.</param>
/// <param name="pixelData">The buffer used to write the resulting pixel to.</param> /// <param name="pixelData">The buffer used to write the resulting pixel to.</param>
void Sample(in SamplerInfo<T> info, in Span<T> pixelData); void Sample(in SamplerInfo<T> info, Span<T> pixelData);
} }

View File

@ -44,7 +44,7 @@ public readonly ref struct SamplerInfo<T>
/// <param name="width">The width of the region the data comes from.</param> /// <param name="width">The width of the region the data comes from.</param>
/// <param name="height">The height of region the data comes from.</param> /// <param name="height">The height of region the data comes from.</param>
/// <param name="data">The data to sample.</param> /// <param name="data">The data to sample.</param>
public SamplerInfo(int x, int y, int width, int height, int stride, int dataPerPixel, in ReadOnlySpan<T> data) public SamplerInfo(int x, int y, int width, int height, int stride, int dataPerPixel, ReadOnlySpan<T> data)
{ {
this._x = x; this._x = x;
this._y = y; this._y = y;

View File

@ -80,7 +80,7 @@ public abstract class UpdateQueue<TIdentifier, TData> : AbstractReferenceCountin
/// Performs the update this queue is responsible for. /// Performs the update this queue is responsible for.
/// </summary> /// </summary>
/// <param name="dataSet">The set of data that needs to be updated.</param> /// <param name="dataSet">The set of data that needs to be updated.</param>
protected abstract bool Update(in ReadOnlySpan<(TIdentifier key, TData color)> dataSet); protected abstract bool Update(ReadOnlySpan<(TIdentifier key, TData color)> dataSet);
/// <summary> /// <summary>
/// Sets or merges the provided data set in the current dataset and notifies the trigger that there is new data available. /// Sets or merges the provided data set in the current dataset and notifies the trigger that there is new data available.

View File

@ -43,7 +43,7 @@ public sealed class AsusUpdateQueue : UpdateQueue
#region Methods #region Methods
/// <inheritdoc /> /// <inheritdoc />
protected override bool Update(in ReadOnlySpan<(object key, Color color)> dataSet) protected override bool Update(ReadOnlySpan<(object key, Color color)> dataSet)
{ {
try try
{ {

View File

@ -37,7 +37,7 @@ public sealed class CoolerMasterUpdateQueue : UpdateQueue
#region Methods #region Methods
/// <inheritdoc /> /// <inheritdoc />
protected override bool Update(in ReadOnlySpan<(object key, Color color)> dataSet) protected override bool Update(ReadOnlySpan<(object key, Color color)> dataSet)
{ {
try try
{ {

View File

@ -40,7 +40,7 @@ public sealed class CorsairDeviceUpdateQueue : UpdateQueue
#region Methods #region Methods
/// <inheritdoc /> /// <inheritdoc />
protected override unsafe bool Update(in ReadOnlySpan<(object key, Color color)> dataSet) protected override unsafe bool Update(ReadOnlySpan<(object key, Color color)> dataSet)
{ {
try try
{ {

View File

@ -35,7 +35,7 @@ public class CorsairDeviceUpdateQueue : UpdateQueue
#region Methods #region Methods
/// <inheritdoc /> /// <inheritdoc />
protected override bool Update(in ReadOnlySpan<(object key, Color color)> dataSet) protected override bool Update(ReadOnlySpan<(object key, Color color)> dataSet)
{ {
try try
{ {

View File

@ -59,7 +59,7 @@ public sealed class E131UpdateQueue : UpdateQueue
} }
/// <inheritdoc /> /// <inheritdoc />
protected override bool Update(in ReadOnlySpan<(object key, Color color)> dataSet) protected override bool Update(ReadOnlySpan<(object key, Color color)> dataSet)
{ {
try try
{ {

View File

@ -7,7 +7,7 @@ internal sealed class DebugDeviceUpdateQueue() : UpdateQueue(new DeviceUpdateTri
{ {
#region Methods #region Methods
protected override bool Update(in ReadOnlySpan<(object key, Color color)> dataSet) => true; protected override bool Update(ReadOnlySpan<(object key, Color color)> dataSet) => true;
#endregion #endregion
} }

View File

@ -25,7 +25,7 @@ public sealed class LogitechPerDeviceUpdateQueue : UpdateQueue
#region Methods #region Methods
/// <inheritdoc /> /// <inheritdoc />
protected override bool Update(in ReadOnlySpan<(object key, Color color)> dataSet) protected override bool Update(ReadOnlySpan<(object key, Color color)> dataSet)
{ {
try try
{ {

View File

@ -24,7 +24,7 @@ public sealed class LogitechPerKeyUpdateQueue : UpdateQueue
#region Methods #region Methods
/// <inheritdoc /> /// <inheritdoc />
protected override bool Update(in ReadOnlySpan<(object key, Color color)> dataSet) protected override bool Update(ReadOnlySpan<(object key, Color color)> dataSet)
{ {
try try
{ {

View File

@ -33,7 +33,7 @@ public sealed class LogitechZoneUpdateQueue : UpdateQueue
#region Methods #region Methods
/// <inheritdoc /> /// <inheritdoc />
protected override bool Update(in ReadOnlySpan<(object key, Color color)> dataSet) protected override bool Update(ReadOnlySpan<(object key, Color color)> dataSet)
{ {
try try
{ {

View File

@ -34,7 +34,7 @@ public sealed class MsiDeviceUpdateQueue : UpdateQueue
#region Methods #region Methods
/// <inheritdoc /> /// <inheritdoc />
protected override bool Update(in ReadOnlySpan<(object key, Color color)> dataSet) protected override bool Update(ReadOnlySpan<(object key, Color color)> dataSet)
{ {
try try
{ {

View File

@ -35,7 +35,7 @@ public abstract class MidiUpdateQueue : UpdateQueue
#region Methods #region Methods
/// <inheritdoc /> /// <inheritdoc />
protected override bool Update(in ReadOnlySpan<(object key, Color color)> dataSet) protected override bool Update(ReadOnlySpan<(object key, Color color)> dataSet)
{ {
try try
{ {

View File

@ -48,7 +48,7 @@ public sealed class OpenRGBUpdateQueue : UpdateQueue
#region Methods #region Methods
/// <inheritdoc /> /// <inheritdoc />
protected override bool Update(in ReadOnlySpan<(object key, Color color)> dataSet) protected override bool Update(ReadOnlySpan<(object key, Color color)> dataSet)
{ {
try try
{ {

View File

@ -44,7 +44,7 @@ public sealed class PicoPiBulkUpdateQueue : UpdateQueue
#region Methods #region Methods
/// <inheritdoc /> /// <inheritdoc />
protected override bool Update(in ReadOnlySpan<(object key, Color color)> dataSet) protected override bool Update(ReadOnlySpan<(object key, Color color)> dataSet)
{ {
try try
{ {

View File

@ -41,7 +41,7 @@ public sealed class PicoPiHIDUpdateQueue : UpdateQueue
#region Methods #region Methods
/// <inheritdoc /> /// <inheritdoc />
protected override bool Update(in ReadOnlySpan<(object key, Color color)> dataSet) protected override bool Update(ReadOnlySpan<(object key, Color color)> dataSet)
{ {
try try
{ {

View File

@ -212,7 +212,7 @@ public sealed class PicoPiSDK : IDisposable
/// </summary> /// </summary>
/// <param name="data">The data to send.</param> /// <param name="data">The data to send.</param>
/// <param name="channel">The channel to update.</param> /// <param name="channel">The channel to update.</param>
public void SendHidUpdate(in Span<byte> buffer, int channel) public void SendHidUpdate(Span<byte> buffer, int channel)
{ {
int chunks = buffer.Length / HID_OFFSET_MULTIPLIER; int chunks = buffer.Length / HID_OFFSET_MULTIPLIER;
if ((chunks * HID_OFFSET_MULTIPLIER) < buffer.Length) chunks++; if ((chunks * HID_OFFSET_MULTIPLIER) < buffer.Length) chunks++;
@ -232,7 +232,7 @@ public sealed class PicoPiSDK : IDisposable
/// <param name="channel">The channel to update.</param> /// <param name="channel">The channel to update.</param>
/// <param name="chunk">The chunk id of the packet. (Required if packets are fragmented.)</param> /// <param name="chunk">The chunk id of the packet. (Required if packets are fragmented.)</param>
/// <param name="update">A value indicating if the device should update directly after receiving this packet. (If packets are fragmented this should only be true for the last chunk.)</param> /// <param name="update">A value indicating if the device should update directly after receiving this packet. (If packets are fragmented this should only be true for the last chunk.)</param>
public void SendHidUpdate(in Span<byte> data, int channel, int chunk, bool update) public void SendHidUpdate(Span<byte> data, int channel, int chunk, bool update)
{ {
if (data.Length == 0) return; if (data.Length == 0) return;
@ -253,7 +253,7 @@ public sealed class PicoPiSDK : IDisposable
/// </remarks> /// </remarks>
/// <param name="data">The data packet to send.</param> /// <param name="data">The data packet to send.</param>
/// <param name="channel">The channel to update.</param> /// <param name="channel">The channel to update.</param>
public void SendBulkUpdate(in Span<byte> data, int channel) public void SendBulkUpdate(Span<byte> data, int channel)
{ {
if ((data.Length == 0) || !IsBulkSupported) return; if ((data.Length == 0) || !IsBulkSupported) return;

View File

@ -25,7 +25,7 @@ public sealed class RazerChromaLinkUpdateQueue : RazerUpdateQueue
#region Methods #region Methods
/// <inheritdoc /> /// <inheritdoc />
protected override nint CreateEffectParams(in ReadOnlySpan<(object key, Color color)> dataSet) protected override nint CreateEffectParams(ReadOnlySpan<(object key, Color color)> dataSet)
{ {
_Color[] colors = new _Color[_Defines.CHROMALINK_MAX_LEDS]; _Color[] colors = new _Color[_Defines.CHROMALINK_MAX_LEDS];

View File

@ -30,7 +30,7 @@ public abstract class RazerUpdateQueue : UpdateQueue
#region Methods #region Methods
/// <inheritdoc /> /// <inheritdoc />
protected override bool Update(in ReadOnlySpan<(object key, Color color)> dataSet) protected override bool Update(ReadOnlySpan<(object key, Color color)> dataSet)
{ {
try try
{ {
@ -77,7 +77,7 @@ public abstract class RazerUpdateQueue : UpdateQueue
/// </summary> /// </summary>
/// <param name="dataSet">The data to be updated.</param> /// <param name="dataSet">The data to be updated.</param>
/// <returns>An <see cref="IntPtr"/> pointing to the effect parameter struct.</returns> /// <returns>An <see cref="IntPtr"/> pointing to the effect parameter struct.</returns>
protected abstract nint CreateEffectParams(in ReadOnlySpan<(object key, Color color)> dataSet); protected abstract nint CreateEffectParams(ReadOnlySpan<(object key, Color color)> dataSet);
#endregion #endregion
} }

View File

@ -25,7 +25,7 @@ public sealed class RazerHeadsetUpdateQueue : RazerUpdateQueue
#region Methods #region Methods
/// <inheritdoc /> /// <inheritdoc />
protected override nint CreateEffectParams(in ReadOnlySpan<(object key, Color color)> dataSet) protected override nint CreateEffectParams(ReadOnlySpan<(object key, Color color)> dataSet)
{ {
_Color[] colors = new _Color[_Defines.HEADSET_MAX_LEDS]; _Color[] colors = new _Color[_Defines.HEADSET_MAX_LEDS];

View File

@ -25,7 +25,7 @@ public sealed class RazerKeyboardUpdateQueue : RazerUpdateQueue
#region Methods #region Methods
/// <inheritdoc /> /// <inheritdoc />
protected override nint CreateEffectParams(in ReadOnlySpan<(object key, Color color)> dataSet) protected override nint CreateEffectParams(ReadOnlySpan<(object key, Color color)> dataSet)
{ {
_Color[] colors = new _Color[_Defines.KEYBOARD_MAX_LEDS]; _Color[] colors = new _Color[_Defines.KEYBOARD_MAX_LEDS];

View File

@ -25,7 +25,7 @@ public sealed class RazerKeypadUpdateQueue : RazerUpdateQueue
#region Methods #region Methods
/// <inheritdoc /> /// <inheritdoc />
protected override nint CreateEffectParams(in ReadOnlySpan<(object key, Color color)> dataSet) protected override nint CreateEffectParams(ReadOnlySpan<(object key, Color color)> dataSet)
{ {
_Color[] colors = new _Color[_Defines.KEYPAD_MAX_LEDS]; _Color[] colors = new _Color[_Defines.KEYPAD_MAX_LEDS];

View File

@ -25,7 +25,7 @@ public sealed class RazerMouseUpdateQueue : RazerUpdateQueue
#region Methods #region Methods
/// <inheritdoc /> /// <inheritdoc />
protected override nint CreateEffectParams(in ReadOnlySpan<(object key, Color color)> dataSet) protected override nint CreateEffectParams(ReadOnlySpan<(object key, Color color)> dataSet)
{ {
_Color[] colors = new _Color[_Defines.MOUSE_MAX_LEDS]; _Color[] colors = new _Color[_Defines.MOUSE_MAX_LEDS];

View File

@ -25,7 +25,7 @@ public sealed class RazerMousepadUpdateQueue : RazerUpdateQueue
#region Methods #region Methods
/// <inheritdoc /> /// <inheritdoc />
protected override nint CreateEffectParams(in ReadOnlySpan<(object key, Color color)> dataSet) protected override nint CreateEffectParams(ReadOnlySpan<(object key, Color color)> dataSet)
{ {
_Color[] colors = new _Color[_Defines.MOUSEPAD_MAX_LEDS]; _Color[] colors = new _Color[_Defines.MOUSEPAD_MAX_LEDS];

View File

@ -51,7 +51,7 @@ internal sealed class SteelSeriesDeviceUpdateQueue : UpdateQueue
} }
/// <inheritdoc /> /// <inheritdoc />
protected override bool Update(in ReadOnlySpan<(object key, Color color)> dataSet) protected override bool Update(ReadOnlySpan<(object key, Color color)> dataSet)
{ {
try try
{ {

View File

@ -63,7 +63,7 @@ internal sealed class WledDeviceUpdateQueue : UpdateQueue
} }
/// <inheritdoc /> /// <inheritdoc />
protected override bool Update(in ReadOnlySpan<(object key, Color color)> dataSet) protected override bool Update(ReadOnlySpan<(object key, Color color)> dataSet)
{ {
try try
{ {

View File

@ -56,7 +56,7 @@ public abstract class SerialConnectionUpdateQueue<TData> : UpdateQueue
} }
/// <inheritdoc /> /// <inheritdoc />
protected override bool Update(in ReadOnlySpan<(object key, Color color)> dataSet) protected override bool Update(ReadOnlySpan<(object key, Color color)> dataSet)
{ {
try try
{ {

View File

@ -77,7 +77,7 @@ public sealed class NodeMCUWS2812USBUpdateQueue : UpdateQueue
} }
/// <inheritdoc /> /// <inheritdoc />
protected override bool Update(in ReadOnlySpan<(object key, Color color)> dataSet) protected override bool Update(ReadOnlySpan<(object key, Color color)> dataSet)
{ {
try try
{ {

View File

@ -33,7 +33,7 @@ public sealed class WootingUpdateQueue : UpdateQueue
#region Methods #region Methods
/// <inheritdoc /> /// <inheritdoc />
protected override bool Update(in ReadOnlySpan<(object key, Color color)> dataSet) protected override bool Update(ReadOnlySpan<(object key, Color color)> dataSet)
{ {
try try
{ {

View File

@ -61,7 +61,7 @@ public sealed class BytePixelTexture : PixelTexture<byte>
/// <inheritdoc /> /// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
protected override Color GetColor(in ReadOnlySpan<byte> pixel) protected override Color GetColor(ReadOnlySpan<byte> pixel)
{ {
return ColorFormat switch return ColorFormat switch
{ {

View File

@ -61,7 +61,7 @@ public sealed class FloatPixelTexture : PixelTexture<float>
/// <inheritdoc /> /// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
protected override Color GetColor(in ReadOnlySpan<float> pixel) protected override Color GetColor(ReadOnlySpan<float> pixel)
{ {
return ColorFormat switch return ColorFormat switch
{ {

View File

@ -18,7 +18,7 @@ public sealed class AverageByteSampler : ISampler<byte>
#region Methods #region Methods
/// <inheritdoc /> /// <inheritdoc />
public unsafe void Sample(in SamplerInfo<byte> info, in Span<byte> pixelData) public unsafe void Sample(in SamplerInfo<byte> info, Span<byte> pixelData)
{ {
int count = info.Width * info.Height; int count = info.Width * info.Height;
if (count == 0) return; if (count == 0) return;

View File

@ -12,7 +12,7 @@ public sealed class AverageFloatSampler : ISampler<float>
#region Methods #region Methods
/// <inheritdoc /> /// <inheritdoc />
public unsafe void Sample(in SamplerInfo<float> info, in Span<float> pixelData) public unsafe void Sample(in SamplerInfo<float> info, Span<float> pixelData)
{ {
int count = info.Width * info.Height; int count = info.Width * info.Height;
if (count == 0) return; if (count == 0) return;