removed debug code and fixed some bugs

This commit is contained in:
Timerix 2025-04-05 08:51:04 +05:00
parent f3106769d9
commit c4ab4028ae
3 changed files with 6 additions and 29 deletions

View File

@ -16,9 +16,6 @@ public class SaveParserEU4
private readonly ObjectPool<StringBuilder> _stringBuilderPool; private readonly ObjectPool<StringBuilder> _stringBuilderPool;
private ISearchExpression? _searchExprCurrent; private ISearchExpression? _searchExprCurrent;
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
/// </param> /// </param>
@ -50,7 +47,6 @@ public class SaveParserEU4
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 strb = _stringBuilderPool.Get();
SBPoolGetCount++;
int line = 2; int line = 2;
int column = 0; int column = 0;
bool isQuoteOpen = false; bool isQuoteOpen = false;
@ -80,7 +76,6 @@ public class SaveParserEU4
value = strb, value = strb,
}; };
strb = _stringBuilderPool.Get(); strb = _stringBuilderPool.Get();
SBPoolGetCount++;
isStrInQuotes = false; isStrInQuotes = false;
return true; return true;
} }
@ -95,7 +90,6 @@ public class SaveParserEU4
if (TryCompleteStringToken()) if (TryCompleteStringToken())
yield return strToken; yield return strToken;
_stringBuilderPool.Return(strb); _stringBuilderPool.Return(strb);
SBPoolReturnCount++;
yield break; yield break;
case '\"': case '\"':
isQuoteOpen = !isQuoteOpen; isQuoteOpen = !isQuoteOpen;
@ -150,7 +144,6 @@ public class SaveParserEU4
} }
_stringBuilderPool.Return(strb); _stringBuilderPool.Return(strb);
SBPoolReturnCount++;
} }
@ -161,9 +154,12 @@ public class SaveParserEU4
switch (tok.type) switch (tok.type)
{ {
case TokenType.StringOrNumber: case TokenType.StringOrNumber:
string tokStr = tok.value!.ToString(); // string values can be empty
if(tok.value!.Length == 0)
return string.Empty;
string tokStr = tok.value.ToString();
_stringBuilderPool.Return(tok.value); _stringBuilderPool.Return(tok.value);
SBPoolReturnCount++;
if (tokStr[0] != '-' && !char.IsDigit(tokStr[0])) if (tokStr[0] != '-' && !char.IsDigit(tokStr[0]))
return tokStr; return tokStr;
if (tokStr.Contains('.') && double.TryParse(tokStr, out double d)) if (tokStr.Contains('.') && double.TryParse(tokStr, out double d))
@ -194,7 +190,6 @@ public class SaveParserEU4
return true; return true;
case TokenType.StringOrNumber: case TokenType.StringOrNumber:
_stringBuilderPool.Return(tok.value!); _stringBuilderPool.Return(tok.value!);
SBPoolReturnCount++;
return true; return true;
case TokenType.Equals: case TokenType.Equals:
return true; return true;
@ -218,7 +213,6 @@ public class SaveParserEU4
else if (tok.type == TokenType.StringOrNumber) else if (tok.type == TokenType.StringOrNumber)
{ {
_stringBuilderPool.Return(tok.value!); _stringBuilderPool.Return(tok.value!);
SBPoolReturnCount++;
} }
} }
} }
@ -302,7 +296,6 @@ public class SaveParserEU4
{ {
SkipValue(); SkipValue();
_stringBuilderPool.Return(keySB); _stringBuilderPool.Return(keySB);
SBPoolReturnCount++;
continue; continue;
} }
@ -315,7 +308,6 @@ public class SaveParserEU4
string keyStr = keySB.ToString(); string keyStr = keySB.ToString();
_stringBuilderPool.Return(keySB); _stringBuilderPool.Return(keySB);
SBPoolReturnCount++;
if (!dict.TryGetValue(keyStr, out var list)) if (!dict.TryGetValue(keyStr, out var list))
{ {
list = new List<object>(); list = new List<object>();

View File

@ -109,7 +109,7 @@ public partial class Program
} }
finally finally
{ {
GC.Collect(); GC.Collect(GC.MaxGeneration, GCCollectionMode.Aggressive, false, true);
} }
return await ReturnResponseJson(ctx, meta); return await ReturnResponseJson(ctx, meta);

View File

@ -55,21 +55,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))
{ {