From a2a50586aa3b77c18990cd21bba62125776b4c0a Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bennet=20Ble=C3=9Fmann?= Date: Sat, 6 Jul 2024 22:48:04 +0200 Subject: [PATCH] replace static Once and two mut statics with one static OnceLock --- src/machine/mod.rs | 47 ++++++++++++++++++++-------------------------- 1 file changed, 20 insertions(+), 27 deletions(-) diff --git a/src/machine/mod.rs b/src/machine/mod.rs index d4a69c77..33199edd 100644 --- a/src/machine/mod.rs +++ b/src/machine/mod.rs @@ -60,6 +60,7 @@ use std::env; use std::io::Read; use std::path::PathBuf; use std::sync::atomic::AtomicBool; +use std::sync::OnceLock; use self::config::MachineConfig; use self::parsed_results::*; @@ -1261,33 +1262,25 @@ impl Machine { #[inline(always)] fn run_cleaners(&mut self) -> bool { - use std::sync::Once; - - static CLEANER_INIT: Once = Once::new(); - - static mut RCWH: usize = 0; - static mut RCWOH: usize = 0; - - let (r_c_w_h, r_c_wo_h) = unsafe { - CLEANER_INIT.call_once(|| { - let r_c_w_h_atom = atom!("run_cleaners_with_handling"); - let r_c_wo_h_atom = atom!("run_cleaners_without_handling"); - let iso_ext = atom!("iso_ext"); - - RCWH = self - .indices - .get_predicate_code_index(r_c_w_h_atom, 0, iso_ext) - .and_then(|item| item.local()) - .unwrap(); - RCWOH = self - .indices - .get_predicate_code_index(r_c_wo_h_atom, 1, iso_ext) - .and_then(|item| item.local()) - .unwrap(); - }); - - (RCWH, RCWOH) - }; + static CLEANER_INIT: OnceLock<(usize, usize)> = OnceLock::new(); + + let (r_c_w_h, r_c_wo_h) = *CLEANER_INIT.get_or_init(|| { + let r_c_w_h_atom = atom!("run_cleaners_with_handling"); + let r_c_wo_h_atom = atom!("run_cleaners_without_handling"); + let iso_ext = atom!("iso_ext"); + + let r_c_w_h = self + .indices + .get_predicate_code_index(r_c_w_h_atom, 0, iso_ext) + .and_then(|item| item.local()) + .unwrap(); + let r_c_wo_h = self + .indices + .get_predicate_code_index(r_c_wo_h_atom, 1, iso_ext) + .and_then(|item| item.local()) + .unwrap(); + (r_c_w_h, r_c_wo_h) + }); if let Some(&(_, b_cutoff, prev_block)) = self.machine_st.cont_pts.last() { if self.machine_st.b < b_cutoff { -- 2.54.0