Various allocation & GC improvements

This commit is contained in:
Archangel
2026-02-06 22:41:13 +01:00
parent d8bbbbeef3
commit 90c577bd29
5 changed files with 65 additions and 19 deletions
@@ -5,7 +5,7 @@
"profileSaveIntervalSeconds": 60, "profileSaveIntervalSeconds": 60,
"sptFriendNickname": "SPT", "sptFriendNickname": "SPT",
"allowProfileWipe": true, "allowProfileWipe": true,
"enableNoGCRegions": true, "enableNoGCRegions": false,
"noGCRegionMaxMemoryGB": 4, "noGCRegionMaxMemoryGB": 4,
"noGCRegionMaxLOHMemoryGB": 3, "noGCRegionMaxLOHMemoryGB": 3,
"bsgLogging": { "bsgLogging": {
@@ -8,19 +8,32 @@ public abstract class BaseLogHandler : ILogHandler
protected string FormatMessage(string processedMessage, SptLogMessage message, BaseSptLoggerReference reference) protected string FormatMessage(string processedMessage, SptLogMessage message, BaseSptLoggerReference reference)
{ {
var formattedMessage = reference var format = reference.GetCompiledFormat();
.Format.Replace("%date%", message.LogTime.ToString("yyyy-MM-dd"))
.Replace("%time%", message.LogTime.ToString("HH:mm:ss.fff")) var formattedMessage = string.Format(
.Replace("%message%", processedMessage) null,
.Replace("%loggerShort%", message.Logger.Split('.').Last()) format,
.Replace("%logger%", message.Logger) message.LogTime.ToString("yyyy-MM-dd"),
.Replace("%tid%", message.threadId.ToString()) message.LogTime.ToString("HH:mm:ss.fff"),
.Replace("%tname%", message.threadName) processedMessage,
.Replace("%level%", Enum.GetName(message.LogLevel)); GetLoggerShortName(message.Logger),
message.Logger,
message.threadId,
message.threadName,
message.LogLevel.ToString()
);
if (message.Exception != null) if (message.Exception != null)
{ {
formattedMessage += $"\n{message.Exception.Message}\n{message.Exception.StackTrace}"; return string.Concat(formattedMessage, "\n", message.Exception.Message, "\n", message.Exception.StackTrace);
} }
return formattedMessage; return formattedMessage;
} }
protected string GetLoggerShortName(string logger)
{
var lastDotIndex = logger.AsSpan().LastIndexOf('.');
return lastDotIndex >= 0 ? logger.Substring(lastDotIndex + 1) : logger;
}
} }
@@ -13,7 +13,7 @@ public class SptLogger<T> : ISptLogger<T>
private const string ConfigurationPath = "./sptLogger.json"; private const string ConfigurationPath = "./sptLogger.json";
private const string ConfigurationPathDev = "./sptLogger.Development.json"; private const string ConfigurationPathDev = "./sptLogger.Development.json";
private SptLoggerConfiguration _config; private static SptLoggerConfiguration? _config = null;
public SptLogger(FileUtil fileUtil, JsonUtil jsonUtil, SptLoggerQueueManager loggerQueueManager) public SptLogger(FileUtil fileUtil, JsonUtil jsonUtil, SptLoggerQueueManager loggerQueueManager)
{ {
@@ -27,13 +27,16 @@ public class SptLogger<T> : ISptLogger<T>
_ => false, _ => false,
}; };
if (!ProgramStatics.DEBUG() || IsReleaseType) if (_config is null)
{ {
LoadConfig(fileUtil, jsonUtil, ConfigurationPath); if (!ProgramStatics.DEBUG() || IsReleaseType)
} {
else LoadConfig(fileUtil, jsonUtil, ConfigurationPath);
{ }
LoadConfig(fileUtil, jsonUtil, ConfigurationPathDev); else
{
LoadConfig(fileUtil, jsonUtil, ConfigurationPathDev);
}
} }
if (_config == null) if (_config == null)
@@ -1,4 +1,5 @@
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Text;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using LogLevel = SPTarkov.Server.Core.Models.Spt.Logging.LogLevel; using LogLevel = SPTarkov.Server.Core.Models.Spt.Logging.LogLevel;
@@ -28,7 +29,31 @@ public abstract class BaseSptLoggerReference
public LogLevel LogLevel { get; set; } public LogLevel LogLevel { get; set; }
[JsonPropertyName("format")] [JsonPropertyName("format")]
public string Format { get; set; } public required string Format { get; set; }
private string? _cachedFormat;
private CompositeFormat? _compiledFormat;
public virtual CompositeFormat GetCompiledFormat()
{
if (_cachedFormat != Format)
{
var convertedFormat = Format
.Replace("%date%", "{0}")
.Replace("%time%", "{1}")
.Replace("%message%", "{2}")
.Replace("%loggerShort%", "{3}")
.Replace("%logger%", "{4}")
.Replace("%tid%", "{5}")
.Replace("%tname%", "{6}")
.Replace("%level%", "{7}");
_compiledFormat = CompositeFormat.Parse(convertedFormat);
_cachedFormat = Format;
}
return _compiledFormat!;
}
} }
public class SptLoggerFilter public class SptLoggerFilter
@@ -0,0 +1,5 @@
{
"configProperties": {
"System.GC.ConserveMemory": 5
}
}