typst_macros

Attribute Macro time

source
#[time]
Expand description

Times function invocations.

When tracing is enabled in the typst-cli, this macro will record the invocations of the function and store them in a global map. The map can be accessed through the typst_trace::RECORDER static.

You can also specify the span of the function invocation:

  • #[time(span = ..)] to record the span, which will be used for the EventKey.

By default, all tracing is omitted using the wasm32 target flag. This is done to avoid bloating the web app, which doesn’t need tracing.

#[time]
fn fibonacci(n: u64) -> u64 {
    if n <= 1 {
        1
    } else {
        fibonacci(n - 1) + fibonacci(n - 2)
    }
}

#[time(span = span)]
fn fibonacci_spanned(n: u64, span: Span) -> u64 {
    if n <= 1 {
        1
    } else {
        fibonacci(n - 1) + fibonacci(n - 2)
    }
}