fix: Eliminate intermediate string in typecache (#2176)

This commit is contained in:
Kamron Batman 2025-05-06 21:04:54 -07:00 • committed by GitHub
parent f0f7d44d4d
commit 4beda6a29d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 21 additions and 19 deletions

View file

@ -234,10 +234,10 @@ public class TypeCache
private static ILogger logger = LogFactory.GetLogger(typeof(TypeCache)); private static ILogger logger = LogFactory.GetLogger(typeof(TypeCache));
#endif #endif
private Dictionary<ulong, Type[]> _nameMap = new(); private readonly Dictionary<ulong, Type[]> _nameMap = [];
private Dictionary<ulong, Type[]> _nameMapInsensitive = new(); private readonly Dictionary<ulong, Type[]> _nameMapInsensitive = [];
private Dictionary<ulong, Type[]> _fullNameMap = new(); private readonly Dictionary<ulong, Type[]> _fullNameMap = [];
private Dictionary<ulong, Type[]> _fullNameMapInsensitive = new(); private readonly Dictionary<ulong, Type[]> _fullNameMapInsensitive = [];
public TypeCache(Assembly asm) public TypeCache(Assembly asm)
{ {
@ -248,15 +248,6 @@ public class TypeCache
var fullNameMap = new Dictionary<string, HashSet<Type>>(); var fullNameMap = new Dictionary<string, HashSet<Type>>();
var fullNameMapInsensitive = new Dictionary<string, HashSet<Type>>(); var fullNameMapInsensitive = new Dictionary<string, HashSet<Type>>();
[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); var aliasType = typeof(TypeAliasAttribute);
for (var i = 0; i < Types.Length; i++) for (var i = 0; i < Types.Length; i++)
{ {
@ -267,7 +258,7 @@ public class TypeCache
for (var j = 0; j < alias.Aliases.Length; j++) for (var j = 0; j < alias.Aliases.Length; j++)
{ {
var fullTypeName = alias.Aliases[j]; var fullTypeName = alias.Aliases[j];
var typeName = fullTypeName[(fullTypeName.LastIndexOf('.')+1)..]; var typeName = fullTypeName[(fullTypeName.AsSpan().LastIndexOf('.') + 1)..];
addTypeToRefs(current, typeName, fullTypeName); addTypeToRefs(current, typeName, fullTypeName);
} }
} }
@ -322,6 +313,17 @@ public class TypeCache
} }
#endif #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)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -338,7 +340,7 @@ public class TypeCache
} }
else else
{ {
refs = new HashSet<Type> { type }; refs = [type];
map.Add(key, refs); map.Add(key, refs);
} }
} }
@ -369,12 +371,12 @@ public class TypeCache
if (ignoreCase) if (ignoreCase)
{ {
var map = full ? cache._fullNameMapInsensitive : cache._nameMapInsensitive; var map = full ? cache._fullNameMapInsensitive : cache._nameMapInsensitive;
_values = map.TryGetValue(hash, out var values) ? values : Array.Empty<Type>(); _values = map.TryGetValue(hash, out var values) ? values : [];
} }
else else
{ {
var map = full ? cache._fullNameMap : cache._nameMap; var map = full ? cache._fullNameMap : cache._nameMap;
_values = map.TryGetValue(hash, out var values) ? values : Array.Empty<Type>(); _values = map.TryGetValue(hash, out var values) ? values : [];
} }
_index = 0; _index = 0;

View file

@ -167,7 +167,7 @@ public sealed unsafe class BinaryFileReader : IDisposable, IGenericReader
public Serial ReadSerial() => _reader.ReadSerial(); public Serial ReadSerial() => _reader.ReadSerial();
/// <summary> /// <summary>
/// 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.
/// <br>If the byte returns 1 => <see cref="ReadStringRaw"/> and translate into a Type via the <see cref="AssemblyHandler"/></br> /// <br>If the byte returns 1 => <see cref="ReadStringRaw"/> and translate into a Type via the <see cref="AssemblyHandler"/></br>
/// <br>If the byte returns 2 => <see cref="UnmanagedDataReader.ReadTypeByHash"/></br> /// <br>If the byte returns 2 => <see cref="UnmanagedDataReader.ReadTypeByHash"/></br>
/// <br>else return null</br> /// <br>else return null</br>

View file

@ -218,7 +218,7 @@ public unsafe class UnmanagedDataReader : IGenericReader
public Serial ReadSerial() => (Serial)ReadUInt(); public Serial ReadSerial() => (Serial)ReadUInt();
/// <summary> /// <summary>
/// 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.
/// <br>If the byte returns 1 => <see cref="ReadStringRaw"/> and translate into a Type via the <see cref="AssemblyHandler"/></br> /// <br>If the byte returns 1 => <see cref="ReadStringRaw"/> and translate into a Type via the <see cref="AssemblyHandler"/></br>
/// <br>If the byte returns 2 => <see cref="ReadTypeByHash"/></br> /// <br>If the byte returns 2 => <see cref="ReadTypeByHash"/></br>
/// <br>else return null</br> /// <br>else return null</br>