febo / pinocchio

Create Solana programs with no dependencies attached
Apache License 2.0
89 stars 18 forks source link

Lightweight log macro #32

Closed febo closed 2 weeks ago

febo commented 2 weeks ago

Problem

Currently, logging messages that require formatting are a bit heavy on the CU consumption. For example, a simple msg!("lamports={}", lamports) log can take ~627 CUs.

Solution

Create a lightweight log! macro that improves CU consumption.

Below is a sample of the improvements observed when formatting log messages, measured in terms of compute units (CU): Ouput message log! msg! Improvement (%)
"Hello world!" 103 103 -
"lamports={}" + u64 374 627 (+253) 40%
"{}" + [&str; 2] 384 1648 (+1264) 76%
"{}" + [u64; 2] 601 1060 (+459) 44%
"lamports={}" + i64 389 660 (+271) 41%
"{}" + [u8; 32] (pubkey bytes) 3147 8401 (+5254) 62%