mmtk / mmtk-core

Memory Management ToolKit
https://www.mmtk.io
Other
379 stars 69 forks source link

Replace InitializedOnce with OnceLock #1231

Open wks opened 2 weeks ago

wks commented 2 weeks ago

We introduced std::util::rust_util::InitializeOnce in 21 January 2022 as a faster replacement of lazy_static. Rust 1.70.0 (1 June 2023) introduced std::cell::OnceCell and std::sync::OnceLock into the standard library. OnceLock provides the same functionality as InitializeOnce. Since we already bumped MSRV to 1.71.1 (as in package.rust-version in Cargo.toml), we can switch to the OnceLock in the standard library.

@qinsoon did a microbenchmark in https://github.com/mmtk/mmtk-core/pull/1142#discussion_r1626766882, showing that the performance difference between InitializeOnce, std::sync::OnceLock and the third-party once_cell crate is negligible. To be safe, we should do a similar microbenchmark with a use pattern of SFTMap (which is currently the sole user of InitializeOnce) and add it to the mmtk-core/benches/regular_bench directory.

Note that std::sync::OnceLock only provides get() and get_mut(), both of which perform a check of whether it is initialized. We should also see if the checking is a bottleneck by comparing initializeOnce::deref(), std::sync::OnceLock.get and once_cell::sync::OnceCell::get_unchecked.