Use allocless `ReadOnlySpan.Split` in `Database.InitializeWork`

reverts 4c69ce4e2
This commit is contained in:
YoshiRulz 2024-12-15 14:40:09 +10:00
parent d72332e97d
commit c2200cb234
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
2 changed files with 37 additions and 22 deletions

View File

@ -292,6 +292,24 @@ namespace BizHawk.Common.CollectionExtensions
return true;
}
public static ReadOnlySpan<T> Slice<T>(this ReadOnlySpan<T> span, Range range)
{
var (offset, length) = range.GetOffsetAndLength(span.Length);
return span.Slice(start: offset, length: length);
}
public static Span<T> Slice<T>(this Span<T> span, Range range)
{
var (offset, length) = range.GetOffsetAndLength(span.Length);
return span.Slice(start: offset, length: length);
}
public static string Substring(this string str, Range range)
{
var (offset, length) = range.GetOffsetAndLength(str.Length);
return str.Substring(startIndex: offset, length: length);
}
/// <summary>shallow clone</summary>
public static Dictionary<TKey, TValue> ToDictionary<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> list)
=> list.ToDictionary(static kvp => kvp.Key, static kvp => kvp.Value);

View File

@ -9,6 +9,7 @@ using System.Text;
using System.Threading;
using BizHawk.Common;
using BizHawk.Common.CollectionExtensions;
using BizHawk.Common.StringExtensions;
namespace BizHawk.Emulation.Common
@ -102,21 +103,15 @@ namespace BizHawk.Emulation.Common
private static bool initialized = false;
public static CompactGameInfo ParseCGIRecord(string line)
public static CompactGameInfo ParseCGIRecord(string lineStr)
{
var line = lineStr.AsSpan();
const char FIELD_SEPARATOR = '\t';
var iFieldStart = -1;
var iFieldEnd = -1; // offset of the tab char, or line.Length if at end
string AdvanceAndReadField(out bool isLastField)
{
iFieldStart = iFieldEnd + 1;
iFieldEnd = line.IndexOf(FIELD_SEPARATOR, iFieldStart);
isLastField = iFieldEnd < 0;
if (isLastField) iFieldEnd = line.Length;
return line.Substring(startIndex: iFieldStart, length: iFieldEnd - iFieldStart);
}
var hashDigest = FormatHash(AdvanceAndReadField(out _));
var dumpStatus = AdvanceAndReadField(out _).Trim() switch
var iter = line.Split(FIELD_SEPARATOR);
_ = iter.MoveNext();
var hashDigest = FormatHash(lineStr.Substring(iter.Current));
_ = iter.MoveNext();
var dumpStatus = line.Slice(iter.Current).Trim() switch
{
"B" => RomStatus.BadDump, // see /Assets/gamedb/gamedb.txt
"V" => RomStatus.BadDump, // see /Assets/gamedb/gamedb.txt
@ -128,21 +123,23 @@ namespace BizHawk.Emulation.Common
"U" => RomStatus.Unknown,
_ => RomStatus.GoodDump
};
var knownName = AdvanceAndReadField(out _);
var sysID = AdvanceAndReadField(out var isLastField);
_ = iter.MoveNext();
var knownName = lineStr.Substring(iter.Current);
_ = iter.MoveNext();
var sysID = lineStr.Substring(iter.Current);
string/*?*/ metadata = null;
string region = string.Empty;
string forcedCore = string.Empty;
if (!isLastField)
if (iter.MoveNext())
{
_ = AdvanceAndReadField(out isLastField); // rarely present; possibly genre or just a remark
if (!isLastField)
//_ = line.Slice(iter.Current); // rarely populated; possibly genre or just a remark
if (iter.MoveNext())
{
metadata = AdvanceAndReadField(out isLastField);
if (!isLastField)
metadata = lineStr.Substring(iter.Current);
if (iter.MoveNext())
{
region = AdvanceAndReadField(out isLastField);
if (!isLastField) forcedCore = AdvanceAndReadField(out isLastField);
region = lineStr.Substring(iter.Current);
if (iter.MoveNext()) forcedCore = lineStr.Substring(iter.Current);
}
}
}