I understand that all claims on performance ought to be measured and hence backed by benchmarks.
However, I would like to ask theoretically if there are good reasons to believe that there will be performance benefits to defining static
variables instead of allocating them at the start of main()
function (hence, with lifetime 'almost as long as' main()
).
Suppose I have these codes:
#[derive(Debug)]
pub struct Edge {
pub(crate) from: &'static str,
pub(crate) to: &'static str,
pub(crate) symbol: &'static str,
pub(crate) bid_or_ask: BidOrAsk,
pub(crate) rate: AtomicF64,
pub(crate) threshold: AtomicF64,
pub(crate) stop_flag: RwLock<bool>,
}
pub(crate) static ETH_BTC_EDGE: Edge = Edge {
from: "ETH",
to: "BTC",
symbol: "ETHBTC",
bid_or_ask: BidOrAsk::BID,
rate: AtomicF64::new(ZERO_FLOAT),
threshold: AtomicF64::new(ZERO_FLOAT),
stop_flag: RwLock::new(false),
};
pub(crate) static ETH_XRP_EDGE: Edge = Edge {
from: "ETH",
to: "XRP",
symbol: "XRPETH",
bid_or_ask: BidOrAsk::ASK,
rate: AtomicF64::new(ZERO_FLOAT),
threshold: AtomicF64::new(ZERO_FLOAT),
stop_flag: RwLock::new(false),
};
where I know at compile time which variables are going to be created at runtime, hence I declare them as static
.
Compared to:
fn main() {
let edges = make_eth_xrp_btc_edges();
...
}
Assuming that the Edge
struct are going to have identical fields (because well, I want to have say 4 threads updating the rate
field), can we form a strong suspicion that the former codes will be faster than the latter?
I am asking because in C/C++, as statics are stored in the .bss
or .text
segment, their access and memory usage can be much faster (?).