Skip to content

Commit fd184c2

Browse files
committed
perf: Minor CPU and garbage improvement in TypeNameFormatter
1 parent bb17e6f commit fd184c2

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

Editor/Util/TypeNameFormatter.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ public static string Format(Type type, string fullTypeName, Grouping grouping)
3939
/// <summary>Gets the name of the type without its namespace.</summary>
4040
/// <param name="fullTypeName">Full name of the type including its namespace.</param>
4141
/// <returns>The type name without namespaces.</returns>
42-
public static string GetShortName(string fullTypeName) => fullTypeName.Split('.').Last();
42+
public static string GetShortName(string fullTypeName)
43+
{
44+
int afterLastDot = fullTypeName.LastIndexOf('.') + 1;
45+
return fullTypeName.Substring(afterLastDot, fullTypeName.Length - afterLastDot);
46+
}
4347

4448
private static string FormatByNamespace(string name)
4549
{
@@ -48,20 +52,20 @@ private static string FormatByNamespace(string name)
4852

4953
private static string FormatByNamespaceFlat(string name)
5054
{
51-
int lastPeriodIndex = name.LastIndexOf('.');
52-
if (lastPeriodIndex != -1)
53-
name = name.Substring(0, lastPeriodIndex) + "/" + name.Substring(lastPeriodIndex + 1);
55+
int lastDotIndex = name.LastIndexOf('.');
5456

55-
return name;
57+
return lastDotIndex == -1
58+
? name
59+
: $"{name.Substring(0, lastDotIndex)}/{name.Substring(lastDotIndex + 1)}";
5660
}
5761

5862
private static string FormatByAddComponentMenu(Type type, string name)
5963
{
6064
var addComponentMenuAttributes = type.GetCustomAttributes(typeof(AddComponentMenu), false);
61-
if (addComponentMenuAttributes.Length == 1)
62-
return ((AddComponentMenu) addComponentMenuAttributes[0]).componentMenu;
6365

64-
return "Scripts/" + FormatByNamespace(name);
66+
return addComponentMenuAttributes.Length == 1
67+
? ((AddComponentMenu) addComponentMenuAttributes[0]).componentMenu
68+
: "Scripts/" + FormatByNamespace(name);
6569
}
6670
}
6771
}

0 commit comments

Comments
 (0)