Crates.io | pinocchio-log |
lib.rs | pinocchio-log |
version | 0.1.0 |
source | src |
created_at | 2024-11-10 12:38:02.622319 |
updated_at | 2024-11-10 12:38:02.622319 |
description | Lightweight log utility for Solana programs |
homepage | |
repository | https://github.com/febo/pinocchio |
max_upload_size | |
id | 1442859 |
size | 21,687 |
Lightweight log utility for Solana programs.
Currently, logging messages that require formatting are a bit heavy on the CU consumption. There are two aspects when comes to determining the cost of a log message:
base cost
: this is the cost of the log syscall. It will either be the syscall_base_cost
(currently 100
CU) or a number of CUs equal to the length of the message, whichever value is higher.
formatting cost
: the compute units required to format the message. This is variable and depends on the number and type of the arguments. Formatting is performed using Rust built-in format!
routines, which in turn use format_args!
.
It is known that Rust formatting routines are CPU-intensive for constrained environments. This has been noted on both the solana-program
msg!
documentation and more generally on rust development.
While the cost related to (1) is fixed, in the sense that it does not change with the addition of formatting, it is possible to improve the overall cost of logging a formatted message using a lightweight formatting routine — this is what this crate does.
This crate defines a lightweight Logger
type to format log messages and a companion log!
macro. The logger is a fixed size buffer that can be used to format log messages before sending them to the log output. Any type that implements the Log
trait can be appended to the logger.
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% |
Note: The improvement in CU is accumulative, meaning that if you are logging multiple
u64
values, there will be a 40% improvement per formattedu64
value.
no_std
cratepinocchio
, solana-program
or anchor
)&str
, unsigned and signed integer typeslog!
macro to facilitate log message formattingFrom your project folder:
cargo add pinocchio-log
The Logger
can be used directly:
use pinocchio_log::logger::Logger;
let mut logger = Logger::<100>::default();
logger.append("Hello ");
logger.append("world!");
logger.log();
or via the log!
macro:
use pinocchio_log::log
let amount = 1_000_000_000;
log!("transfer amount: {}", amount);
Since the formatting routine does not perform additional allocations, the Logger
type has a fixed size specified on its creation. When using the log!
macro, it is also possible to specify the size of the logger buffer:
use pinocchio_log::log
let amount = 1_000_000_000;
log!(100, "transfer amount: {}", amount);
Currently the log!
macro does not offer extra formatting options apart from the placeholder "{}
" for argument values.
The code is licensed under the Apache License Version 2.0