Skip to content

Commit abe77da

Browse files
committed
Compiler performance improvements
- Remove the regex replacement for labels since it was taking >50% of the compile time on larger files. Switch it to just going through line by line looking for the tokens to replace - Add caching for type lookups that return null
1 parent 83372c5 commit abe77da

File tree

2 files changed

+36
-27
lines changed

2 files changed

+36
-27
lines changed

Assets/UdonSharp/Editor/UdonSharpAssemblyBuilder.cs

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//#define USE_UDON_LABELS
33

44
using System.Collections.Generic;
5+
using System.IO;
56
using System.Text;
67
using System.Text.RegularExpressions;
78

@@ -36,7 +37,7 @@ public string GetAssemblyStr(LabelTable labelTable = null)
3637
currentLabelTable = labelTable;
3738

3839
#if !USE_UDON_LABELS
39-
assemblyString = Regex.Replace(assemblyString, @"(?<whitespace>\s*)(?<labeltype>JUMP_LABEL,|JUMP_IF_FALSE_LABEL,)\s*[[](?<label>[a-zA-Z_\d]+)[\]](?<commentwhitespace>\s*)(?<comment>[#]+\s*[a-zA-Z#\s]+)*", MatchEval);
40+
assemblyString = ReplaceLabels(assemblyString, labelTable);
4041
#endif
4142

4243
currentLabelTable = null;
@@ -45,36 +46,43 @@ public string GetAssemblyStr(LabelTable labelTable = null)
4546
}
4647

4748
#if !USE_UDON_LABELS
48-
private static string MatchEval(Match match)
49+
private string ReplaceLabels(string assemblyString, LabelTable labelTable)
4950
{
50-
GroupCollection groupCollection = match.Groups;
51-
52-
string replaceStr = "";
51+
StringBuilder newAssemblyBuilder = new StringBuilder();
5352

54-
replaceStr += groupCollection["whitespace"].Value; // Whitespace
55-
56-
if (groupCollection["labeltype"].Value == "JUMP_LABEL,")
53+
using (StringReader reader = new StringReader(assemblyString))
5754
{
58-
replaceStr += "JUMP, ";
55+
string currentLine = reader.ReadLine();
56+
57+
while (currentLine != null)
58+
{
59+
string line = currentLine.TrimStart(' ', '\n', '\r');
60+
if (line.StartsWith("JUMP_LABEL,"))
61+
{
62+
int startIdx = line.IndexOf('[') + 1;
63+
int endIdx = line.IndexOf(']');
64+
string labelName = line.Substring(startIdx, endIdx - startIdx);
65+
JumpLabel label = labelTable.GetLabel(labelName);
66+
newAssemblyBuilder.AppendLine(" JUMP, " + label.AddresStr());
67+
}
68+
else if (line.StartsWith("JUMP_IF_FALSE_LABEL,"))
69+
{
70+
int startIdx = line.IndexOf('[') + 1;
71+
int endIdx = line.IndexOf(']');
72+
string labelName = line.Substring(startIdx, endIdx - startIdx);
73+
JumpLabel label = labelTable.GetLabel(labelName);
74+
newAssemblyBuilder.AppendLine(" JUMP_IF_FALSE, " + label.AddresStr());
75+
}
76+
else
77+
{
78+
newAssemblyBuilder.AppendLine(currentLine);
79+
}
80+
81+
currentLine = reader.ReadLine();
82+
}
5983
}
60-
else if (groupCollection["labeltype"].Value == "JUMP_IF_FALSE_LABEL,")
61-
{
62-
replaceStr += "JUMP_IF_FALSE, ";
63-
}
64-
65-
string labelName = groupCollection["label"].Value;
66-
67-
JumpLabel targetLabel = currentLabelTable.GetLabel(labelName);
68-
69-
replaceStr += targetLabel.AddresStr();
70-
71-
replaceStr += groupCollection["commentwhitespace"];
72-
73-
// optional comment
74-
if (groupCollection.Count > 4)
75-
replaceStr += groupCollection["comment"].Value;
7684

77-
return replaceStr;
85+
return newAssemblyBuilder.ToString();
7886
}
7987
#endif
8088

Assets/UdonSharp/Editor/UdonSharpResolverContext.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,8 @@ public System.Type ResolveExternType(string qualifiedTypeName)
256256
}
257257
}
258258
}
259-
259+
260+
typeLookupCache.Add(qualifiedTypeName, null);
260261
// We didn't find a valid type
261262
//throw new System.ArgumentException($"Could not resolve type {qualifiedTypeName}");
262263
return null;

0 commit comments

Comments
 (0)