Skip to content

Commit 83c701d

Browse files
committed
fix Inspector low fps
1 parent c137712 commit 83c701d

3 files changed

Lines changed: 58 additions & 3 deletions

File tree

‎tests/unity-project/Assets/Tests/Editor/MenSharpIntegrationTests.cs‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using UnityEngine;
1212
using UnityEngine.TestTools;
1313
using VRC.Udon;
14+
using VRC.Udon.Common.Interfaces;
1415

1516
public class MenSharpIntegrationTests
1617
{
@@ -679,5 +680,28 @@ public void NetworkCallableRatesComeFromConstantsAndAliases()
679680
Assert.AreEqual(5, rates["Aim"]);
680681
Assert.AreEqual(0, rates["Hit"]);
681682
}
683+
684+
/// The declared variable types the inspector asks for before every
685+
/// play-mode repaint come from one symbol table per build of the
686+
/// program, not from a fresh RetrieveProgram (a whole-heap
687+
/// deserialization) each time; a rebuild drops the cached table.
688+
[Test]
689+
public void DeclaredSymbolsAreReadOncePerBuild()
690+
{
691+
MenSharpCompiler.CompileAll();
692+
AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport);
693+
MenSharpProgramAsset program = MenSharpSources.FindProgram("MenSharpRuntimeSmoke");
694+
Assert.IsNotNull(program, "MenSharpRuntimeSmoke program asset");
695+
IUdonSymbolTable first = program.DeclaredSymbols();
696+
Assert.IsNotNull(first);
697+
Assert.AreEqual(typeof(int), first.GetSymbolType("indexOfObject"));
698+
Assert.IsFalse(first.HasAddressForSymbol("noSuchVariable"));
699+
Assert.AreSame(first, program.DeclaredSymbols(), "the table is kept between lookups");
700+
program.RefreshProgram();
701+
IUdonSymbolTable rebuilt = program.DeclaredSymbols();
702+
Assert.IsNotNull(rebuilt);
703+
Assert.AreNotSame(first, rebuilt, "a rebuild reads the symbols afresh");
704+
Assert.AreEqual(typeof(int), rebuilt.GetSymbolType("indexOfObject"));
705+
}
682706
}
683707
#endif

‎unity/io.tesca.mensharp/Editor/MenSharpProgramAsset.cs‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,36 @@ public MenSharpLayout LayoutOf(string typeName)
7676
return layoutsByName.TryGetValue(typeName, out MenSharpLayout found) ? found : null;
7777
}
7878

79+
/// The compiled program's symbol table, deserialized once and kept until
80+
/// the program is rebuilt. Null until first asked for.
81+
[NonSerialized]
82+
private IUdonSymbolTable declaredSymbols;
83+
84+
/// The symbol table of the program this asset carries — which public
85+
/// variables exist and the types they were declared with — or null when
86+
/// there is no program yet.
87+
///
88+
/// Cached on purpose: the SDK's RetrieveProgram copies the byte code and
89+
/// deserializes the whole heap on every call, its serialization cache
90+
/// included, and the play-mode inspector asks for the declared types
91+
/// before every repaint. Asking through RetrieveProgram each time dragged
92+
/// the editor to a few frames per second while a large program was
93+
/// selected. The symbol table never changes between two builds of the
94+
/// program, so it is read once per build (and once per domain reload,
95+
/// which drops the field).
96+
public IUdonSymbolTable DeclaredSymbols()
97+
{
98+
if (declaredSymbols == null)
99+
{
100+
declaredSymbols = program?.SymbolTable ?? SerializedProgramAsset?.RetrieveProgram()?.SymbolTable;
101+
}
102+
return declaredSymbols;
103+
}
104+
79105
protected override void RefreshProgramImpl()
80106
{
107+
// a rebuild changes the symbols: the next lookup reads them afresh
108+
declaredSymbols = null;
81109
var watch = System.Diagnostics.Stopwatch.StartNew();
82110
if (!BuildDirectly())
83111
{

‎unity/io.tesca.mensharp/Editor/MenSharpProxy.cs‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,14 +321,17 @@ private static void PinHolderToNoSync(UdonBehaviour holder, bool undoable)
321321
}
322322

323323
/// The type the compiled program declared a public variable with, from
324-
/// its symbol table (retrieved on first use); null when there is no
325-
/// program to ask or the program has no such variable.
324+
/// its symbol table; null when there is no program to ask or the program
325+
/// has no such variable. The table comes from the program asset's cache
326+
/// (one deserialization per build, see DeclaredSymbols) — this runs
327+
/// before every play-mode repaint, and retrieving the program each time
328+
/// deserialized its whole heap.
326329
private static Type DeclaredVariableType(UdonBehaviour udon, string name, ref IUdonSymbolTable symbols)
327330
{
328331
if (symbols == null)
329332
{
330333
var program = udon.programSource as MenSharpProgramAsset;
331-
symbols = program?.SerializedProgramAsset?.RetrieveProgram()?.SymbolTable;
334+
symbols = program != null ? program.DeclaredSymbols() : null;
332335
if (symbols == null)
333336
{
334337
return null;

0 commit comments

Comments
 (0)