Improve key parser performance a bit

This commit is contained in:
Alex Barney 2020-10-11 01:26:43 -07:00
parent b5dabe78f5
commit 8f011387a0
2 changed files with 21 additions and 8 deletions

View file

@ -177,6 +177,8 @@ namespace LibHac.Common.Keys
Span<char> buffer = stackalloc char[1024]; Span<char> buffer = stackalloc char[1024];
var ctx = new KvPairReaderContext(streamReader, buffer); var ctx = new KvPairReaderContext(streamReader, buffer);
// Estimate the number of keys by assuming each line is about 69 bytes.
// Subtract 2 from that so we estimate slightly high.
keySet.ExternalKeySet.EnsureCapacity((int)reader.Length / 67); keySet.ExternalKeySet.EnsureCapacity((int)reader.Length / 67);
while (true) while (true)
@ -310,11 +312,18 @@ namespace LibHac.Common.Keys
continue; continue;
case ReaderState.Initial when IsValidNameChar(c): case ReaderState.Initial when IsValidNameChar(c):
state = ReaderState.Key; state = ReaderState.Key;
ToLower(ref buffer[i]);
keyOffset = i; keyOffset = i;
continue;
case ReaderState.Key when IsValidNameChar(c): // Skip the next few rounds through the state machine since we know we should be
ToLower(ref buffer[i]); // encountering a string of name characters
do
{
ToLower(ref buffer[i]);
i++;
} while (i < buffer.Length && IsValidNameChar(buffer[i]));
// Decrement so we can process this character the next round through the state machine
i--;
continue; continue;
case ReaderState.Key when IsWhiteSpace(c): case ReaderState.Key when IsWhiteSpace(c):
state = ReaderState.WhiteSpace1; state = ReaderState.WhiteSpace1;
@ -334,6 +343,13 @@ namespace LibHac.Common.Keys
case ReaderState.Delimiter when StringUtils.IsHexDigit((byte)c): case ReaderState.Delimiter when StringUtils.IsHexDigit((byte)c):
state = ReaderState.Value; state = ReaderState.Value;
valueOffset = i; valueOffset = i;
do
{
i++;
} while (i < buffer.Length && !IsEndOfLine(buffer[i]) && !IsWhiteSpace(buffer[i]));
i--;
continue; continue;
case ReaderState.Value when IsEndOfLine(c): case ReaderState.Value when IsEndOfLine(c):
state = ReaderState.End; state = ReaderState.End;
@ -343,8 +359,6 @@ namespace LibHac.Common.Keys
state = ReaderState.WhiteSpace2; state = ReaderState.WhiteSpace2;
valueLength = i - valueOffset; valueLength = i - valueOffset;
continue; continue;
case ReaderState.Value when StringUtils.IsHexDigit((byte)c):
continue;
case ReaderState.WhiteSpace2 when IsWhiteSpace(c): case ReaderState.WhiteSpace2 when IsWhiteSpace(c):
continue; continue;
case ReaderState.WhiteSpace2 when IsEndOfLine(c): case ReaderState.WhiteSpace2 when IsEndOfLine(c):

View file

@ -194,8 +194,7 @@ namespace LibHac.Util
public static bool IsHexDigit(byte c) public static bool IsHexDigit(byte c)
{ {
return (uint)(c - (byte)'0') <= 9 || return HexConverter.CharToHexLookup[c] != 0xFF;
(c | 0x20u) - (byte)'a' <= 'f' - 'a';
} }
public static bool TryFromHexString(ReadOnlySpan<char> chars, Span<byte> outputBytes) public static bool TryFromHexString(ReadOnlySpan<char> chars, Span<byte> outputBytes)