-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from Govorunb/editor-perf
Improve editor performance
- Loading branch information
Showing
6 changed files
with
172 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using HarmonyLib; | ||
using NaughtyAttributes.Editor; | ||
|
||
[HarmonyPatch] | ||
public static class NaughtyPatches | ||
{ | ||
[HarmonyPatch(typeof(ReflectionUtility))] | ||
public static class ReflectionPerfDetours | ||
{ | ||
[HarmonyPatch(nameof(ReflectionUtility.GetField))] | ||
[HarmonyPrefix] | ||
public static bool FieldLookupPerfDetour(out FieldInfo __result, object target, string fieldName) | ||
{ | ||
__result = null; | ||
if (target != null) __result = ReflectionCache.GetField(target.GetType(), fieldName); | ||
|
||
return false; | ||
} | ||
|
||
[HarmonyPatch(nameof(ReflectionUtility.GetMethod))] | ||
[HarmonyPrefix] | ||
public static bool MethodLookupPerfDetour(out MethodInfo __result, object target, string methodName) | ||
{ | ||
__result = null; | ||
if (target != null) __result = ReflectionCache.GetMethod(target.GetType(), methodName); | ||
|
||
return false; | ||
} | ||
|
||
[HarmonyPatch(nameof(ReflectionUtility.GetProperty))] | ||
[HarmonyPrefix] | ||
public static bool PropertyLookupPerfDetour(out PropertyInfo __result, object target, string propertyName) | ||
{ | ||
__result = null; | ||
if (target != null) __result = ReflectionCache.GetProperty(target.GetType(), propertyName); | ||
|
||
return false; | ||
} | ||
|
||
[HarmonyPatch("GetSelfAndBaseTypes")] | ||
[HarmonyPrefix] | ||
public static bool TypeHierarchyPerfDetour(out List<Type> __result, object target) | ||
{ | ||
__result = WalkTypeHierarchy(target?.GetType()).ToList(); | ||
return false; | ||
} | ||
public static IEnumerable<Type> WalkTypeHierarchy(Type leaf) | ||
{ | ||
for (Type curr = leaf; curr != null; curr = curr.BaseType) | ||
yield return curr; | ||
} | ||
} | ||
|
||
[HarmonyPatch(typeof(NaughtyInspector), "OnEnable")] | ||
[HarmonyPostfix] | ||
public static void EnumerablePerfDetour(NaughtyInspector __instance, | ||
ref IEnumerable<FieldInfo> ____nonSerializedFields, | ||
ref IEnumerable<PropertyInfo> ____nativeProperties, | ||
ref IEnumerable<MethodInfo> ____methods) | ||
{ | ||
if (!__instance.target) return; | ||
|
||
____nonSerializedFields = ____nonSerializedFields.ToList(); | ||
____nativeProperties = ____nativeProperties.ToList(); | ||
____methods = ____methods.ToList(); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Unity/Assets/Scripts/Editor/Patches/NaughtyPatches.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using HarmonyLib; | ||
using NaughtyAttributes.Editor; | ||
|
||
public static class ReflectionCache | ||
{ | ||
private class Cache<TKey, TValue> | ||
{ | ||
private readonly Func<TKey, TValue> getValueFunc; | ||
private readonly Dictionary<TKey, TValue> _cache = new Dictionary<TKey, TValue>(); | ||
public Cache(Func<TKey, TValue> getValueFunc) | ||
{ | ||
this.getValueFunc = getValueFunc; | ||
} | ||
public TValue GetCached(TKey key) | ||
{ | ||
if (!_cache.TryGetValue(key, out TValue value)) | ||
value = _cache[key] = getValueFunc(key); | ||
return value; | ||
} | ||
} | ||
private static readonly Cache<string, Type> _types = new Cache<string, Type>(typeName => AccessTools.TypeByName(typeName)); | ||
private static readonly Cache<Type, List<FieldInfo>> _allFields = new Cache<Type, List<FieldInfo>>( | ||
type => NaughtyPatches.ReflectionPerfDetours.WalkTypeHierarchy(type) | ||
.SelectMany(t => t.GetFields(AccessTools.all)) | ||
.ToList() | ||
); | ||
private static readonly Cache<(Type, string), FieldInfo> _fieldByName = new Cache<(Type, string), FieldInfo>( | ||
pair => NaughtyPatches.ReflectionPerfDetours.WalkTypeHierarchy(pair.Item1) | ||
.Select(t => t.GetField(pair.Item2, AccessTools.all)) | ||
.FirstOrDefault(f => f != null) | ||
); | ||
private static readonly Cache<Type, List<MethodInfo>> _allMethods = new Cache<Type, List<MethodInfo>>( | ||
type => NaughtyPatches.ReflectionPerfDetours.WalkTypeHierarchy(type) | ||
.SelectMany(t => t.GetMethods(AccessTools.all)) | ||
.ToList() | ||
); | ||
private static readonly Cache<(Type, string), MethodInfo> _methodByName = new Cache<(Type, string), MethodInfo>( | ||
pair => NaughtyPatches.ReflectionPerfDetours.WalkTypeHierarchy(pair.Item1) | ||
.Select(t => t.GetMethod(pair.Item2, AccessTools.all)) | ||
.FirstOrDefault(f => f != null) | ||
); | ||
private static readonly Cache<Type, List<PropertyInfo>> _allProperties = new Cache<Type, List<PropertyInfo>>( | ||
type => NaughtyPatches.ReflectionPerfDetours.WalkTypeHierarchy(type) | ||
.SelectMany(t => t.GetProperties(AccessTools.all)) | ||
.ToList() | ||
); | ||
private static readonly Cache<(Type, string), PropertyInfo> _propertyByName = new Cache<(Type, string), PropertyInfo>( | ||
pair => NaughtyPatches.ReflectionPerfDetours.WalkTypeHierarchy(pair.Item1) | ||
.Select(t => t.GetProperty(pair.Item2, AccessTools.all)) | ||
.FirstOrDefault(f => f != null) | ||
); | ||
private static readonly Cache<MemberInfo, List<Attribute>> _memberAttrs = new Cache<MemberInfo, List<Attribute>>(member => member.GetCustomAttributes().ToList()); | ||
|
||
public static Type GetType(string typeName) => _types.GetCached(typeName); | ||
public static List<FieldInfo> GetAllFields(Type type) => _allFields.GetCached(type); | ||
public static FieldInfo GetField(Type type, string name) => _fieldByName.GetCached((type, name)); | ||
public static List<MethodInfo> GetAllMethods(Type type) => _allMethods.GetCached(type); | ||
public static MethodInfo GetMethod(Type type, string name) => _methodByName.GetCached((type, name)); | ||
public static List<PropertyInfo> GetAllProperties(Type type) => _allProperties.GetCached(type); | ||
public static PropertyInfo GetProperty(Type type, string name) => _propertyByName.GetCached((type, name)); | ||
|
||
public static List<Attribute> GetCustomAttributes(MemberInfo member) => _memberAttrs.GetCached(member); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.