Skip to main content

ProfilerMarkers

Profiler markers in one line, with no fields or names to keep up by hand.

Quick start​

Before — Unity APIAfter — FastTools
private static readonly
ProfilerMarker _marker =
new("FlockSimulation.Step");

public void Step()
{
using var _ = _marker.Auto();
Integrate();
}
public void Step()
{
using var _ = this.Marker();
Integrate();
}

Marker()​

The marker name includes the type, the member name and the call’s line number:

Where this.Marker() is calledMarker name
void Step()FlockSimulation.Step (line)
FlockSimulation()FlockSimulation.Ctor (line)
float Speed { get; }FlockSimulation.Speed (line)
Agent this[int i] { get; }FlockSimulation.Indexer (line)
event Action ChangedFlockSimulation.Changed (line)
Explicit implementation void ISimulation.Step()FlockSimulation.Step (line)

The line number changes when the call moves in the source.

Nested and generic types
Call locationMarker name
Method Move() in nested FlockSimulation.AgentAgent.Move (line)
Step() in class FlockSimulation<Int32>FlockSimulation<Int32>.Step (line)
Step() in struct FlockSimulation<T>FlockSimulation<T>.Step (line) for any T
Generic method Step<T>()FlockSimulation.Step (line) for any T

WithName()​

WithName("Steering") replaces the member name, keeping the type and line number.

public void Step()
{
using var _ = this.Marker();

using (this.Marker().WithName("Steering"))
{
foreach (var agent in _agents)
{
using (this.Marker().WithName("Steering.Agent"))
ComputeSteering(agent);
}
}

using (this.Marker().WithName("Integrate"))
Integrate();
}

The name must be a constant: a string literal, a const, nameof or a string built from them. A name known only at run time leaves the member name.

In the Profiler​

Markers are created once. After initialization, repeated measurements allocate no memory. In a player build without Development Build, this.Marker() measures nothing: ENABLE_PROFILER is not defined.

HierarchyFrame 1284

Package markers​

The package marks its own hot paths too, such as the first access to EnumValues, which builds its entries, and SerializableType resolution. Lookups in an already built EnumValues open no marker. Add the symbol ASPID_FASTTOOLS_UNITY_PROFILER_DISABLED to Scripting Define Symbols to compile these markers out. Markers from your own this.Marker() calls stay.

Limitations​

  • Call this.Marker() inside its own type. Calls on another type’s instance, in a static class or in a private/protected nested type are unreliable: the measurement may be missing or use another call’s marker.
  • Marker() is declared without a namespace, so it needs no using and code completion offers it on any expression, even when the call gets no marker.
  • Use using. A standalone this.Marker(); starts a measurement without ending it.
  • End the using before yield return or await. A coroutine or an async method resumes in a later frame or on another thread, so a measurement left open across them leaves the Profiler samples unbalanced.
warning

Calls within one type that share a line number share a marker, even across different partial files. Give each call site a distinct line number, or measurements will be combined under the first call’s name.

Package sample​

The markers from WithName() run in the ProfilerMarkers scene.

A flock of agents in the ProfilerMarkers sceneA flock of agents in the ProfilerMarkers scene