From 4beda6a29d4ba7f17b1e307b3d7848daf1c5cfb6 Mon Sep 17 00:00:00 2001 From: Kamron Batman <3953314+kamronbatman@users.noreply.github.com> Date: Tue, 6 May 2025 21:04:54 -0700 Subject: [PATCH] fix: Eliminate intermediate string in typecache (#2176) --- Projects/Server/AssemblyHandler.cs | 36 ++++++++++--------- .../Server/Serialization/BinaryFileReader.cs | 2 +- .../Serialization/UnmanagedDataReader.cs | 2 +- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/Projects/Server/AssemblyHandler.cs b/Projects/Server/AssemblyHandler.cs index 2ff44bd4f..4e3f45e8b 100644 --- a/Projects/Server/AssemblyHandler.cs +++ b/Projects/Server/AssemblyHandler.cs @@ -234,10 +234,10 @@ public class TypeCache private static ILogger logger = LogFactory.GetLogger(typeof(TypeCache)); #endif - private Dictionary _nameMap = new(); - private Dictionary _nameMapInsensitive = new(); - private Dictionary _fullNameMap = new(); - private Dictionary _fullNameMapInsensitive = new(); + private readonly Dictionary _nameMap = []; + private readonly Dictionary _nameMapInsensitive = []; + private readonly Dictionary _fullNameMap = []; + private readonly Dictionary _fullNameMapInsensitive = []; public TypeCache(Assembly asm) { @@ -248,15 +248,6 @@ public class TypeCache var fullNameMap = new Dictionary>(); var fullNameMapInsensitive = new Dictionary>(); - [MethodImpl(MethodImplOptions.AggressiveInlining)] - void addTypeToRefs(Type type, string typeName, string fullTypeName) - { - AddToRefs(type, typeName, nameMap); - AddToRefs(type, typeName.ToLower(), nameMapInsensitive); - AddToRefs(type, fullTypeName, fullNameMap); - AddToRefs(type, fullTypeName.ToLower(), fullNameMapInsensitive); - } - var aliasType = typeof(TypeAliasAttribute); for (var i = 0; i < Types.Length; i++) { @@ -267,7 +258,7 @@ public class TypeCache for (var j = 0; j < alias.Aliases.Length; j++) { var fullTypeName = alias.Aliases[j]; - var typeName = fullTypeName[(fullTypeName.LastIndexOf('.')+1)..]; + var typeName = fullTypeName[(fullTypeName.AsSpan().LastIndexOf('.') + 1)..]; addTypeToRefs(current, typeName, fullTypeName); } } @@ -322,6 +313,17 @@ public class TypeCache } #endif } + + return; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + void addTypeToRefs(Type type, string typeName, string fullTypeName) + { + AddToRefs(type, typeName, nameMap); + AddToRefs(type, typeName.ToLower(), nameMapInsensitive); + AddToRefs(type, fullTypeName, fullNameMap); + AddToRefs(type, fullTypeName.ToLower(), fullNameMapInsensitive); + } } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -338,7 +340,7 @@ public class TypeCache } else { - refs = new HashSet { type }; + refs = [type]; map.Add(key, refs); } } @@ -369,12 +371,12 @@ public class TypeCache if (ignoreCase) { var map = full ? cache._fullNameMapInsensitive : cache._nameMapInsensitive; - _values = map.TryGetValue(hash, out var values) ? values : Array.Empty(); + _values = map.TryGetValue(hash, out var values) ? values : []; } else { var map = full ? cache._fullNameMap : cache._nameMap; - _values = map.TryGetValue(hash, out var values) ? values : Array.Empty(); + _values = map.TryGetValue(hash, out var values) ? values : []; } _index = 0; diff --git a/Projects/Server/Serialization/BinaryFileReader.cs b/Projects/Server/Serialization/BinaryFileReader.cs index f540f8e2e..d9b5c0dc1 100644 --- a/Projects/Server/Serialization/BinaryFileReader.cs +++ b/Projects/Server/Serialization/BinaryFileReader.cs @@ -167,7 +167,7 @@ public sealed unsafe class BinaryFileReader : IDisposable, IGenericReader public Serial ReadSerial() => _reader.ReadSerial(); /// - /// Reads the next Byte which helps determin how to read the following Type. + /// Reads the next Byte which helps determine how to read the following Type. ///
If the byte returns 1 => and translate into a Type via the
///
If the byte returns 2 =>
///
else return null
diff --git a/Projects/Server/Serialization/UnmanagedDataReader.cs b/Projects/Server/Serialization/UnmanagedDataReader.cs index 9babb86fd..f5a6967be 100644 --- a/Projects/Server/Serialization/UnmanagedDataReader.cs +++ b/Projects/Server/Serialization/UnmanagedDataReader.cs @@ -218,7 +218,7 @@ public unsafe class UnmanagedDataReader : IGenericReader public Serial ReadSerial() => (Serial)ReadUInt(); /// - /// Reads the next Byte which helps determin how to read the following Type. + /// Reads the next Byte which helps determine how to read the following Type. ///
If the byte returns 1 => and translate into a Type via the
///
If the byte returns 2 =>
///
else return null