Skip to content

Commit

Permalink
Add SequenceEqual extension to prevent linq usage
Browse files Browse the repository at this point in the history
this speeds up the state history integrity check by about an order of magnitude
  • Loading branch information
Morilli committed Oct 20, 2024
1 parent 1dbd735 commit 7ab2ca6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/BizHawk.Bizware.Graphics/BitmapBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using System.IO;
using System.Runtime.InteropServices;

using BizHawk.Common.CollectionExtensions;

using SDGraphics = System.Drawing.Graphics;

namespace BizHawk.Bizware.Graphics
Expand All @@ -34,10 +36,6 @@ public unsafe class BitmapBuffer : IDisposable
private GCHandle CurrLockHandle;
private BitmapData CurrLock;

/// <summary>same as <see cref="Pixels"/> (<see cref="PixelFormat.Format32bppArgb">A8R8G8B8</see>)</summary>
public Span<int> AsSpan()
=> Pixels;

/// <exception cref="InvalidOperationException">already locked</exception>
/// <remarks>TODO add read/write semantic, for wraps</remarks>
public BitmapData LockBits()
Expand Down Expand Up @@ -545,7 +543,7 @@ private static int NextHigher(int k)
}

public bool SequenceEqual(BitmapBuffer other)
=> Width == other.Width && Height == other.Height && AsSpan().SequenceEqual(other.AsSpan());
=> Width == other.Width && Height == other.Height && Pixels.SequenceEqual(other.Pixels);

/// <summary>
/// Dumps this BitmapBuffer to a new System.Drawing.Bitmap
Expand Down Expand Up @@ -607,4 +605,4 @@ public void ToSysdrawingBitmap(Bitmap bmp)

}

}
}
2 changes: 2 additions & 0 deletions src/BizHawk.Common/Extensions/CollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -321,5 +321,7 @@ public static bool IsSortedDesc<T>(this ReadOnlySpan<T> span)
for (int i = 0, e = span.Length - 1; i < e; i++) if (span[i + 1].CompareTo(span[i]) > 0) return false;
return true;
}

public static bool SequenceEqual<T>(this T[] a, ReadOnlySpan<T> b) where T : IEquatable<T> => a.AsSpan().SequenceEqual(b);
}
}

3 comments on commit 7ab2ca6

@YoshiRulz
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous SequenceEqual(this Span, Span) call was actually LINQ?

@YoshiRulz
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or is that referring to some other call, which you've invisibly changed the overload resolution for?

@Morilli
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or is that referring to some other call, which you've invisibly changed the overload resolution for?

Correct, the resolution for

if (!state.SequenceEqual(greenZone))
(and some other less relevant cases) was changed to this function.

Please sign in to comment.