Skip to content

Commit

Permalink
feat: changed datastructure of timing before getting emitted to speed…
Browse files Browse the repository at this point in the history
… up emit
  • Loading branch information
DeltaNeverUsed committed Jul 30, 2024
1 parent a88c6c6 commit a9b1d85
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructio

codes.InsertRange(
paramInsertIndex,
AddUdonReflVars(UdonProfilerConsts.StopwatchHeapKey, typeof(DataDictionary), true)
AddUdonReflVars(UdonProfilerConsts.StopwatchListKey, typeof(DataList), true)
);

codes.InsertRange(
paramInsertIndex,
AddUdonReflVars(UdonProfilerConsts.StopwatchHeapParentKey, typeof(DataDictionary), false)
AddUdonReflVars(UdonProfilerConsts.StopwatchParentKey, typeof(DataDictionary), false)
);

codes.InsertRange(
paramInsertIndex,
AddUdonReflVars(UdonProfilerConsts.StopwatchSelfKey, typeof(UdonSharpBehaviour), false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,21 @@ public class UdonSharpTimerInjector : CSharpSyntaxRewriter {
SyntaxFactory.ParseStatement(
@"{
var fakeSelf = (UdonSharp.UdonSharpBehaviour)GetProgramVariable(UdonSharpProfiler.UdonProfilerConsts.StopwatchSelfKey);
var root = (Profiler_Data.DataDictionary)fakeSelf.GetProgramVariable(UdonSharpProfiler.UdonProfilerConsts.StopwatchHeapKey);
var parent = (Profiler_Data.DataDictionary)fakeSelf.GetProgramVariable(UdonSharpProfiler.UdonProfilerConsts.StopwatchHeapParentKey);
var list = (Profiler_Data.DataList)fakeSelf.GetProgramVariable(UdonSharpProfiler.UdonProfilerConsts.StopwatchListKey);
var parent = (Profiler_Data.DataDictionary)fakeSelf.GetProgramVariable(UdonSharpProfiler.UdonProfilerConsts.StopwatchParentKey);
var info = new Profiler_Data.DataDictionary();
var name = (string)GetProgramVariable(UdonSharpProfiler.UdonProfilerConsts.StopwatchNameKey);
info.Add(""parent"", parent);
info.Add(""name"", name);
info.Add(""start"", System.Diagnostics.Stopwatch.GetTimestamp());
info.Add(""end"", (long)0);
info.Add(""children"", new Profiler_Data.DataList());
if (Profiler_Utilities.IsValid(parent)){
parent[""children""].DataList.Add(info);
}
else {
root[name] = info;
}
list.Add(info);
fakeSelf.SetProgramVariable(UdonSharpProfiler.UdonProfilerConsts.StopwatchHeapKey, root);
fakeSelf.SetProgramVariable(UdonSharpProfiler.UdonProfilerConsts.StopwatchHeapParentKey, info);
fakeSelf.SetProgramVariable(UdonSharpProfiler.UdonProfilerConsts.StopwatchListKey, list);
fakeSelf.SetProgramVariable(UdonSharpProfiler.UdonProfilerConsts.StopwatchParentKey, info);
}")
));

Expand All @@ -61,13 +54,13 @@ public class UdonSharpTimerInjector : CSharpSyntaxRewriter {
SyntaxFactory.ParseStatement(
@"{
var fakeSelf = (UdonSharp.UdonSharpBehaviour)GetProgramVariable(UdonSharpProfiler.UdonProfilerConsts.StopwatchSelfKey);
var parent = (Profiler_Data.DataDictionary)fakeSelf.GetProgramVariable(UdonSharpProfiler.UdonProfilerConsts.StopwatchHeapParentKey);
var parent = (Profiler_Data.DataDictionary)fakeSelf.GetProgramVariable(UdonSharpProfiler.UdonProfilerConsts.StopwatchParentKey);
if (Profiler_Utilities.IsValid(parent)) {
parent[""end""] = System.Diagnostics.Stopwatch.GetTimestamp();
if (parent.TryGetValue(""parent"", Profiler_Data.TokenType.DataDictionary, out var value)) {
fakeSelf.SetProgramVariable(UdonSharpProfiler.UdonProfilerConsts.StopwatchHeapParentKey, value.DataDictionary);
fakeSelf.SetProgramVariable(UdonSharpProfiler.UdonProfilerConsts.StopwatchParentKey, value.DataDictionary);
} else {
fakeSelf.SetProgramVariable(UdonSharpProfiler.UdonProfilerConsts.StopwatchHeapParentKey, null);
fakeSelf.SetProgramVariable(UdonSharpProfiler.UdonProfilerConsts.StopwatchParentKey, null);
}
}}")
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public void Emit(DataDictionary packet) {
/// Creates DataDict(s) in the form of a Perfetto packet(s) and adds it to the _packets DataList recursively
/// </summary>
/// <param name="node">Packets from the object's profiling dict</param>
[RecursiveMethod, DontUdonProfile]
private void EmitTree(DataDictionary node) {
[DontUdonProfile]
private void EmitPacket(DataDictionary node) {
var start = (long)((double)node["start"].Long / Stopwatch.Frequency * 1000000d); // in microseconds
var end = (long)((double)node["end"].Long / Stopwatch.Frequency * 1000000d); // in microseconds
var functionName = node["name"].String;
Expand All @@ -65,13 +65,6 @@ private void EmitTree(DataDictionary node) {
.AddDuration(end - start)
.AddEventType(PerfettoTrackEventType.TYPE_SLICE_COMPLETE)
.AddIds());

// Get the children
var children = node["children"].DataList.ToArray();
foreach (var child in children) {
// Repeat on that child
EmitTree(child.DataDictionary);
}
}

[DontUdonProfile]
Expand All @@ -88,16 +81,16 @@ public override void PostLateUpdate() {

if (recording) {
// Get every called function from the "root" or for example: Update, Start, or input events
var root = (DataDictionary)target.GetProgramVariable(UdonProfilerConsts.StopwatchHeapKey);
var keys = root.GetKeys().ToArray();
var root = (DataList)target.GetProgramVariable(UdonProfilerConsts.StopwatchListKey);
var unformattedPackets = root.ToArray();

foreach (var key in keys) {
EmitTree(root[key].DataDictionary);
foreach (var unformattedPacket in unformattedPackets) {
EmitPacket(unformattedPacket.DataDictionary);
}
}

// Reset Dict after getting it
target.SetProgramVariable(UdonProfilerConsts.StopwatchHeapKey, new DataDictionary());
target.SetProgramVariable(UdonProfilerConsts.StopwatchListKey, new DataList());
}

Emit(PerfettoHelper.CreatePacket()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace UdonSharpProfiler {
public static class UdonProfilerConsts {
public const string StopwatchHeapKey = "__refl_stopwatch_dict";
public const string StopwatchHeapParentKey = "__refl_stopwatch_parent_dict";
public const string StopwatchListKey = "__refl_stopwatch_list";
public const string StopwatchParentKey = "__refl_stopwatch_parent_dict";
public const string StopwatchSelfKey = "__refl_stopwatch_self";
public const string StopwatchNameKey = "__refl_stopwatch_name";

Expand Down

0 comments on commit a9b1d85

Please sign in to comment.