Performance Profiling

Performance Profiling

Understanding the performance of your plugins is crucial for maintaining a smooth user experience, especially in a real-time 3D environment like the Foresight Development Kit.

Using Spans for Profiling

The primary way to profile code in the Foresight Development Kit is by instrumenting it with tracing spans. By wrapping a block of code in a span, you record how long it takes to execute.

use tracing::info_span;

pub fn heavy_computation() {
    let _span = info_span!("heavy_computation").entered();
    // Your complex logic here...
}

Or using the #[instrument] attribute:

use tracing::instrument;

#[instrument(skip_all)]
pub fn complex_system() {
    // This entire function is now a span.
}

Built-in Profiler

The Foresight Development Kit includes a built-in profiler that can visualize these spans in real-time. This is often the quickest way to identify which systems or tools are consuming the most frame time.

Opening Developer Tools

You can open the built-in developer tools and profiler using the following keyboard shortcut:

  • macOS: ⌘ + Shift + I
  • Windows/Linux: Ctrl + Shift + I

The developer tools panel provides access to real-time performance metrics, span timelines, and other diagnostic utilities.

Third-Party Tools: Tracy

For more in-depth analysis, the SDK supports Tracy, a powerful graphical profiler. Tracy provides a detailed timeline of all spans across different threads, along with memory allocation tracking and more.

How to use Tracy:

  1. Build with Tracy support: Enable the trace_tracy feature when building the client.
    cargo run --features trace_tracy
  2. Run Tracy: Download and run the Tracy profiler application on your machine.
  3. Connect: The client will automatically attempt to connect to the Tracy application when it starts.

Best Practices

  • Instrument key systems: Add spans to your main Bevy systems and any long-running asynchronous tasks.
  • Keep spans meaningful: Use descriptive names for your spans.
  • Avoid excessive overhead: While tracing is efficient, adding thousands of very short spans in a tight loop can impact performance.