diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ecf16e..bca1a6a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,6 +45,7 @@ Additions Improvements - Improved RawContentManager's reader loading and added better exception handling +- Improved CopyExtensions construction speed ## 5.0.0 ### MLEM diff --git a/MLEM.Data/CopyExtensions.cs b/MLEM.Data/CopyExtensions.cs index ccf7bd4..a0eeb11 100644 --- a/MLEM.Data/CopyExtensions.cs +++ b/MLEM.Data/CopyExtensions.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Reflection; @@ -9,6 +10,7 @@ namespace MLEM.Data { public static class CopyExtensions { private const BindingFlags DefaultFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; + private static readonly Dictionary ConstructorCache = new Dictionary(); /// /// Creates a shallow copy of the object and returns it. @@ -85,17 +87,20 @@ namespace MLEM.Data { } private static object Construct(Type t, BindingFlags flags) { - var constructors = t.GetConstructors(flags); - // find a contructor with the correct attribute - var constructor = constructors.FirstOrDefault(c => c.GetCustomAttribute() != null); - // find a parameterless construcotr - if (constructor == null) - constructor = t.GetConstructor(flags, null, Type.EmptyTypes, null); - // fall back to the first constructor - if (constructor == null) - constructor = constructors.FirstOrDefault(); - if (constructor == null) - throw new NullReferenceException($"Type {t} does not have a constructor with the required visibility"); + if (!ConstructorCache.TryGetValue(t, out var constructor)) { + var constructors = t.GetConstructors(flags); + // find a contructor with the correct attribute + constructor = constructors.FirstOrDefault(c => c.GetCustomAttribute() != null); + // find a parameterless construcotr + if (constructor == null) + constructor = t.GetConstructor(flags, null, Type.EmptyTypes, null); + // fall back to the first constructor + if (constructor == null) + constructor = constructors.FirstOrDefault(); + if (constructor == null) + throw new NullReferenceException($"Type {t} does not have a constructor with the required visibility"); + ConstructorCache.Add(t, constructor); + } return constructor.Invoke(new object[constructor.GetParameters().Length]); } diff --git a/MLEM.Ui/Style/UntexturedStyle.cs b/MLEM.Ui/Style/UntexturedStyle.cs index 108a445..8243057 100644 --- a/MLEM.Ui/Style/UntexturedStyle.cs +++ b/MLEM.Ui/Style/UntexturedStyle.cs @@ -1,8 +1,6 @@ -using System.Text; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using MLEM.Extensions; -using MLEM.Font; namespace MLEM.Ui.Style { ///