replaced ReadByte with 64kb buffer in parser

This commit is contained in:
2026-09-14 23:09:16 +02:00
parent f726597401
commit f1aca261ae
+14 -8
View File
@@ -1,4 +1,4 @@
global using System; global using System;
global using System.Collections.Generic; global using System.Collections.Generic;
global using System.IO; global using System.IO;
global using System.Text; global using System.Text;
@@ -80,17 +80,19 @@ public class SaveParserEU4
return true; return true;
} }
while (_saveFile.CanRead) // Reading the save one byte at a time through Stream.ReadByte() costs a virtual call
// per byte, which dominated the parsing time. Bytes are pulled into this buffer
// instead, so the stream is touched once per 64 KB and the inner loop reads an array.
byte[] buffer = new byte[64 * 1024];
int bufferLength;
while ((bufferLength = _saveFile.Read(buffer, 0, buffer.Length)) > 0)
{ {
int c = _saveFile.ReadByte(); for (int i = 0; i < bufferLength; i++)
{
int c = buffer[i];
column++; column++;
switch (c) switch (c)
{ {
case -1:
if (TryCompleteStringToken())
yield return strToken;
_stringBuilderPool.Return(strb);
yield break;
case '\"': case '\"':
isQuoteOpen = !isQuoteOpen; isQuoteOpen = !isQuoteOpen;
isStrInQuotes = true; isStrInQuotes = true;
@@ -142,7 +144,11 @@ public class SaveParserEU4
break; break;
} }
} }
}
// end of file: the last token may still be unterminated
if (TryCompleteStringToken())
yield return strToken;
_stringBuilderPool.Return(strb); _stringBuilderPool.Return(strb);
} }