Skip to content

Commit 0ae12b7

Browse files
committed
perf: Decreased serialization time of TypeReference
1 parent e5631e3 commit 0ae12b7

File tree

4 files changed

+43
-16
lines changed

4 files changed

+43
-16
lines changed

Editor/Util/Sedgewick.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ private static void SortInPlace(TypeItem[] input, int a, int n, int depth)
5151

5252
int r;
5353

54-
for (; ; )
54+
while (true)
5555
{
5656
while (pb <= pc && (r = empty ? (input[pb].Path.Length - depth) : ((depth == input[pb].Path.Length) ? -1 : (input[pb].Path[depth] - partVal))) <= 0)
5757
{

Editor/Util/TypeCache.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@
88
/// </summary>
99
internal static class TypeCache
1010
{
11-
private static readonly Dictionary<string, Type> TypeDict = new Dictionary<string, Type>();
11+
private static readonly Dictionary<string, Type> _typeDict = new Dictionary<string, Type>();
1212

1313
/// <summary>
1414
/// Get type from TypeCache if it is cached.
1515
/// Otherwise, find the type, cache it, and return it to the caller.
1616
/// </summary>
17-
/// <param name="typeName">Type name, followed by a comma and assembly name.</param>
17+
/// <param name="typeNameAndAssembly">Type name, followed by a comma and assembly name.</param>
1818
/// <returns>Cached class type.</returns>
19-
public static Type GetType(string typeName)
19+
public static Type GetType(string typeNameAndAssembly)
2020
{
21-
if (TypeDict.TryGetValue(typeName, out Type type))
21+
if (_typeDict.TryGetValue(typeNameAndAssembly, out Type type))
2222
return type;
2323

24-
type = ! string.IsNullOrEmpty(typeName) ? Type.GetType(typeName) : null;
25-
TypeDict[typeName] = type;
24+
type = string.IsNullOrEmpty(typeNameAndAssembly) ? null : Type.GetType(typeNameAndAssembly);
25+
_typeDict[typeNameAndAssembly] = type;
2626
return type;
2727
}
2828
}

Runtime/Attributes/InheritsAttribute.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
[AttributeUsage(AttributeTargets.Field)]
1313
public class InheritsAttribute : TypeOptionsAttribute
1414
{
15-
private readonly Type[] _baseTypes;
15+
public Type[] BaseTypes;
1616

1717
/// <summary>
1818
/// Initializes a new instance of the <see cref="InheritsAttribute"/> class using one base type and optional
@@ -27,13 +27,13 @@ public InheritsAttribute(Type baseType, [CanBeNull] params Type[] additionalBase
2727
{
2828
if (additionalBaseTypes == null || additionalBaseTypes.Length == 0)
2929
{
30-
_baseTypes = new[] { baseType };
30+
BaseTypes = new[] { baseType };
3131
}
3232
else
3333
{
34-
_baseTypes = new Type[additionalBaseTypes.Length+1];
35-
additionalBaseTypes.CopyTo(_baseTypes, 0);
36-
_baseTypes[additionalBaseTypes.Length] = baseType;
34+
BaseTypes = new Type[additionalBaseTypes.Length+1];
35+
additionalBaseTypes.CopyTo(BaseTypes, 0);
36+
BaseTypes[additionalBaseTypes.Length] = baseType;
3737
}
3838
}
3939

@@ -46,7 +46,7 @@ public InheritsAttribute(Type baseType, [CanBeNull] params Type[] additionalBase
4646
[PublicAPI]
4747
public InheritsAttribute(Type[] baseTypes)
4848
{
49-
_baseTypes = baseTypes;
49+
BaseTypes = baseTypes;
5050
}
5151

5252
/// <summary>
@@ -66,20 +66,20 @@ public InheritsAttribute(Type[] baseTypes)
6666
/// <inheritdoc/>
6767
internal override bool MatchesRequirements(Type type)
6868
{
69-
if (_baseTypes.Contains(type) && !IncludeBaseType)
69+
if (BaseTypes.Contains(type) && !IncludeBaseType)
7070
{
7171
return false;
7272
}
7373

7474
// Include base type in the drop-down even if it is abstract.
7575
// If the user set IncludeBaseType to true, they probably want to include the base type in the dropdown
7676
// even though it is abstract.
77-
if (_baseTypes.Contains(type))
77+
if (BaseTypes.Contains(type))
7878
return true;
7979

8080
bool passesAbstractConstraint = AllowAbstract || !type.IsAbstract;
8181

82-
return _baseTypes.All(type.InheritsFrom) && passesAbstractConstraint && base.MatchesRequirements(type);
82+
return BaseTypes.All(type.InheritsFrom) && passesAbstractConstraint && base.MatchesRequirements(type);
8383
}
8484
}
8585
}

Runtime/TypeReference.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,13 @@ private void LogTypeNotFoundIfNeeded()
198198

199199
private void SetClassGuidIfExists(Type type)
200200
{
201+
// common case optimization
202+
if (TypeCannotHaveGUID())
203+
{
204+
GUID = string.Empty;
205+
return;
206+
}
207+
201208
try
202209
{
203210
GUID = GetClassGUID(type);
@@ -210,6 +217,26 @@ private void SetClassGuidIfExists(Type type)
210217
}
211218
}
212219

220+
private bool TypeCannotHaveGUID()
221+
{
222+
if (string.IsNullOrEmpty(TypeNameAndAssembly))
223+
return false;
224+
225+
int charAfterWhiteSpace = TypeNameAndAssembly.IndexOf(' ') + 1;
226+
227+
string assemblyName = TypeNameAndAssembly.Substring(
228+
charAfterWhiteSpace,
229+
TypeNameAndAssembly.Length - charAfterWhiteSpace);
230+
231+
return assemblyName == "mscorlib"
232+
|| assemblyName == "netstandard"
233+
|| assemblyName.StartsWith("System.")
234+
|| assemblyName.StartsWith("Microsoft.")
235+
|| assemblyName.StartsWith("Unity.")
236+
|| assemblyName.StartsWith("UnityEngine.")
237+
|| assemblyName.StartsWith("UnityEditor.");
238+
}
239+
213240
[CanBeNull]
214241
private Type TryGetTypeFromSerializedFields()
215242
{

0 commit comments

Comments
 (0)