From 90c577bd29f9fbf7480258fcb73d11fe4dca0816 Mon Sep 17 00:00:00 2001 From: Archangel Date: Fri, 6 Feb 2026 22:41:13 +0100 Subject: [PATCH] Various allocation & GC improvements --- .../SPT_Data/configs/core.json | 2 +- .../Utils/Logger/Handlers/BaseLogHandler.cs | 33 +++++++++++++------ .../Utils/Logger/SptLogger.cs | 17 ++++++---- .../Utils/Logger/SptLoggerConfiguration.cs | 27 ++++++++++++++- SPTarkov.Server/runtimeconfig.template.json | 5 +++ 5 files changed, 65 insertions(+), 19 deletions(-) create mode 100644 SPTarkov.Server/runtimeconfig.template.json diff --git a/Libraries/SPTarkov.Server.Assets/SPT_Data/configs/core.json b/Libraries/SPTarkov.Server.Assets/SPT_Data/configs/core.json index 47196e06..9f3d1a8c 100644 --- a/Libraries/SPTarkov.Server.Assets/SPT_Data/configs/core.json +++ b/Libraries/SPTarkov.Server.Assets/SPT_Data/configs/core.json @@ -5,7 +5,7 @@ "profileSaveIntervalSeconds": 60, "sptFriendNickname": "SPT", "allowProfileWipe": true, - "enableNoGCRegions": true, + "enableNoGCRegions": false, "noGCRegionMaxMemoryGB": 4, "noGCRegionMaxLOHMemoryGB": 3, "bsgLogging": { diff --git a/Libraries/SPTarkov.Server.Core/Utils/Logger/Handlers/BaseLogHandler.cs b/Libraries/SPTarkov.Server.Core/Utils/Logger/Handlers/BaseLogHandler.cs index 2857ad98..6a4877da 100644 --- a/Libraries/SPTarkov.Server.Core/Utils/Logger/Handlers/BaseLogHandler.cs +++ b/Libraries/SPTarkov.Server.Core/Utils/Logger/Handlers/BaseLogHandler.cs @@ -8,19 +8,32 @@ public abstract class BaseLogHandler : ILogHandler protected string FormatMessage(string processedMessage, SptLogMessage message, BaseSptLoggerReference reference) { - var formattedMessage = reference - .Format.Replace("%date%", message.LogTime.ToString("yyyy-MM-dd")) - .Replace("%time%", message.LogTime.ToString("HH:mm:ss.fff")) - .Replace("%message%", processedMessage) - .Replace("%loggerShort%", message.Logger.Split('.').Last()) - .Replace("%logger%", message.Logger) - .Replace("%tid%", message.threadId.ToString()) - .Replace("%tname%", message.threadName) - .Replace("%level%", Enum.GetName(message.LogLevel)); + var format = reference.GetCompiledFormat(); + + var formattedMessage = string.Format( + null, + format, + message.LogTime.ToString("yyyy-MM-dd"), + message.LogTime.ToString("HH:mm:ss.fff"), + processedMessage, + GetLoggerShortName(message.Logger), + message.Logger, + message.threadId, + message.threadName, + message.LogLevel.ToString() + ); + 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; } + + protected string GetLoggerShortName(string logger) + { + var lastDotIndex = logger.AsSpan().LastIndexOf('.'); + return lastDotIndex >= 0 ? logger.Substring(lastDotIndex + 1) : logger; + } } diff --git a/Libraries/SPTarkov.Server.Core/Utils/Logger/SptLogger.cs b/Libraries/SPTarkov.Server.Core/Utils/Logger/SptLogger.cs index 29b7d599..0113fd6c 100644 --- a/Libraries/SPTarkov.Server.Core/Utils/Logger/SptLogger.cs +++ b/Libraries/SPTarkov.Server.Core/Utils/Logger/SptLogger.cs @@ -13,7 +13,7 @@ public class SptLogger : ISptLogger private const string ConfigurationPath = "./sptLogger.json"; private const string ConfigurationPathDev = "./sptLogger.Development.json"; - private SptLoggerConfiguration _config; + private static SptLoggerConfiguration? _config = null; public SptLogger(FileUtil fileUtil, JsonUtil jsonUtil, SptLoggerQueueManager loggerQueueManager) { @@ -27,13 +27,16 @@ public class SptLogger : ISptLogger _ => false, }; - if (!ProgramStatics.DEBUG() || IsReleaseType) + if (_config is null) { - LoadConfig(fileUtil, jsonUtil, ConfigurationPath); - } - else - { - LoadConfig(fileUtil, jsonUtil, ConfigurationPathDev); + if (!ProgramStatics.DEBUG() || IsReleaseType) + { + LoadConfig(fileUtil, jsonUtil, ConfigurationPath); + } + else + { + LoadConfig(fileUtil, jsonUtil, ConfigurationPathDev); + } } if (_config == null) diff --git a/Libraries/SPTarkov.Server.Core/Utils/Logger/SptLoggerConfiguration.cs b/Libraries/SPTarkov.Server.Core/Utils/Logger/SptLoggerConfiguration.cs index ac5affcd..11183fb1 100644 --- a/Libraries/SPTarkov.Server.Core/Utils/Logger/SptLoggerConfiguration.cs +++ b/Libraries/SPTarkov.Server.Core/Utils/Logger/SptLoggerConfiguration.cs @@ -1,4 +1,5 @@ using System.Collections.Concurrent; +using System.Text; using System.Text.Json.Serialization; using System.Text.RegularExpressions; using LogLevel = SPTarkov.Server.Core.Models.Spt.Logging.LogLevel; @@ -28,7 +29,31 @@ public abstract class BaseSptLoggerReference public LogLevel LogLevel { get; set; } [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 diff --git a/SPTarkov.Server/runtimeconfig.template.json b/SPTarkov.Server/runtimeconfig.template.json new file mode 100644 index 00000000..1cf37778 --- /dev/null +++ b/SPTarkov.Server/runtimeconfig.template.json @@ -0,0 +1,5 @@ +{ + "configProperties": { + "System.GC.ConserveMemory": 5 + } +}