sol_dev_macros/
lib.rs

1/// Original [here](https://github.com/thlorenz/sol-contracts/blob/master/packages/sol-common/rust/src/lib.rs)
2/// A macro for logging compute units used by a specific code block.
3///
4/// This macro wraps a code block, logging the compute units before and after its execution.
5/// It also adds opening and closing log messages for easier tracking in the program output.
6///
7/// # Arguments
8///
9/// * `$msg` - A string literal used as a label for the logged block.
10/// * `$($tt)*` - The code block to be executed and measured.
11///
12/// # Returns
13///
14/// Returns the result of the executed code block.
15///
16/// # Note
17///
18/// This macro consumes an additional 409 compute units per call due to the logging operations.
19///
20/// # Examples
21///
22/// ```rust,ignore
23/// use solana_program;
24/// sol_dev_macros::compute_fn!("My Operation" => {
25///     // Your code here
26///     42
27/// });
28/// ```
29///
30/// # References
31///
32/// * [Logging syscall](https://github.com/anza-xyz/agave/blob/d88050cda335f87e872eddbdf8506bc063f039d3/programs/bpf_loader/src/syscalls/logging.rs#L70)
33/// * [Compute budget](https://github.com/anza-xyz/agave/blob/d88050cda335f87e872eddbdf8506bc063f039d3/program-runtime/src/compute_budget.rs#L150)
34///
35#[macro_export]
36macro_rules! compute_fn {
37    ($msg:expr=> $($tt:tt)*) => {
38        ::solana_program::msg!(concat!($msg, " {"));
39        ::solana_program::log::sol_log_compute_units();
40        let res = { $($tt)* };
41        ::solana_program::log::sol_log_compute_units();
42        ::solana_program::msg!(concat!(" } // ", $msg));
43        res
44    };
45}