From 022cd464e63a23b232a77299ad6d6fea354db798 Mon Sep 17 00:00:00 2001 From: CptMoore <39010654+cptmoore@users.noreply.github.com> Date: Tue, 21 Jan 2025 21:13:38 +0100 Subject: [PATCH] added fast starts with --- ModTek/Features/Logging/FastMemCpy.cs | 98 +++++++++++++++++++++++- ModTek/Features/Logging/Filters.cs | 104 +++++++++++++------------- 2 files changed, 147 insertions(+), 55 deletions(-) diff --git a/ModTek/Features/Logging/FastMemCpy.cs b/ModTek/Features/Logging/FastMemCpy.cs index 95ffee44..3606ad03 100644 --- a/ModTek/Features/Logging/FastMemCpy.cs +++ b/ModTek/Features/Logging/FastMemCpy.cs @@ -1,5 +1,4 @@ using System; -using System.Runtime.CompilerServices; using ModTek.Util.Stopwatch; namespace ModTek.Features.Logging; @@ -7,6 +6,103 @@ namespace ModTek.Features.Logging; // effectively used only for byte arrays sized <= 14 (112bit) internal static class FastMemCpy { + internal static unsafe bool FastStartsWith(this string text, string prefix) + { + if (text == null || prefix == null) + { + return false; + } + if (text.Length < prefix.Length) + { + return false; + } + fixed (char* prefixPtr = prefix) + { + fixed (char* textPtr = text) + { + return FastStartsWith((ushort*)prefixPtr, (ushort*)textPtr, prefix.Length); + } + } + } + + private static unsafe bool FastStartsWith(ushort* prefix, ushort* text, int size) + { + // search backwards, failures are faster that way + prefix += size; + text += size; + { + const int BatchSize = 16 * sizeof(ulong)/sizeof(ushort); + for (; size >= BatchSize; size -= BatchSize) + { + prefix -= BatchSize; + text -= BatchSize; + if ( + *((ulong*)prefix+ 0) != *((ulong*)text+ 0) + || *((ulong*)prefix+ 1) != *((ulong*)text+ 1) + || *((ulong*)prefix+ 2) != *((ulong*)text+ 2) + || *((ulong*)prefix+ 3) != *((ulong*)text+ 3) + || *((ulong*)prefix+ 4) != *((ulong*)text+ 4) + || *((ulong*)prefix+ 5) != *((ulong*)text+ 5) + || *((ulong*)prefix+ 6) != *((ulong*)text+ 6) + || *((ulong*)prefix+ 7) != *((ulong*)text+ 7) + || *((ulong*)prefix+ 8) != *((ulong*)text+ 8) + || *((ulong*)prefix+ 9) != *((ulong*)text+ 9) + || *((ulong*)prefix+10) != *((ulong*)text+10) + || *((ulong*)prefix+11) != *((ulong*)text+11) + || *((ulong*)prefix+12) != *((ulong*)text+12) + || *((ulong*)prefix+13) != *((ulong*)text+13) + || *((ulong*)prefix+14) != *((ulong*)text+14) + || *((ulong*)prefix+15) != *((ulong*)text+15) + ) + { + return false; + } + } + } + { + const int BatchSize = 4 * sizeof(ulong)/sizeof(ushort); + for (; size >= BatchSize; size -= BatchSize) + { + prefix -= BatchSize; + text -= BatchSize; + if ( + *(ulong*)prefix != *(ulong*)text + || *((ulong*)prefix+1) != *((ulong*)text+1) + || *((ulong*)prefix+2) != *((ulong*)text+2) + || *((ulong*)prefix+3) != *((ulong*)text+3) + ) + { + return false; + } + } + } + { + const int BatchSize = sizeof(ulong)/sizeof(ushort); + for (; size >= BatchSize; size -= BatchSize) + { + prefix -= BatchSize; + text -= BatchSize; + if (*(ulong*)prefix != *(ulong*)text) + { + return false; + } + } + } + { + const int BatchSize = sizeof(ushort)/sizeof(ushort); + for (; size >= BatchSize; size -= BatchSize) + { + prefix -= BatchSize; + text -= BatchSize; + if (*prefix != *text) + { + return false; + } + } + } + return true; + } + internal static unsafe void BlockCopy(byte[] src, int srcOffset, byte[] dst, int dstOffset, int length) { if (length > Threshold) // 700-1300 bytes are typical diff --git a/ModTek/Features/Logging/Filters.cs b/ModTek/Features/Logging/Filters.cs index 046692f3..138ea486 100644 --- a/ModTek/Features/Logging/Filters.cs +++ b/ModTek/Features/Logging/Filters.cs @@ -36,10 +36,6 @@ internal static class Filters private static readonly string UI = string.Intern("UI"); private static readonly string Unity = string.Intern("Unity"); private static readonly string UserSettings = string.Intern("UserSettings"); - private static unsafe bool FastStartsWith(string instance, string str) - { - return false; - } internal static bool HardcodedFilter(ref MTLoggerMessageDto messageDto) { var loggerName = messageDto.LoggerName; @@ -56,15 +52,15 @@ internal static bool HardcodedFilter(ref MTLoggerMessageDto messageDto) { if (logLevel == LogLevel.Warning) { - if (message.StartsWith("Analytics Event requested with invalid IP")) + if (message.FastStartsWith("Analytics Event requested with invalid IP")) { return false; } - if (message.StartsWith("Request next called but no servers have been found")) + if (message.FastStartsWith("Request next called but no servers have been found")) { return false; } - if (message.StartsWith("Request next called but reporting is disabled")) + if (message.FastStartsWith("Request next called but reporting is disabled")) { return false; } @@ -74,11 +70,11 @@ internal static bool HardcodedFilter(ref MTLoggerMessageDto messageDto) { if (logLevel == LogLevel.Debug) { - if (message.StartsWith("Audio Engine")) + if (message.FastStartsWith("Audio Engine")) { return false; } - if (message.StartsWith("OnApplicationFocus")) + if (message.FastStartsWith("OnApplicationFocus")) { return false; } @@ -88,7 +84,7 @@ internal static bool HardcodedFilter(ref MTLoggerMessageDto messageDto) { if (logLevel == LogLevel.Debug) { - if (message.StartsWith("[BattleTechParadoxAPI]")) + if (message.FastStartsWith("[BattleTechParadoxAPI]")) { return false; } @@ -98,7 +94,7 @@ internal static bool HardcodedFilter(ref MTLoggerMessageDto messageDto) { if (logLevel == LogLevel.Log) { - if (message.StartsWith("???????? DIRECTION:")) + if (message.FastStartsWith("???????? DIRECTION:")) { return false; } @@ -108,14 +104,14 @@ internal static bool HardcodedFilter(ref MTLoggerMessageDto messageDto) { if (logLevel == LogLevel.Error) { - if (message.StartsWith("Error instantiating VFX")) + if (message.FastStartsWith("Error instantiating VFX")) { return false; } } if (logLevel == LogLevel.Warning) { - if (message.StartsWith("Turret")) + if (message.FastStartsWith("Turret")) { return false; } @@ -125,22 +121,22 @@ internal static bool HardcodedFilter(ref MTLoggerMessageDto messageDto) { if (logLevel == LogLevel.Debug) { - if (message.StartsWith("[CC]-- CustomComponents.AutoLinked")) + if (message.FastStartsWith("[CC]-- CustomComponents.AutoLinked")) { return false; } - if (message.StartsWith("[CC]-- CustomComponents.Flags")) + if (message.FastStartsWith("[CC]-- CustomComponents.Flags")) { return false; } - if (message.StartsWith("[CC]Mech validation for NULL return")) + if (message.FastStartsWith("[CC]Mech validation for NULL return")) { return false; } } if (logLevel == LogLevel.Log) { - if (message.StartsWith("[CC]SimpleCustom")) + if (message.FastStartsWith("[CC]SimpleCustom")) { return false; } @@ -157,7 +153,7 @@ internal static bool HardcodedFilter(ref MTLoggerMessageDto messageDto) { if (logLevel == LogLevel.Log) { - if (message.StartsWith("Trying to load AssetBundle:")) + if (message.FastStartsWith("Trying to load AssetBundle:")) { return false; } @@ -167,7 +163,7 @@ internal static bool HardcodedFilter(ref MTLoggerMessageDto messageDto) { if (logLevel == LogLevel.Warning) { - if (message.StartsWith("LoadRequest for")) + if (message.FastStartsWith("LoadRequest for")) { return false; } @@ -177,7 +173,7 @@ internal static bool HardcodedFilter(ref MTLoggerMessageDto messageDto) { if (logLevel == LogLevel.Debug) { - if (message.StartsWith("Loading:")) + if (message.FastStartsWith("Loading:")) { return false; } @@ -191,15 +187,15 @@ internal static bool HardcodedFilter(ref MTLoggerMessageDto messageDto) { if (logLevel == LogLevel.Log) { - if (message.StartsWith("DEVELOPMENT FLAG NOT SET")) + if (message.FastStartsWith("DEVELOPMENT FLAG NOT SET")) { return false; } - if (message.StartsWith("ProductName: BATTLETECH")) + if (message.FastStartsWith("ProductName: BATTLETECH")) { return false; } - if (message.StartsWith("RELEASE FLAG SET")) + if (message.FastStartsWith("RELEASE FLAG SET")) { return false; } @@ -220,7 +216,7 @@ internal static bool HardcodedFilter(ref MTLoggerMessageDto messageDto) { if (logLevel == LogLevel.Debug) { - if (message.StartsWith("Fixing cost of")) + if (message.FastStartsWith("Fixing cost of")) { return false; } @@ -230,7 +226,7 @@ internal static bool HardcodedFilter(ref MTLoggerMessageDto messageDto) { if (logLevel == LogLevel.Log) { - if (message.StartsWith("Successfully authenticated with OPS. Moving onto ComStar connection proper.")) + if (message.FastStartsWith("Successfully authenticated with OPS. Moving onto ComStar connection proper.")) { return false; } @@ -240,7 +236,7 @@ internal static bool HardcodedFilter(ref MTLoggerMessageDto messageDto) { if (logLevel == LogLevel.Error) { - if (message.StartsWith("Out of bounds")) + if (message.FastStartsWith("Out of bounds")) { return false; } @@ -250,22 +246,22 @@ internal static bool HardcodedFilter(ref MTLoggerMessageDto messageDto) { if (logLevel == LogLevel.Log) { - if (message.StartsWith("Auto fixing chassisDef")) + if (message.FastStartsWith("Auto fixing chassisDef")) { return false; } - if (message.StartsWith("settings loaded")) + if (message.FastStartsWith("settings loaded")) { return false; } } if (logLevel == LogLevel.Warning) { - if (message.StartsWith("Unsupported weapon category for 666")) + if (message.FastStartsWith("Unsupported weapon category for 666")) { return false; } - if (message.StartsWith("Unsupported weapon category for 667")) + if (message.FastStartsWith("Unsupported weapon category for 667")) { return false; } @@ -275,7 +271,7 @@ internal static bool HardcodedFilter(ref MTLoggerMessageDto messageDto) { if (logLevel == LogLevel.Warning) { - if (message.StartsWith("GetWeaponPrefabName failed to find a prefab name for unit")) + if (message.FastStartsWith("GetWeaponPrefabName failed to find a prefab name for unit")) { return false; } @@ -285,7 +281,7 @@ internal static bool HardcodedFilter(ref MTLoggerMessageDto messageDto) { if (logLevel == LogLevel.Log) { - if (message.StartsWith("Connection")) + if (message.FastStartsWith("Connection")) { return false; } @@ -295,7 +291,7 @@ internal static bool HardcodedFilter(ref MTLoggerMessageDto messageDto) { if (logLevel == LogLevel.Debug) { - if (message.StartsWith("Seed")) + if (message.FastStartsWith("Seed")) { return false; } @@ -305,7 +301,7 @@ internal static bool HardcodedFilter(ref MTLoggerMessageDto messageDto) { if (logLevel == LogLevel.Debug) { - if (message.StartsWith("Seed")) + if (message.FastStartsWith("Seed")) { return false; } @@ -315,7 +311,7 @@ internal static bool HardcodedFilter(ref MTLoggerMessageDto messageDto) { if (logLevel == LogLevel.Warning) { - if (message.StartsWith("PilotDef already did Init From Save!")) + if (message.FastStartsWith("PilotDef already did Init From Save!")) { return false; } @@ -332,7 +328,7 @@ internal static bool HardcodedFilter(ref MTLoggerMessageDto messageDto) { if (logLevel == LogLevel.Warning) { - if (message.StartsWith("Singleton UIManagerFader")) + if (message.FastStartsWith("Singleton UIManagerFader")) { return false; } @@ -342,73 +338,73 @@ internal static bool HardcodedFilter(ref MTLoggerMessageDto messageDto) { if (logLevel == LogLevel.Error) { - if (message.StartsWith("================== ERROR! Found missing transforms")) + if (message.FastStartsWith("================== ERROR! Found missing transforms")) { return false; } - if (message.StartsWith("Bones do not match bindpose.")) + if (message.FastStartsWith("Bones do not match bindpose.")) { return false; } - if (message.StartsWith("Desired shader compiler platform")) + if (message.FastStartsWith("Desired shader compiler platform")) { return false; } - if (message.StartsWith("Generating SIM GAME UID while still loading!")) + if (message.FastStartsWith("Generating SIM GAME UID while still loading!")) { return false; } - if (message.StartsWith("Material doesn't have a color property")) + if (message.FastStartsWith("Material doesn't have a color property")) { return false; } - if (message.StartsWith("RenderTexture.Create failed: format unsupported for random writes")) + if (message.FastStartsWith("RenderTexture.Create failed: format unsupported for random writes")) { return false; } } if (logLevel == LogLevel.Log) { - if (message.StartsWith("Look rotation viewing vector is zero")) + if (message.FastStartsWith("Look rotation viewing vector is zero")) { return false; } } if (logLevel == LogLevel.Warning) { - if (message.StartsWith("BoxColliders does not support negative scale or size")) + if (message.FastStartsWith("BoxColliders does not support negative scale or size")) { return false; } - if (message.StartsWith("Could not retrieve member")) + if (message.FastStartsWith("Could not retrieve member")) { return false; } - if (message.StartsWith("ERROR! Couldn't auto-setup")) + if (message.FastStartsWith("ERROR! Couldn't auto-setup")) { return false; } - if (message.StartsWith("HBS Tooltip on UI Element")) + if (message.FastStartsWith("HBS Tooltip on UI Element")) { return false; } - if (message.StartsWith("No DefaultCombatLeaderCastDefId specified for faction [Player1sMercUnit]")) + if (message.FastStartsWith("No DefaultCombatLeaderCastDefId specified for faction [Player1sMercUnit]")) { return false; } - if (message.StartsWith("No DefaultCombatLeaderCastDefId specified for faction [Player2sMercUnit]")) + if (message.FastStartsWith("No DefaultCombatLeaderCastDefId specified for faction [Player2sMercUnit]")) { return false; } - if (message.StartsWith("Parent of RectTransform is being set with parent property")) + if (message.FastStartsWith("Parent of RectTransform is being set with parent property")) { return false; } - if (message.StartsWith("Rejecting unit")) + if (message.FastStartsWith("Rejecting unit")) { return false; } - if (message.StartsWith("The referenced script")) + if (message.FastStartsWith("The referenced script")) { return false; } @@ -418,14 +414,14 @@ internal static bool HardcodedFilter(ref MTLoggerMessageDto messageDto) { if (logLevel == LogLevel.Debug) { - if (message.StartsWith("Using existing cloud settings from save manager")) + if (message.FastStartsWith("Using existing cloud settings from save manager")) { return false; } } if (logLevel == LogLevel.Log) { - if (message.StartsWith("[LOG] User settings saved")) + if (message.FastStartsWith("[LOG] User settings saved")) { return false; }