Skip to content

Commit

Permalink
Fast \bin skip in rtf parsers
Browse files Browse the repository at this point in the history
Remove the ridiculous Binary state check on every single char in the loop, and just do it when we actually find a \bin keyword (which is basically never)
  • Loading branch information
FenPhoenix committed Sep 30, 2023
1 parent 7d4cf23 commit 872e7b1
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 38 deletions.
1 change: 0 additions & 1 deletion AL_Common/RTFParserCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,6 @@ public enum RtfDestinationState
public enum RtfInternalState
{
Normal,
Binary,
HexEncodedChar
}

Expand Down
14 changes: 2 additions & 12 deletions AngelLoader/Rtf/RtfDisplayedReadmeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,6 @@ private RtfError ParseRtf()

char ch = (char)_rtfBytes[CurrentPos++];

if (_ctx.CurrentScope.RtfInternalState == RtfInternalState.Binary)
{
if (--_binaryCharsLeftToSkip <= 0)
{
_ctx.CurrentScope.RtfInternalState = RtfInternalState.Normal;
_binaryCharsLeftToSkip = 0;
}
continue;
}

RtfError ec;
switch (ch)
{
Expand Down Expand Up @@ -194,8 +184,8 @@ private RtfError DispatchSpecialKeyword(SpecialType specialType, int param)
case SpecialType.Bin:
if (param > 0)
{
_ctx.CurrentScope.RtfInternalState = RtfInternalState.Binary;
_binaryCharsLeftToSkip = param;
CurrentPos += param;
if (CurrentPos >= _rtfBytes.Length) return RtfError.EndOfFile;
}
break;
case SpecialType.SkipDest:
Expand Down
3 changes: 0 additions & 3 deletions AngelLoader/Rtf/RtfDisplayedReadmeParser_Dupe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ public sealed partial class RtfDisplayedReadmeParser

private ArrayWithLength<byte> _rtfBytes = ArrayWithLength<byte>.Empty();

private int _binaryCharsLeftToSkip;

private bool _skipDestinationIfUnknown;

// For whatever reason it's faster to have this
Expand All @@ -29,7 +27,6 @@ private void ResetBase()
_ctx.Reset();

_groupCount = 0;
_binaryCharsLeftToSkip = 0;
_skipDestinationIfUnknown = false;
}

Expand Down
25 changes: 5 additions & 20 deletions FMScanner/Core/RtfToTextConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -970,16 +970,6 @@ private RtfError ParseRtf()
{
char ch = (char)_rtfBytes[CurrentPos++];

if (_ctx.CurrentScope.RtfInternalState == RtfInternalState.Binary)
{
if (--_binaryCharsLeftToSkip <= 0)
{
_ctx.CurrentScope.RtfInternalState = RtfInternalState.Normal;
_binaryCharsLeftToSkip = 0;
}
continue;
}

RtfError ec;
switch (ch)
{
Expand Down Expand Up @@ -1051,13 +1041,9 @@ private RtfError DispatchKeyword(int param, bool hasParam)
// data that follows are considered one character for skipping purposes."
if (symbol.KeywordType == KeywordType.Special && symbol.Index == (int)SpecialType.Bin && _unicodeCharsLeftToSkip > 0)
{
// Hard-skip the chars here, because if we mess around with just incrementing the chars left to
// skip count, we don't end up skipping control char data in the binary run (which we need to).
for (int i = 0; i < param; i++)
{
bool success = GetNextChar(out _);
if (!success) return RtfError.EndOfFile;
}
CurrentPos += param;
if (CurrentPos >= _rtfBytes.Length) return RtfError.EndOfFile;

_unicodeCharsLeftToSkip--;
return RtfError.OK;
}
Expand All @@ -1067,7 +1053,6 @@ private RtfError DispatchKeyword(int param, bool hasParam)
// skippable characters."
// But don't do it if it's a hex char, because we handle it elsewhere in that case.
if ((symbol.KeywordType != KeywordType.Special || symbol.Index != (int)SpecialType.HexEncodedChar) &&
_ctx.CurrentScope.RtfInternalState != RtfInternalState.Binary &&
_unicodeCharsLeftToSkip > 0)
{
if (--_unicodeCharsLeftToSkip <= 0) _unicodeCharsLeftToSkip = 0;
Expand Down Expand Up @@ -1114,8 +1099,8 @@ private RtfError DispatchSpecialKeyword(SpecialType specialType, int param)
case SpecialType.Bin:
if (param > 0)
{
_ctx.CurrentScope.RtfInternalState = RtfInternalState.Binary;
_binaryCharsLeftToSkip = param;
CurrentPos += param;
if (CurrentPos >= _rtfBytes.Length) return RtfError.EndOfFile;
}
break;
case SpecialType.HexEncodedChar:
Expand Down
2 changes: 0 additions & 2 deletions FMScanner/Core/RtfToTextConverter_Dupe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public sealed partial class RtfToTextConverter

private ArrayWithLength<byte> _rtfBytes = ArrayWithLength<byte>.Empty();

private int _binaryCharsLeftToSkip;
private int _unicodeCharsLeftToSkip;

private bool _skipDestinationIfUnknown;
Expand All @@ -30,7 +29,6 @@ private void ResetBase()
_ctx.Reset();

_groupCount = 0;
_binaryCharsLeftToSkip = 0;
_unicodeCharsLeftToSkip = 0;
_skipDestinationIfUnknown = false;
}
Expand Down

0 comments on commit 872e7b1

Please sign in to comment.