Compare commits

..

No commits in common. "f3106769d9f4d40d2ffe4ef5535eb24ef0d21b2f" and "e9c7c8f5c16df017b569548bae7c2038ab0d0bb2" have entirely different histories.

5 changed files with 47 additions and 165 deletions

View File

@ -37,73 +37,35 @@ namespace ParadoxSaveParser.Lib;
/// 2 3 | 4 | 5 /// 2 3 | 4 | 5
/// 3 4 | 5 | /// 3 4 | 5 |
/// </code> /// </code>
public class BufferedEnumerator<T> : IEnumerator<BufferedEnumerator<T>.Node> public class BufferedEnumerator<T> : IEnumerator<LinkedListNode<T>>
{ {
public class Node private readonly int _bufferSize;
{ private LinkedListNode<T>? _currentNode;
#nullable disable private int _currentNodeIndex = -1;
public Node Previous;
public Node Next;
public T Value;
#nullable enable
}
private readonly IEnumerator<T> _enumerator; private readonly IEnumerator<T> _enumerator;
private readonly Node[] _ringBuffer; private readonly LinkedList<T> _llist = new();
private Node? _currentNode;
private int _currentBufferIndex = -1;
private int _lastValueIndex = -1;
public BufferedEnumerator(IEnumerator<T> enumerator, int bufferSize) public BufferedEnumerator(IEnumerator<T> enumerator, int bufferSize)
{ {
_enumerator = enumerator; _enumerator = enumerator;
_ringBuffer = new Node[bufferSize]; _bufferSize = bufferSize;
}
private void InitBuffer()
{
_ringBuffer[0] = new Node
{
Value = default!
};
for (int i = 1; i < _ringBuffer.Length; i++)
{
_ringBuffer[i] = new Node
{
Previous = _ringBuffer[i - 1],
Value = default!,
};
_ringBuffer[i - 1].Next = _ringBuffer[i];
}
_ringBuffer[^1].Next = _ringBuffer[0];
_ringBuffer[0].Previous = _ringBuffer[^1];
} }
public bool MoveNext() public bool MoveNext()
{ {
if (_currentBufferIndex == -1) if (_currentNodeIndex >= _bufferSize / 2)
{ _llist.RemoveFirst();
InitBuffer();
int beforeMidpoint = _ringBuffer.Length / 2 - 1; while (_llist.Count < _bufferSize && _enumerator.MoveNext())
for (int i = 0; i <= beforeMidpoint && _enumerator.MoveNext(); i++)
{
_ringBuffer[i].Value = _enumerator.Current;
}
}
_currentBufferIndex = (_currentBufferIndex + 1) % _ringBuffer.Length;
if (_enumerator.MoveNext())
{ {
int midpoint = (_currentBufferIndex + _ringBuffer.Length / 2) % _ringBuffer.Length; _llist.AddLast(_enumerator.Current);
_ringBuffer[midpoint].Value = _enumerator.Current;
_lastValueIndex = midpoint;
} }
if(_currentBufferIndex == (_lastValueIndex + 1) % _ringBuffer.Length) if (_llist.Count == 0)
return false; return false;
_currentNode = _ringBuffer[_currentBufferIndex]; _currentNodeIndex++;
return true; _currentNode = _currentNode is null ? _llist.First : _currentNode.Next;
return _currentNode is not null;
} }
public void Reset() public void Reset()
@ -111,7 +73,7 @@ public class BufferedEnumerator<T> : IEnumerator<BufferedEnumerator<T>.Node>
throw new NotImplementedException(); throw new NotImplementedException();
} }
public Node Current => _currentNode!; public LinkedListNode<T> Current => _currentNode!;
object IEnumerator.Current => Current; object IEnumerator.Current => Current;

View File

@ -4,8 +4,5 @@
<ImplicitUsings>disable</ImplicitUsings> <ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="9.0.3" />
</ItemGroup>
</Project> </Project>

View File

