Skip to content

Commit

Permalink
Use allocless ReadOnlySpan.Split in Database.InitializeWork
Browse files Browse the repository at this point in the history
reverts 4c69ce4
  • Loading branch information
YoshiRulz committed Dec 16, 2024
1 parent d72332e commit c2200cb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 22 deletions.
18 changes: 18 additions & 0 deletions src/BizHawk.Common/Extensions/CollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,24 @@ public static bool ReversedSequenceEqual<T>(this ReadOnlySpan<T> a, ReadOnlySpan
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);
Expand Down
41 changes: 19 additions & 22 deletions src/BizHawk.Emulation.Common/Database/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Threading;

using BizHawk.Common;
using BizHawk.Common.CollectionExtensions;
using BizHawk.Common.StringExtensions;

namespace BizHawk.Emulation.Common
Expand Down Expand Up @@ -102,21 +103,15 @@ public static void SaveDatabaseEntry(CompactGameInfo gameInfo, string filename =

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
Expand All @@ -128,21 +123,23 @@ string AdvanceAndReadField(out bool isLastField)
"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);
}
}
}
Expand Down

0 comments on commit c2200cb

Please sign in to comment.