1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//! Helpers for tracing/spans/flamegraphs and such.

#[cfg(not(target_arch = "wasm32"))]
#[cfg(feature = "server")]
mod server;

#[cfg(not(target_arch = "wasm32"))]
#[cfg(feature = "server")]
pub use server::Profiler;

pub mod reexports {
    #[cfg(not(target_arch = "wasm32"))]
    pub use puffin;
}

/// Create a profile scope based on the function name.
///
/// Call this at the very top of an expensive function.
#[macro_export]
macro_rules! profile_function {
    ($($arg: tt)*) => {
        #[cfg(not(target_arch = "wasm32"))]
        $crate::reexports::puffin::profile_function!($($arg)*);
    };
}

/// Create a profile scope based on the function name, if the given condition holds true.
///
/// Call this at the very top of a potentially expensive function.
#[macro_export]
macro_rules! profile_function_if {
    ($($arg: tt)*) => {
        #[cfg(not(target_arch = "wasm32"))]
        $crate::reexports::puffin::profile_function_if!($($arg)*);
    };
}

/// Create a profiling scope with a custom name.
#[macro_export]
macro_rules! profile_scope {
    ($($arg: tt)*) => {
        #[cfg(not(target_arch = "wasm32"))]
        $crate::reexports::puffin::profile_scope!($($arg)*);
    };
}

/// Create a profiling scope with a custom name, if the given condition holds true.
#[macro_export]
macro_rules! profile_scope_if {
    ($($arg: tt)*) => {
        #[cfg(not(target_arch = "wasm32"))]
        $crate::reexports::puffin::profile_scope_if!($($arg)*);
    };
}

/// Create a special profiling scope that indicates that we are waiting
/// for some other thread to finish.
///
/// You should pass in the name of the thing you are waiting for as the first argument.
///
/// # Example
/// ```ignore
/// let normals = {
///     profile_wait!("compute_normals");
///     things.par_iter().for_each(compute_normals)
/// };
/// ```
#[macro_export]
macro_rules! profile_wait {
    () => {
        #[cfg(not(target_arch = "wasm32"))]
        $crate::reexports::puffin::profile_scope!("[WAIT]");
    };
    ($id:expr) => {
        #[cfg(not(target_arch = "wasm32"))]
        $crate::reexports::puffin::profile_scope!(concat!("[WAIT] ", $id));
    };
    ($id:expr, $data:expr) => {
        #[cfg(not(target_arch = "wasm32"))]
        $crate::reexports::puffin::profile_scope!(concat!("[WAIT] ", $id), $data);
    };
}