From 144a7759118f5449bf64bbda24ed46bf60e93d9a Mon Sep 17 00:00:00 2001 From: Yuanxin Cao Date: Fri, 15 Nov 2024 15:13:39 -0500 Subject: [PATCH 1/3] use once cell instead of lazy static --- optd-cost-model/Cargo.lock | 2 +- optd-cost-model/Cargo.toml | 2 +- optd-cost-model/src/stats/arith_encoder.rs | 23 ++++++++++------------ 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/optd-cost-model/Cargo.lock b/optd-cost-model/Cargo.lock index f749199..7769cfe 100644 --- a/optd-cost-model/Cargo.lock +++ b/optd-cost-model/Cargo.lock @@ -1799,7 +1799,7 @@ dependencies = [ "crossbeam", "datafusion-expr", "itertools 0.13.0", - "lazy_static", + "once_cell", "optd-persistent", "ordered-float 4.5.0", "rand", diff --git a/optd-cost-model/Cargo.toml b/optd-cost-model/Cargo.toml index d37c72a..7b6498c 100644 --- a/optd-cost-model/Cargo.toml +++ b/optd-cost-model/Cargo.toml @@ -13,7 +13,7 @@ datafusion-expr = "32.0.0" ordered-float = "4.0" chrono = "0.4" itertools = "0.13" -lazy_static = "1.5" +once_cell = "1.20" [dev-dependencies] crossbeam = "0.8" diff --git a/optd-cost-model/src/stats/arith_encoder.rs b/optd-cost-model/src/stats/arith_encoder.rs index 4285939..62a0181 100644 --- a/optd-cost-model/src/stats/arith_encoder.rs +++ b/optd-cost-model/src/stats/arith_encoder.rs @@ -7,10 +7,9 @@ //! Non-alpha-numeric characters are relegated to the end of the encoded value, //! rendering them indistinguishable from one another in this context. -use std::collections::HashMap; +use std::{cell::LazyCell, collections::HashMap}; -// TODO: Use lazy cell instead of lazy static. -use lazy_static::lazy_static; +use once_cell::sync::Lazy; // The alphanumerical ordering. const ALPHANUMERIC_ORDER: [char; 95] = [ @@ -23,16 +22,14 @@ const ALPHANUMERIC_ORDER: [char; 95] = [ const PMF: f64 = 1.0 / (ALPHANUMERIC_ORDER.len() as f64); -lazy_static! { - static ref CDF: HashMap = { - let length = ALPHANUMERIC_ORDER.len() + 1; // To account for non-alpha-numeric characters. - let mut cdf = HashMap::with_capacity(length); - for (index, &char) in ALPHANUMERIC_ORDER.iter().enumerate() { - cdf.insert(char, (index as f64) / (length as f64)); - } - cdf - }; -} +static CDF: Lazy> = Lazy::new(|| { + let length = ALPHANUMERIC_ORDER.len() + 1; // To account for non-alpha-numeric characters. + let mut cdf = HashMap::with_capacity(length); + for (index, &char) in ALPHANUMERIC_ORDER.iter().enumerate() { + cdf.insert(char, (index as f64) / (length as f64)); + } + cdf +}); pub fn encode(string: &str) -> f64 { let mut left = 0.0; From 80f46f72fe01caf573305bbaccab4f4fbe440a34 Mon Sep 17 00:00:00 2001 From: Yuanxin Cao Date: Fri, 15 Nov 2024 15:15:01 -0500 Subject: [PATCH 2/3] remove unused imports --- optd-cost-model/src/stats/arith_encoder.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/optd-cost-model/src/stats/arith_encoder.rs b/optd-cost-model/src/stats/arith_encoder.rs index 62a0181..7544dfe 100644 --- a/optd-cost-model/src/stats/arith_encoder.rs +++ b/optd-cost-model/src/stats/arith_encoder.rs @@ -7,7 +7,7 @@ //! Non-alpha-numeric characters are relegated to the end of the encoded value, //! rendering them indistinguishable from one another in this context. -use std::{cell::LazyCell, collections::HashMap}; +use std::collections::HashMap; use once_cell::sync::Lazy; From a607c17b16315ea63267b96d0bf1fd123aebeb2c Mon Sep 17 00:00:00 2001 From: Yuanxin Cao Date: Fri, 15 Nov 2024 16:15:37 -0500 Subject: [PATCH 3/3] use std lazy lock --- optd-cost-model/Cargo.lock | 1 - optd-cost-model/Cargo.toml | 1 - optd-cost-model/src/stats/arith_encoder.rs | 6 ++---- 3 files changed, 2 insertions(+), 6 deletions(-) diff --git a/optd-cost-model/Cargo.lock b/optd-cost-model/Cargo.lock index 7769cfe..a38097d 100644 --- a/optd-cost-model/Cargo.lock +++ b/optd-cost-model/Cargo.lock @@ -1799,7 +1799,6 @@ dependencies = [ "crossbeam", "datafusion-expr", "itertools 0.13.0", - "once_cell", "optd-persistent", "ordered-float 4.5.0", "rand", diff --git a/optd-cost-model/Cargo.toml b/optd-cost-model/Cargo.toml index 7b6498c..1d41af7 100644 --- a/optd-cost-model/Cargo.toml +++ b/optd-cost-model/Cargo.toml @@ -13,7 +13,6 @@ datafusion-expr = "32.0.0" ordered-float = "4.0" chrono = "0.4" itertools = "0.13" -once_cell = "1.20" [dev-dependencies] crossbeam = "0.8" diff --git a/optd-cost-model/src/stats/arith_encoder.rs b/optd-cost-model/src/stats/arith_encoder.rs index 7544dfe..730fcdb 100644 --- a/optd-cost-model/src/stats/arith_encoder.rs +++ b/optd-cost-model/src/stats/arith_encoder.rs @@ -7,9 +7,7 @@ //! Non-alpha-numeric characters are relegated to the end of the encoded value, //! rendering them indistinguishable from one another in this context. -use std::collections::HashMap; - -use once_cell::sync::Lazy; +use std::{collections::HashMap, sync::LazyLock}; // The alphanumerical ordering. const ALPHANUMERIC_ORDER: [char; 95] = [ @@ -22,7 +20,7 @@ const ALPHANUMERIC_ORDER: [char; 95] = [ const PMF: f64 = 1.0 / (ALPHANUMERIC_ORDER.len() as f64); -static CDF: Lazy> = Lazy::new(|| { +static CDF: LazyLock> = LazyLock::new(|| { let length = ALPHANUMERIC_ORDER.len() + 1; // To account for non-alpha-numeric characters. let mut cdf = HashMap::with_capacity(length); for (index, &char) in ALPHANUMERIC_ORDER.iter().enumerate() {