Skip to content

Commit

Permalink
Merge pull request #586 from Popax21/patch-1
Browse files Browse the repository at this point in the history
Speed up Type.IsSafe()
  • Loading branch information
Kalobi authored Apr 12, 2023
2 parents 61b7b99 + 9ede29d commit 725b081
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Celeste.Mod.mm/Mod/Everest/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,15 @@ public static bool IsDown(this TouchLocationState state)
public static bool IsUp(this TouchLocationState state)
=> state == TouchLocationState.Released || state == TouchLocationState.Invalid;

[ThreadStatic]
private static HashSet<string> _SafeTypes;
public static bool IsSafe(this Type type) {
_SafeTypes ??= new HashSet<string>();

try {
if (_SafeTypes.Contains(type.AssemblyQualifiedName))
return true;

// "Probe" the type
_ = type.Name;
_ = type.Assembly.FullName;
Expand All @@ -348,6 +355,7 @@ public static bool IsSafe(this Type type) {
if (!type.BaseType?.IsSafe() ?? false)
return false;

_SafeTypes.Add(type.AssemblyQualifiedName);
return true;
} catch {
return false;
Expand Down

0 comments on commit 725b081

Please sign in to comment.