rust-prometheus icon indicating copy to clipboard operation
rust-prometheus copied to clipboard

Migrate from lazy_static crate to std::sync::LazyLock

Open jaudiger opened this issue 3 years ago • 5 comments

lazy_static!, while a declarative macro, is a macro nonetheless. It can add quite a bit of additional compilation time cost. ~~once_cell::sync::Lazy does the same thing with generics, and can be used more flexibly (i.e. non-static lazily initialised values), and has been proposed to be added to std: https://github.com/rust-lang/rfcs/pull/2788.~~

Famous Rust projects already did the transition, such as:

  • https://github.com/tokio-rs/tokio/pull/3187
  • https://github.com/tokio-rs/tracing/pull/2147

Edit: Since the opening of this issue, a lot of things has happened in Rust, and with Rust 1.80.0, std::sync::LazyLock has been stabilised (see https://doc.rust-lang.org/std/sync/struct.LazyLock.html). There is no need anymore to rely on a crate to lazy initialised a value that is thread safe, everything can now be managed from Rust std 🚀

jaudiger avatar Jul 26 '22 06:07 jaudiger

Thanks for the report. Sounds like a good idea! I'll be happy to review a patch doing this if anybody wants to tackle it.

lucab avatar Jul 26 '22 07:07 lucab

Hi @lucab, thanks, I just opened a PR for it !

jaudiger avatar Jul 30 '22 21:07 jaudiger