@ -2,7 +2,6 @@
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;
using Microsoft.Extensions.ObjectPool;
namespace ParadoxSaveParser.Lib; namespace ParadoxSaveParser.Lib;
@ -11,13 +10,9 @@ namespace ParadoxSaveParser.Lib;
/// </summary> /// </summary>
public class SaveParserEU4 public class SaveParserEU4
{ {
protected readonly Stream _saveFile; protected Stream _saveFile;
private readonly BufferedEnumerator<Token> _tokens;
private readonly ObjectPool<StringBuilder> _stringBuilderPool;
private ISearchExpression? _searchExprCurrent; private ISearchExpression? _searchExprCurrent;
private readonly BufferedEnumerator<Token> _tokens;
public int SBPoolGetCount = 0;
public int SBPoolReturnCount = 0;
/// <param name="savefile"> /// <param name="savefile">
/// Uncompressed stream of <c>gamestate</c> file which can be extracted from save archive /// Uncompressed stream of <c>gamestate</c> file which can be extracted from save archive
@ -28,16 +23,9 @@ public class SaveParserEU4
/// </param> /// </param>
public SaveParserEU4(Stream savefile, ISearchExpression? query) public SaveParserEU4(Stream savefile, ISearchExpression? query)
{ {
_tokens = new BufferedEnumerator<Token>(LexTextSave(), 5);
_saveFile = savefile; _saveFile = savefile;
_searchExprCurrent = query; _searchExprCurrent = query;
const int tokenBufSize = 5;
_tokens = new BufferedEnumerator<Token>(LexTextSave(), tokenBufSize);
_stringBuilderPool = new DefaultObjectPool<StringBuilder>(
new StringBuilderPooledObjectPolicy
{
InitialCapacity = tokenBufSize * 13,
MaximumRetainedCapacity = tokenBufSize * 13,
});
} }
protected IEnumerator<Token> LexTextSave() protected IEnumerator<Token> LexTextSave()
@ -49,8 +37,7 @@ public class SaveParserEU4
if (headStr != expectedHeader) if (headStr != expectedHeader)
throw new Exception($"Invalid gamestate header. Expected '{expectedHeader}', got '{headStr}'."); throw new Exception($"Invalid gamestate header. Expected '{expectedHeader}', got '{headStr}'.");
StringBuilder strb = _stringBuilderPool.Get(); StringBuilder str = new();
SBPoolGetCount++;
int line = 2; int line = 2;
int column = 0; int column = 0;
bool isQuoteOpen = false; bool isQuoteOpen = false;
@ -59,8 +46,7 @@ public class SaveParserEU4
{ {
type = TokenType.Invalid, type = TokenType.Invalid,
column = -1, column = -1,
line = -1, line = -1
value = null,
}; };
bool TryCompleteStringToken() bool TryCompleteStringToken()
@ -69,18 +55,17 @@ public class SaveParserEU4
return false; return false;
// strings in quotes may be empty // strings in quotes may be empty
if (!isStrInQuotes && (strb.Length <= 0 || strb[0] == '#')) if (!isStrInQuotes && (str.Length <= 0 || str[0] == '#'))
return false; return false;
strToken = new Token strToken = new Token
{ {
type = TokenType.StringOrNumber, type = TokenType.StringOrNumber,
column = (short)(column - strb.Length), column = (short)(column - str.Length),
line = line, line = line,
value = strb, value = str.ToString()
}; };
strb = _stringBuilderPool.Get(); str.Clear();
SBPoolGetCount++;
isStrInQuotes = false; isStrInQuotes = false;
return true; return true;
} }
@ -94,8 +79,6 @@ public class SaveParserEU4
case -1: case -1:
if (TryCompleteStringToken()) if (TryCompleteStringToken())
yield return strToken; yield return strToken;
_stringBuilderPool.Return(strb);
SBPoolReturnCount++;
yield break; yield break;
case '\"': case '\"':
isQuoteOpen = !isQuoteOpen; isQuoteOpen = !isQuoteOpen;
@ -144,13 +127,10 @@ public class SaveParserEU4
// Skip control characters, which are invisible and causing frontend bugs. // Skip control characters, which are invisible and causing frontend bugs.
// I dont know why there are so many of them in strings. // I dont know why there are so many of them in strings.
if (c >= 0x20) if (c >= 0x20)
strb.Append((char)c); str.Append((char)c);
break; break;
} }
} }
_stringBuilderPool.Return(strb);
SBPoolReturnCount++;
} }
@ -161,16 +141,15 @@ public class SaveParserEU4
switch (tok.type) switch (tok.type)
{ {
case TokenType.StringOrNumber: case TokenType.StringOrNumber:
string tokStr = tok.value!.ToString(); if (string.IsNullOrEmpty(tok.value))
_stringBuilderPool.Return(tok.value); return string.Empty;
SBPoolReturnCount++; if (tok.value[0] != '-' && !char.IsDigit(tok.value[0]))
if (tokStr[0] != '-' && !char.IsDigit(tokStr[0])) return tok.value;
return tokStr; if (tok.value.Contains('.') && double.TryParse(tok.value, out double d))
if (tokStr.Contains('.') && double.TryParse(tokStr, out double d))
return d; return d;
if (long.TryParse(tokStr, out long l)) if (long.TryParse(tok.value, out long l))
return l; return l;
return tokStr; return tok.value;
case TokenType.BracketOpen: case TokenType.BracketOpen:
object obj = ParseListOrDict(); object obj = ParseListOrDict();
return obj; return obj;
@ -187,22 +166,13 @@ public class SaveParserEU4
private bool SkipValue() private bool SkipValue()
{ {
var tok = _tokens.Current.Value; var tok = _tokens.Current.Value;
switch (tok.type) if (tok.type == TokenType.BracketOpen)
{ {
case TokenType.BracketOpen: SkipObject();
SkipObject(); return true;
return true;
case TokenType.StringOrNumber:
_stringBuilderPool.Return(tok.value!);
SBPoolReturnCount++;
return true;
case TokenType.Equals:
return true;
case TokenType.BracketClose:
return false;
default:
throw new UnexpectedTokenException(tok);
} }
return tok.type != TokenType.BracketClose;
} }
// skips all tokens inside curly braces block // skips all tokens inside curly braces block
@ -215,11 +185,6 @@ public class SaveParserEU4
bracketBalance++; bracketBalance++;
else if (tok.type == TokenType.BracketClose) else if (tok.type == TokenType.BracketClose)
bracketBalance--; bracketBalance--;
else if (tok.type == TokenType.StringOrNumber)
{
_stringBuilderPool.Return(tok.value!);
SBPoolReturnCount++;
}
} }
} }
@ -277,7 +242,7 @@ public class SaveParserEU4
if (tok.type != TokenType.StringOrNumber) if (tok.type != TokenType.StringOrNumber)
throw new UnexpectedTokenException(tok); throw new UnexpectedTokenException(tok);
var keySB = tok.value!; string key = tok.value!;
// next token should be `=` or `{` // next token should be `=` or `{`
if (!_tokens.MoveNext()) if (!_tokens.MoveNext())
@ -298,11 +263,9 @@ public class SaveParserEU4
ISearchExpression? searchExprNext = null; ISearchExpression? searchExprNext = null;
if (_searchExprCurrent != null if (_searchExprCurrent != null
&& !_searchExprCurrent.DoesMatch(new SearchArgs(localIndex, keySB), out searchExprNext)) && !_searchExprCurrent.DoesMatch(new SearchArgs(key, localIndex), out searchExprNext))
{ {
SkipValue(); SkipValue();
_stringBuilderPool.Return(keySB);
SBPoolReturnCount++;
continue; continue;
} }
@ -313,13 +276,10 @@ public class SaveParserEU4
throw new UnexpectedTokenException(_tokens.Current.Value); throw new UnexpectedTokenException(_tokens.Current.Value);
_searchExprCurrent = searExpressionPrevious; _searchExprCurrent = searExpressionPrevious;
string keyStr = keySB.ToString(); if (!dict.TryGetValue(key, out var list))
_stringBuilderPool.Return(keySB);
SBPoolReturnCount++;
if (!dict.TryGetValue(keyStr, out var list))
{ {
list = new List<object>(); list = new List<object>();
dict.Add(keyStr, list); dict.Add(key, list);
} }
list.Add(value); list.Add(value);
@ -348,7 +308,7 @@ public class SaveParserEU4
public required TokenType type; public required TokenType type;
public required short column; public required short column;
public required int line; public required int line;
public StringBuilder? value; public string? value;
public override string ToString() public override string ToString()
{ {
@ -359,9 +319,7 @@ public class SaveParserEU4
s = "INVALID_TOKEN"; s = "INVALID_TOKEN";
break; break;
case TokenType.StringOrNumber: case TokenType.StringOrNumber:
if (value == null || value.Length == 0) s = value ?? "NULL";
s = "NULL";
else s = value.ToString();
break; break;
case TokenType.Equals: case TokenType.Equals:
s = "="; s = "=";

View File

@ -1,25 +1,6 @@
namespace ParadoxSaveParser.Lib; namespace ParadoxSaveParser.Lib;
public record SearchArgs public record SearchArgs(string key, int localIndex);
{
public readonly string KeyStr;
public readonly StringBuilder? KeySB;
public readonly int LocalIndex;
public SearchArgs(int localIndex, string keyStr)
{
KeyStr = keyStr;
KeySB = null;
LocalIndex = localIndex;
}
public SearchArgs(int localIndex, StringBuilder keySb)
{
KeyStr = string.Empty;
KeySB = keySb;
LocalIndex = localIndex;
}
}
public interface ISearchExpression public interface ISearchExpression
{ {
@ -128,7 +109,7 @@ public static class SearchExpressionCompiler
{ {
public bool DoesMatch(SearchArgs args, out ISearchExpression? nextSearchExpression) public bool DoesMatch(SearchArgs args, out ISearchExpression? nextSearchExpression)
{ {
if (args.LocalIndex == index) if (args.localIndex == index)
{ {
nextSearchExpression = next; nextSearchExpression = next;
return true; return true;
@ -143,7 +124,7 @@ public static class SearchExpressionCompiler
{ {
public bool DoesMatch(SearchArgs args, out ISearchExpression? nextSearchExpression) public bool DoesMatch(SearchArgs args, out ISearchExpression? nextSearchExpression)
{ {
if ((args.KeySB != null && args.KeySB.Equals(key)) || args.KeyStr == key) if (args.key == key)
{ {
nextSearchExpression = next; nextSearchExpression = next;
return true; return true;

View File

@ -12,7 +12,6 @@ global using Directory = DTLib.Filesystem.Directory;
global using File = DTLib.Filesystem.File; global using File = DTLib.Filesystem.File;
global using Path = DTLib.Filesystem.Path; global using Path = DTLib.Filesystem.Path;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Diagnostics;
using System.IO; using System.IO;
using System.Text.Encodings.Web; using System.Text.Encodings.Web;
using DTLib.Dtsod; using DTLib.Dtsod;
@ -55,21 +54,6 @@ public static partial class Program
try try
{ {
Stopwatch stopwatch = new();
using var save = File.OpenRead("data/gamestate");
stopwatch.Start();
var parser = new SaveParserEU4(save, SearchExpressionCompiler.Compile("saved_event_target"));
var result = parser.Parse();
stopwatch.Stop();
using (var resultFile = File.OpenWrite("data/parsed.json"))
{
JsonSerializer.Serialize(resultFile, result, _saveSerializerOptions);
}
Console.WriteLine($"get: {parser.SBPoolGetCount} return: {parser.SBPoolReturnCount} " +
$"delta: {parser.SBPoolGetCount - parser.SBPoolReturnCount}");
Console.WriteLine(stopwatch.Elapsed);
return;
// config // config
if (!File.Exists(_configPath)) if (!File.Exists(_configPath))
{ {