(no expressed opinion on the proposed feature)
(generally I prefer OnceCell
to lazy_static
nowadays)
The general solution to the problem for libs where using a global matters for performance is to either
- have the caller initialize a
Ctx
and pass references to that, or - have the caller create a zero-sized
InitToken
that proves they've called ainit() -> InitToken
, which is truly zero-cost to pass as an argument, or - if it really needs to be global data, have an
unsafe
requirement to call some initialization function
It's interesting that you're already using an InitToken
to prove that the global cell has been initialized and won't be written again. It should be possible to use unsafe
to convince the optimizer to do a non-atomic read (and optimize as such). It's clearly a bug that this currently doesn't happen (and likely due to underutilization of noalias
, which I believe is still enabled (finally)).
I'd even argue that a Relaxed
ordering allows the desired optimization. I don't think life-before-main is required to get the result you're looking for in the linked urlo thread.