fix(core): Fixes type caching & cleanup (#422)
- [X] Fixes some issues with the type caching - [X] Changes type check default to ignore case - [X] Splits out type caching of insensitive and sensitive lists. This means less stuff to iterate through when checking a type against the string. - [X] Adds an ArrayEnumerator, mostly for reference purposes (copy/paste as needed)
This commit is contained in:
parent
b921c38879
commit
e869c105d0
35 changed files with 253 additions and 96 deletions
|
|
@ -90,35 +90,32 @@ namespace Server
|
|||
return m_TypeCaches[asm] = new TypeCache(asm);
|
||||
}
|
||||
|
||||
private static bool IgnoreCaseTypeComparer(string name, Type type) =>
|
||||
type.FullName.InsensitiveEquals(name) || type.Name.InsensitiveEquals(name);
|
||||
|
||||
private static bool CaseTypeComparer(string name, Type type) =>
|
||||
type.FullName.EqualsOrdinal(name) || type.Name.EqualsOrdinal(name);
|
||||
|
||||
public static Type FindFirstTypeForName(string name, bool ignoreCase = false, Func<string, Type, bool> predicate = null)
|
||||
public static Type FindTypeByFullName(string name, bool ignoreCase = true)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(name))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var types = FindTypesByName(name, ignoreCase);
|
||||
|
||||
if (types.Count == 0)
|
||||
if (ignoreCase)
|
||||
{
|
||||
return null;
|
||||
name = name.ToLower();
|
||||
}
|
||||
|
||||
// Try to find the closest match if there is no predicate.
|
||||
// Check for exact match of the FullName or Name
|
||||
// Then check for case-insensitive match of FullName or Name
|
||||
// Otherwise just return the first entry
|
||||
predicate ??= ignoreCase ? IgnoreCaseTypeComparer : CaseTypeComparer;
|
||||
|
||||
foreach (var type in types)
|
||||
for (var i = 0; i < Assemblies.Length; i++)
|
||||
{
|
||||
if (predicate(name, type))
|
||||
foreach (var type in GetTypeCache(Assemblies[i]).GetEnumerator(name, ignoreCase))
|
||||
{
|
||||
if (type.FullName.EqualsOrdinal(name))
|
||||
{
|
||||
return type;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach(var type in GetTypeCache(Core.Assembly).GetEnumerator(name, ignoreCase))
|
||||
{
|
||||
if (type.FullName.EqualsOrdinal(name))
|
||||
{
|
||||
return type;
|
||||
}
|
||||
|
|
@ -127,7 +124,36 @@ namespace Server
|
|||
return null;
|
||||
}
|
||||
|
||||
public static List<Type> FindTypesByName(string name, bool ignoreCase = false)
|
||||
public static Type FindTypeByName(string name, bool ignoreCase = true)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(name))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (ignoreCase)
|
||||
{
|
||||
name = name.ToLower();
|
||||
}
|
||||
|
||||
for (var i = 0; i < Assemblies.Length; i++)
|
||||
{
|
||||
foreach (var type in GetTypeCache(Assemblies[i]).GetEnumerator(name, ignoreCase))
|
||||
{
|
||||
return type;
|
||||
}
|
||||
}
|
||||
|
||||
foreach(var type in GetTypeCache(Core.Assembly).GetEnumerator(name, ignoreCase))
|
||||
{
|
||||
return type;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
// TODO: Change to IEnumerable using another custom enumerator
|
||||
public static List<Type> FindTypesByFullName(string name, bool ignoreCase = true)
|
||||
{
|
||||
var types = new List<Type>();
|
||||
|
||||
|
|
@ -138,20 +164,49 @@ namespace Server
|
|||
|
||||
for (var i = 0; i < Assemblies.Length; i++)
|
||||
{
|
||||
foreach (var type in GetTypeCache(Assemblies[i])[name])
|
||||
foreach (var type in GetTypeCache(Assemblies[i]).GetEnumerator(name, ignoreCase))
|
||||
{
|
||||
if (type.FullName.EqualsOrdinal(name))
|
||||
{
|
||||
types.Add(type);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach(var type in GetTypeCache(Core.Assembly).GetEnumerator(name, ignoreCase))
|
||||
{
|
||||
if (type.FullName.EqualsOrdinal(name))
|
||||
{
|
||||
types.Add(type);
|
||||
}
|
||||
}
|
||||
|
||||
if (types.Count == 0)
|
||||
return types;
|
||||
}
|
||||
|
||||
// TODO: Change to IEnumerable using another custom enumerator
|
||||
public static List<Type> FindTypesByName(string name, bool ignoreCase = true)
|
||||
{
|
||||
var types = new List<Type>();
|
||||
|
||||
if (ignoreCase)
|
||||
{
|
||||
foreach(var type in GetTypeCache(Core.Assembly)[name])
|
||||
name = name.ToLower();
|
||||
}
|
||||
|
||||
for (var i = 0; i < Assemblies.Length; i++)
|
||||
{
|
||||
foreach (var type in GetTypeCache(Assemblies[i]).GetEnumerator(name, ignoreCase))
|
||||
{
|
||||
types.Add(type);
|
||||
}
|
||||
}
|
||||
|
||||
foreach(var type in GetTypeCache(Core.Assembly).GetEnumerator(name, ignoreCase))
|
||||
{
|
||||
types.Add(type);
|
||||
}
|
||||
|
||||
return types;
|
||||
}
|
||||
|
||||
|
|
@ -166,52 +221,64 @@ namespace Server
|
|||
|
||||
public class TypeCache
|
||||
{
|
||||
private readonly Dictionary<string, int[]> m_NameMap = new();
|
||||
private readonly Dictionary<string, int[]> _nameMap = new();
|
||||
private readonly Dictionary<string, int[]> _nameMapInsensitive = new();
|
||||
|
||||
public TypeCache(Assembly asm)
|
||||
{
|
||||
Types = asm?.GetTypes() ?? Type.EmptyTypes;
|
||||
|
||||
var nameMap = new Dictionary<string, HashSet<int>>();
|
||||
HashSet<int> refs;
|
||||
Action<int, string> addToRefs = (index, key) =>
|
||||
var nameMapInsensitive = new Dictionary<string, HashSet<int>>();
|
||||
|
||||
void addToRefs(int index, string key, Dictionary<string, HashSet<int>> map)
|
||||
{
|
||||
if (nameMap.TryGetValue(key, out refs))
|
||||
if (key == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (map.TryGetValue(key, out var refs))
|
||||
{
|
||||
refs.Add(index);
|
||||
}
|
||||
else
|
||||
{
|
||||
refs = new HashSet<int> { index };
|
||||
nameMap.Add(key, refs);
|
||||
map.Add(key, refs);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
var aliasType = typeof(TypeAliasAttribute);
|
||||
for (var i = 0; i < Types.Length; i++)
|
||||
{
|
||||
var current = Types[i];
|
||||
addToRefs(i, current.Name);
|
||||
addToRefs(i, current.Name.ToLower());
|
||||
addToRefs(i, current.FullName);
|
||||
addToRefs(i, current.FullName?.ToLower());
|
||||
addToRefs(i, current.Name, nameMap);
|
||||
addToRefs(i, current.Name.ToLower(), nameMapInsensitive);
|
||||
addToRefs(i, current.FullName, nameMap);
|
||||
addToRefs(i, current.FullName?.ToLower(), nameMapInsensitive);
|
||||
if (current.GetCustomAttribute(aliasType, false) is TypeAliasAttribute alias)
|
||||
{
|
||||
for (var j = 0; j < alias.Aliases.Length; j++)
|
||||
{
|
||||
addToRefs(i, alias.Aliases[j]);
|
||||
addToRefs(i, alias.Aliases[j].ToLower());
|
||||
addToRefs(i, alias.Aliases[j], nameMap);
|
||||
addToRefs(i, alias.Aliases[j], nameMapInsensitive);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var (key, value) in nameMap)
|
||||
{
|
||||
m_NameMap[key] = value.ToArray();
|
||||
_nameMap[key] = value.ToArray();
|
||||
}
|
||||
|
||||
foreach (var (key, value) in nameMapInsensitive)
|
||||
{
|
||||
_nameMapInsensitive[key] = value.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
public Enumerator this[string name] => new(name, this);
|
||||
public Enumerator GetEnumerator(string name, bool ignoreCase) => new(name, this, ignoreCase);
|
||||
|
||||
public Type[] Types { get; }
|
||||
|
||||
|
|
@ -222,10 +289,12 @@ namespace Server
|
|||
private int _index;
|
||||
private Type _current;
|
||||
|
||||
internal Enumerator(string name, TypeCache cache)
|
||||
internal Enumerator(string name, TypeCache cache, bool ignoreCase)
|
||||
{
|
||||
_cache = cache;
|
||||
_values = !cache.m_NameMap.TryGetValue(name, out var values) ? Array.Empty<int>() : values;
|
||||
|
||||
var map = ignoreCase ? _cache._nameMap : _cache._nameMapInsensitive;
|
||||
_values = map.TryGetValue(name, out var values) ? values : Array.Empty<int>();
|
||||
_index = 0;
|
||||
_current = default;
|
||||
}
|
||||
|
|
@ -274,7 +343,7 @@ namespace Server
|
|||
|
||||
public IEnumerator<Type> GetEnumerator() => this;
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
IEnumerator IEnumerable.GetEnumerator() => this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Server.Buffers;
|
||||
|
||||
namespace Server
|
||||
|
|
@ -163,7 +164,8 @@ namespace Server
|
|||
|
||||
public static bool operator <(ClientVersion l, ClientVersion r) => Compare(l, r) < 0;
|
||||
|
||||
public override int GetHashCode() => Major ^ Minor ^ Revision ^ Patch ^ (int)Type;
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override int GetHashCode() => HashCode.Combine(Major, Minor, Revision, Patch, Type);
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
|
|
|
|||
79
Projects/Server/Collections/ArrayEnumerator.cs
Normal file
79
Projects/Server/Collections/ArrayEnumerator.cs
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright (C) 2019-2021 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: ArrayEnumerator.cs *
|
||||
* *
|
||||
* This program is free software: you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation, either version 3 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
||||
*************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Server.Collections
|
||||
{
|
||||
/// <summary>
|
||||
/// Non-thread safe, non-guarded enumerator for classes that have internal arrays.
|
||||
/// Recommended to copy this and use it as a nested struct.
|
||||
/// Recommend adding version checking to properly guard against modification during enumeration.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public struct ArrayEnumerator<T> : IEnumerator<T>
|
||||
{
|
||||
private readonly T[] _array;
|
||||
private int _index;
|
||||
private T? _current;
|
||||
|
||||
public ArrayEnumerator(T[] array)
|
||||
{
|
||||
_array = array;
|
||||
_index = 0;
|
||||
_current = default;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
|
||||
public bool MoveNext()
|
||||
{
|
||||
T[] localList = _array;
|
||||
|
||||
if ((uint)_index < (uint)localList.Length)
|
||||
{
|
||||
_current = _array[_index++];
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public T? Current => _current!;
|
||||
|
||||
object IEnumerator.Current
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_index == 0 || _index == _array.Length + 1)
|
||||
{
|
||||
throw new InvalidOperationException(nameof(_index));
|
||||
}
|
||||
|
||||
return _current;
|
||||
}
|
||||
}
|
||||
|
||||
void IEnumerator.Reset()
|
||||
{
|
||||
_index = 0;
|
||||
_current = default;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -609,7 +609,7 @@ namespace Server.Collections
|
|||
|
||||
public struct Enumerator : IEnumerator<TValue>
|
||||
{
|
||||
private readonly OrderedHashSet<TValue> _OrderedHashSet;
|
||||
private readonly OrderedHashSet<TValue> _orderedHashSet;
|
||||
private readonly int _version;
|
||||
private int _index;
|
||||
private TValue _current;
|
||||
|
|
@ -618,10 +618,10 @@ namespace Server.Collections
|
|||
|
||||
object IEnumerator.Current => _current;
|
||||
|
||||
internal Enumerator(OrderedHashSet<TValue> OrderedHashSet)
|
||||
internal Enumerator(OrderedHashSet<TValue> orderedHashSet)
|
||||
{
|
||||
_OrderedHashSet = OrderedHashSet;
|
||||
_version = OrderedHashSet._version;
|
||||
_orderedHashSet = orderedHashSet;
|
||||
_version = orderedHashSet._version;
|
||||
_index = 0;
|
||||
_current = default;
|
||||
}
|
||||
|
|
@ -632,14 +632,14 @@ namespace Server.Collections
|
|||
|
||||
public bool MoveNext()
|
||||
{
|
||||
if (_version != _OrderedHashSet._version)
|
||||
if (_version != _orderedHashSet._version)
|
||||
{
|
||||
throw new InvalidOperationException(InvalidOperation_EnumFailedVersion);
|
||||
}
|
||||
|
||||
if (_index < _OrderedHashSet.Count)
|
||||
if (_index < _orderedHashSet.Count)
|
||||
{
|
||||
Entry entry = _OrderedHashSet._entries[_index];
|
||||
Entry entry = _orderedHashSet._entries[_index];
|
||||
_current = entry.Value;
|
||||
++_index;
|
||||
return true;
|
||||
|
|
@ -650,7 +650,7 @@ namespace Server.Collections
|
|||
|
||||
void IEnumerator.Reset()
|
||||
{
|
||||
if (_version != _OrderedHashSet._version)
|
||||
if (_version != _orderedHashSet._version)
|
||||
{
|
||||
throw new InvalidOperationException(InvalidOperation_EnumFailedVersion);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ namespace Server.Json
|
|||
throw new JsonException("The JSON value could not be converted to System.Type");
|
||||
}
|
||||
|
||||
return AssemblyHandler.FindFirstTypeForName(reader.GetString());
|
||||
return AssemblyHandler.FindTypeByName(reader.GetString());
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, Type value, JsonSerializerOptions options) =>
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Server
|
||||
{
|
||||
|
|
@ -130,6 +131,7 @@ namespace Server
|
|||
|
||||
public override string ToString() => $"0x{BodyID:X}";
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override int GetHashCode() => BodyID.GetHashCode();
|
||||
|
||||
public override bool Equals(object o) => o is Body b && b.BodyID == BodyID;
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ namespace Server
|
|||
|
||||
foreach (var json in regions)
|
||||
{
|
||||
var type = AssemblyHandler.FindFirstTypeForName(json.Type);
|
||||
var type = AssemblyHandler.FindTypeByName(json.Type);
|
||||
|
||||
if (type == null || !typeof(Region).IsAssignableFrom(type))
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Server
|
||||
{
|
||||
|
|
@ -17,6 +18,7 @@ namespace Server
|
|||
|
||||
public bool IsValid => Value > 0;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override int GetHashCode() => Value.GetHashCode();
|
||||
|
||||
public int CompareTo(Serial other) => Value.CompareTo(other.Value);
|
||||
|
|
|
|||
|
|
@ -805,9 +805,9 @@ namespace Server
|
|||
[CommandProperty(AccessLevel.Counselor)]
|
||||
public Skill Throwing => this[SkillName.Throwing];
|
||||
|
||||
public Enumerator GetEnumerator() => new(this);
|
||||
IEnumerator<Skill> IEnumerable<Skill>.GetEnumerator() => new Enumerator(this);
|
||||
IEnumerator IEnumerable.GetEnumerator() => new Enumerator(this);
|
||||
public Enumerator GetEnumerator() => new(m_Skills);
|
||||
IEnumerator<Skill> IEnumerable<Skill>.GetEnumerator() => GetEnumerator();
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
|
||||
public override string ToString() => "...";
|
||||
|
||||
|
|
@ -898,11 +898,11 @@ namespace Server
|
|||
|
||||
public struct Enumerator : IEnumerator<Skill>
|
||||
{
|
||||
private readonly Skills _skills;
|
||||
private readonly Skill[] _skills;
|
||||
private int _index;
|
||||
private Skill _current;
|
||||
|
||||
internal Enumerator(Skills skills)
|
||||
internal Enumerator(Skill[] skills)
|
||||
{
|
||||
_skills = skills;
|
||||
_index = 0;
|
||||
|
|
@ -915,11 +915,11 @@ namespace Server
|
|||
|
||||
public bool MoveNext()
|
||||
{
|
||||
Skills localList = _skills;
|
||||
Skill[] localList = _skills;
|
||||
|
||||
while ((uint)_index < (uint)localList.Length)
|
||||
{
|
||||
_current = localList.m_Skills[_index++];
|
||||
_current = _skills[_index++];
|
||||
if (_current != null)
|
||||
{
|
||||
return true;
|
||||
|
|
|
|||
|
|
@ -191,7 +191,7 @@ namespace Server
|
|||
{
|
||||
var typeName = tdbReader.ReadString();
|
||||
|
||||
var t = AssemblyHandler.FindFirstTypeForName(typeName);
|
||||
var t = AssemblyHandler.FindTypeByFullName(typeName, false);
|
||||
|
||||
if (t?.IsAbstract != false)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue