1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-26 06:28:35 +01:00

Improved CopyExtensions construction speed

This commit is contained in:
Ell 2021-08-05 03:59:14 +02:00
parent a9a7f2b421
commit 51833d523d
3 changed files with 17 additions and 13 deletions

View file

@ -45,6 +45,7 @@ Additions
Improvements Improvements
- Improved RawContentManager's reader loading and added better exception handling - Improved RawContentManager's reader loading and added better exception handling
- Improved CopyExtensions construction speed
## 5.0.0 ## 5.0.0
### MLEM ### MLEM

View file

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
@ -9,6 +10,7 @@ namespace MLEM.Data {
public static class CopyExtensions { public static class CopyExtensions {
private const BindingFlags DefaultFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; private const BindingFlags DefaultFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
private static readonly Dictionary<Type, ConstructorInfo> ConstructorCache = new Dictionary<Type, ConstructorInfo>();
/// <summary> /// <summary>
/// Creates a shallow copy of the object and returns it. /// 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) { private static object Construct(Type t, BindingFlags flags) {
var constructors = t.GetConstructors(flags); if (!ConstructorCache.TryGetValue(t, out var constructor)) {
// find a contructor with the correct attribute var constructors = t.GetConstructors(flags);
var constructor = constructors.FirstOrDefault(c => c.GetCustomAttribute<CopyConstructorAttribute>() != null); // find a contructor with the correct attribute
// find a parameterless construcotr constructor = constructors.FirstOrDefault(c => c.GetCustomAttribute<CopyConstructorAttribute>() != null);
if (constructor == null) // find a parameterless construcotr
constructor = t.GetConstructor(flags, null, Type.EmptyTypes, null); if (constructor == null)
// fall back to the first constructor constructor = t.GetConstructor(flags, null, Type.EmptyTypes, null);
if (constructor == null) // fall back to the first constructor
constructor = constructors.FirstOrDefault(); if (constructor == null)
if (constructor == null) constructor = constructors.FirstOrDefault();
throw new NullReferenceException($"Type {t} does not have a constructor with the required visibility"); 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]); return constructor.Invoke(new object[constructor.GetParameters().Length]);
} }

View file

@ -1,8 +1,6 @@
using System.Text;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using MLEM.Extensions; using MLEM.Extensions;
using MLEM.Font;
namespace MLEM.Ui.Style { namespace MLEM.Ui.Style {
/// <summary> /// <summary>