Skip to content

Commit

Permalink
Properly cache CStrings for plot names
Browse files Browse the repository at this point in the history
  • Loading branch information
Mervill committed Sep 27, 2024
1 parent f7e6e1e commit 0c1bdc6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
.idea/
.vscode/

# visual studio
.vs/

# C# build artifacts
/bin/
/obj/
/artifacts/

# C# packages
/nupkg/
Expand Down
35 changes: 30 additions & 5 deletions src/cs/samples/HelloWorld/Profiler.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
using System.Runtime.CompilerServices;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using bottlenoselabs.C2CS.Runtime;
using static Tracy.PInvoke;

public static class Profiler
{
// Plot names need to be cached for the lifetime of the program
// seealso Tracy docs section 3.1
private static readonly Dictionary<string, CString> PlotNameCache = new Dictionary<string, CString>();

/// <summary>
/// Begins a new <see cref="ProfilerZone"/> and returns the handle to that zone. Time
/// spent inside a zone is calculated by Tracy and shown in the profiler. A zone is
Expand Down Expand Up @@ -76,7 +82,7 @@ public static ProfilerZone BeginZone(
/// </param>
public static void PlotConfig(string name, PlotType type = PlotType.Number, bool step = false, bool fill = true, uint color = 0)
{
using var namestr = GetCString(name, out var _);
var namestr = GetPlotCString(name);
TracyEmitPlotConfig(namestr, (int)type, step ? 1 : 0, fill ? 1 : 0, color);
}

Expand All @@ -85,7 +91,7 @@ public static void PlotConfig(string name, PlotType type = PlotType.Number, bool
/// </summary>
public static void Plot(string name, double val)
{
using var namestr = GetCString(name, out var _);
var namestr = GetPlotCString(name);
TracyEmitPlot(namestr, val);
}

Expand All @@ -94,7 +100,7 @@ public static void Plot(string name, double val)
/// </summary>
public static void Plot(string name, int val)
{
using var namestr = GetCString(name, out var _);
var namestr = GetPlotCString(name);
TracyEmitPlotInt(namestr, val);
}

Expand All @@ -103,10 +109,20 @@ public static void Plot(string name, int val)
/// </summary>
public static void Plot(string name, float val)
{
using var namestr = GetCString(name, out var _);
var namestr = GetPlotCString(name);
TracyEmitPlotFloat(namestr, val);
}

private static CString GetPlotCString(string name)
{
if(!PlotNameCache.TryGetValue(name, out var plotCString))
{
plotCString = CString.FromString(name);
PlotNameCache.Add(name, plotCString);
}
return plotCString;
}

/// <summary>
/// Emit the top-level frame marker.
/// </summary>
Expand All @@ -118,6 +134,15 @@ public static void EmitFrameMark()
TracyEmitFrameMark(null);
}

/// <summary>
/// Is the app connected to the external profiler?
/// </summary>
/// <returns></returns>
public static bool IsConnected()
{
return Convert.ToBoolean(TracyConnected());
}

/// <summary>
/// Creates a <seealso cref="CString"/> for use by Tracy. Also returns the
/// length of the string for interop convenience.
Expand Down
8 changes: 5 additions & 3 deletions src/cs/samples/HelloWorld/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,26 @@ static void Delay()
int i, end;
double j = 0;

Profiler.PlotConfig("Random", step: true);

using (Profiler.BeginZone("My Custom Event Name"))
{

Random random = new Random();

for (i = 0, end = (int)random.NextInt64() / 100; i < end; ++i)
var randomValue = (int)random.NextInt64() / 100;
for (i = 0, end = randomValue; i < end; ++i)
{
j += Math.Sin(i);
}

Profiler.Plot("Random", randomValue);
}
}

static void ColoredEvent()
{
using (Profiler.BeginZone(color: (uint)ColorType.Aqua))
{

Thread.Sleep(16);
}
}
Expand Down

0 comments on commit 0c1bdc6

Please sign in to comment.