Skip to content

Commit d7bb30d

Browse files
AlexMikhalevclaude
andauthored
refactor: replace deprecated lazy_static with std::sync::LazyLock (#734)
Migrate all lazy_static! macro usage to std::sync::LazyLock (stable since Rust 1.80) across terraphim_rolegraph, terraphim_multi_agent, and terraphim_automata crates. Removes the lazy_static dependency from all three Cargo.toml files. Fixes #136 Co-authored-by: Terraphim AI <noreply@anthropic.com>
1 parent e7eb709 commit d7bb30d

9 files changed

Lines changed: 30 additions & 33 deletions

File tree

Cargo.lock

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/terraphim_automata/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,4 @@ medical = ["daachorse", "zstd", "anyhow"]
5555
[dev-dependencies]
5656
criterion = "0.8"
5757
tempfile = "3.27"
58-
lazy_static = "1.4.0"
5958
tokio = { version = "1", features = ["io-util", "time","macros","rt","rt-multi-thread"] }

crates/terraphim_automata/benches/autocomplete_bench.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ use terraphim_automata::{AutomataPath, load_thesaurus};
2929
use tokio::runtime::Runtime;
3030

3131
#[cfg(feature = "tokio-runtime")]
32-
lazy_static::lazy_static! {
33-
static ref TOKIO_RUNTIME: Runtime = tokio::runtime::Builder::new_current_thread()
32+
static TOKIO_RUNTIME: std::sync::LazyLock<Runtime> = std::sync::LazyLock::new(|| {
33+
tokio::runtime::Builder::new_current_thread()
3434
.enable_all()
3535
.build()
36-
.unwrap();
37-
}
36+
.unwrap()
37+
});
3838

3939
// We can use this `block_on` function to run async code in the benchmarks when tokio is available
4040
#[cfg(feature = "tokio-runtime")]

crates/terraphim_multi_agent/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ rand = "0.9"
3535
# tiktoken-rs = "0.6" # Now handled by rust-genai
3636
tracing = "0.1"
3737
regex = "1.12"
38-
lazy_static = "1.4"
3938

4039
# Terraphim dependencies
4140
terraphim_types = { path = "../terraphim_types" }

crates/terraphim_multi_agent/src/prompt_sanitizer.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
1-
use lazy_static::lazy_static;
21
use regex::Regex;
32
use std::collections::HashSet;
3+
use std::sync::LazyLock;
44
use tracing::warn;
55

66
const MAX_PROMPT_LENGTH: usize = 10_000;
77

8-
lazy_static! {
9-
static ref SUSPICIOUS_PATTERNS: Vec<Regex> = vec![
8+
static SUSPICIOUS_PATTERNS: LazyLock<Vec<Regex>> = LazyLock::new(|| {
9+
vec![
1010
Regex::new(r"(?i)ignore\s+\s*(previous|above|prior)\s+\s*(instructions|prompts?)").unwrap(),
11-
Regex::new(r"(?i)disregard\s+\s*(previous|above|all)\s+\s*(instructions|prompts?)").unwrap(),
11+
Regex::new(r"(?i)disregard\s+\s*(previous|above|all)\s+\s*(instructions|prompts?)")
12+
.unwrap(),
1213
Regex::new(r"(?i)system\s*:\s*you\s+\s*are\s+\s*now").unwrap(),
1314
Regex::new(r"(?i)<\|?im_start\|?>").unwrap(),
1415
Regex::new(r"(?i)<\|?im_end\|?>").unwrap(),
1516
Regex::new(r"(?i)###\s*instruction").unwrap(),
1617
Regex::new(r"(?i)forget\s+\s*(everything|all|previous)").unwrap(),
1718
Regex::new(r"\x00").unwrap(),
1819
Regex::new(r"[\x01-\x08\x0B-\x0C\x0E-\x1F\x7F]").unwrap(),
19-
];
20-
static ref CONTROL_CHAR_PATTERN: Regex =
21-
Regex::new(r"[\x00-\x08\x0B-\x0C\x0E-\x1F\x7F]").unwrap();
20+
]
21+
});
2222

23-
// Unicode special characters that can be used for obfuscation or attacks.
24-
// HashSet for O(1) per-character lookup.
25-
static ref UNICODE_SPECIAL_CHARS: HashSet<char> = [
23+
static CONTROL_CHAR_PATTERN: LazyLock<Regex> =
24+
LazyLock::new(|| Regex::new(r"[\x00-\x08\x0B-\x0C\x0E-\x1F\x7F]").unwrap());
25+
26+
// Unicode special characters that can be used for obfuscation or attacks.
27+
// HashSet for O(1) per-character lookup.
28+
static UNICODE_SPECIAL_CHARS: LazyLock<HashSet<char>> = LazyLock::new(|| {
29+
[
2630
'\u{202E}', // RIGHT-TO-LEFT OVERRIDE
2731
'\u{202D}', // LEFT-TO-RIGHT OVERRIDE
2832
'\u{202C}', // POP DIRECTIONAL FORMATTING
@@ -43,8 +47,11 @@ lazy_static! {
4347
'\u{206D}', // ACTIVATE ARABIC FORM SHAPING
4448
'\u{206E}', // NATIONAL DIGIT SHAPES
4549
'\u{206F}', // NOMINAL DIGIT SHAPES
46-
].iter().copied().collect();
47-
}
50+
]
51+
.iter()
52+
.copied()
53+
.collect()
54+
});
4855

4956
#[derive(Debug, Clone)]
5057
pub struct SanitizedPrompt {

crates/terraphim_multi_agent/tests/concurrent_security_test.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ async fn test_sanitizer_thread_safety() {
129129
}
130130

131131
#[test]
132-
fn test_lazy_static_thread_safety() {
133-
// Verify lazy_static patterns are initialized safely
132+
fn test_lazy_lock_thread_safety() {
133+
// Verify LazyLock patterns are initialized safely
134134
// This tests the regex compilation in SUSPICIOUS_PATTERNS
135135
use std::thread;
136136

@@ -150,7 +150,7 @@ fn test_lazy_static_thread_safety() {
150150

151151
#[tokio::test]
152152
async fn test_unicode_chars_vec_concurrent_access() {
153-
// Test concurrent access to UNICODE_SPECIAL_CHARS lazy_static
153+
// Test concurrent access to UNICODE_SPECIAL_CHARS LazyLock
154154
let mut handles = vec![];
155155

156156
for _ in 0..50 {

crates/terraphim_rolegraph/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ ahash = { version = "0.8.3", features = ["serde"] }
1919
aho-corasick = "1.0.2"
2020
anyhow = { version = "1.0", optional = true }
2121
itertools = "0.14.0"
22-
lazy_static = "1.4.0"
2322
log = "0.4.29"
2423
memoize = "0.5.1"
2524
regex = "1.12.3"

crates/terraphim_rolegraph/benches/throughput.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ use terraphim_rolegraph::input::TEST_CORPUS;
1616
use terraphim_rolegraph::split_paragraphs;
1717
use terraphim_types::{Document, DocumentType, Thesaurus};
1818

19-
lazy_static::lazy_static! {
20-
static ref TOKIO_RUNTIME: Runtime = Runtime::new().unwrap();
21-
}
19+
static TOKIO_RUNTIME: std::sync::LazyLock<Runtime> =
20+
std::sync::LazyLock::new(|| Runtime::new().unwrap());
2221

2322
// We can use this `block_on` function to run async code in the benchmarks
2423
// without having to use `async fn`, which is not supported by the `criterion` library.

crates/terraphim_rolegraph/src/lib.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,11 +1249,8 @@ impl From<RoleGraph> for RoleGraphSync {
12491249
}
12501250
}
12511251

1252-
#[macro_use]
1253-
extern crate lazy_static;
1254-
lazy_static! {
1255-
static ref RE: Regex = Regex::new(r"[?!|]\s+").unwrap();
1256-
}
1252+
static RE: std::sync::LazyLock<Regex> =
1253+
std::sync::LazyLock::new(|| Regex::new(r"[?!|]\s+").unwrap());
12571254

12581255
pub fn split_paragraphs(paragraphs: &str) -> Vec<&str> {
12591256
let sentences = UnicodeSegmentation::split_sentence_bounds(paragraphs);

0 commit comments

Comments
 (0)