Use a crc32 hash as a key

This commit is contained in:
sunshineinabox 2024-03-08 19:53:22 -08:00
parent b2180d34bc
commit 6670961fda

View file

@ -5,6 +5,7 @@ using Avalonia.Media.Imaging;
using System;
using System.Globalization;
using System.IO;
using System.IO.Hashing;
using System.Runtime.Caching;
namespace Ryujinx.Ava.UI.Helpers
@ -26,18 +27,19 @@ namespace Ryujinx.Ava.UI.Helpers
if (value is byte[] buffer && targetType == typeof(IImage))
{
var retrieved = cache.Contains(buffer.AsSpan().ToString());
var bufferhash = BufferHash(buffer);
var retrieved = cache.Contains(bufferhash);
if (retrieved == false)
{
MemoryStream mem = new(buffer);
var bitmap = new Bitmap(mem).CreateScaledBitmap(new PixelSize(256, 256));
cache.Add(buffer.AsSpan().ToString(), bitmap, policy);
cache.Add(bufferhash, bitmap, policy);
return bitmap;
}
else
{
return cache.Get(buffer.AsSpan().ToString());
return cache.Get(bufferhash);
}
}
@ -53,5 +55,10 @@ namespace Ryujinx.Ava.UI.Helpers
{
throw new NotSupportedException();
}
private static string BufferHash(byte[] input)
{
var hashBytes = Crc32.HashToUInt32(input);
return hashBytes.ToString();
}
}
}