Replace lazy_static with OnceLock

This commit replaces the `lazy_static` crate with `std::sync::OnceLock`.
This removes an external dependency and utilizes a standard library
feature for lazy initialization.

Additionally, a benchmark regression test has been added to
`src/ast/tester.rs`.
This commit is contained in:
Michael Schimmel
2026-02-18 16:00:21 +01:00
parent 76586c0903
commit b6d1d41c8b
4 changed files with 48 additions and 21 deletions
+6 -8
View File
@@ -2,7 +2,7 @@ use std::collections::{HashMap, BTreeMap};
use std::rc::Rc;
use std::cell::RefCell;
use std::fmt;
use lazy_static::lazy_static;
use std::sync::OnceLock;
use std::sync::Mutex; // Still needed for global keyword registry
use std::any::Any;
@@ -25,18 +25,16 @@ pub type Identity = Rc<NodeIdentity>;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Keyword(pub u32);
lazy_static! {
static ref KEYWORD_REGISTRY: Mutex<HashMap<String, u32>> = Mutex::new(HashMap::new());
static ref KEYWORD_REVERSE: Mutex<Vec<String>> = Mutex::new(Vec::new());
}
static KEYWORD_REGISTRY: OnceLock<Mutex<HashMap<String, u32>>> = OnceLock::new();
static KEYWORD_REVERSE: OnceLock<Mutex<Vec<String>>> = OnceLock::new();
impl Keyword {
pub fn intern(name: &str) -> Self {
let mut reg = KEYWORD_REGISTRY.lock().unwrap();
let mut reg = KEYWORD_REGISTRY.get_or_init(|| Mutex::new(HashMap::new())).lock().unwrap();
if let Some(&id) = reg.get(name) {
Keyword(id)
} else {
let mut rev = KEYWORD_REVERSE.lock().unwrap();
let mut rev = KEYWORD_REVERSE.get_or_init(|| Mutex::new(Vec::new())).lock().unwrap();
let id = rev.len() as u32;
reg.insert(name.to_string(), id);
rev.push(name.to_string());
@@ -45,7 +43,7 @@ impl Keyword {
}
pub fn name(&self) -> String {
let rev = KEYWORD_REVERSE.lock().unwrap();
let rev = KEYWORD_REVERSE.get_or_init(|| Mutex::new(Vec::new())).lock().unwrap();
rev[self.0 as usize].clone()
}
}