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; 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> /// <summary>shallow clone</summary>
public static Dictionary<TKey, TValue> ToDictionary<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> list) public static Dictionary<TKey, TValue> ToDictionary<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> list)
=> list.ToDictionary(static kvp => kvp.Key, static kvp => kvp.Value); => list.ToDictionary(static kvp => kvp.Key, static kvp => kvp.Value);

View File

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