Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use once cell instead of lazy static #35

Merged
merged 3 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion optd-cost-model/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion optd-cost-model/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ datafusion-expr = "32.0.0"
ordered-float = "4.0"
chrono = "0.4"
itertools = "0.13"
lazy_static = "1.5"

[dev-dependencies]
crossbeam = "0.8"
Expand Down
23 changes: 9 additions & 14 deletions optd-cost-model/src/stats/arith_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +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;

// TODO: Use lazy cell instead of lazy static.
use lazy_static::lazy_static;
use std::{collections::HashMap, sync::LazyLock};

// The alphanumerical ordering.
const ALPHANUMERIC_ORDER: [char; 95] = [
Expand All @@ -23,16 +20,14 @@ const ALPHANUMERIC_ORDER: [char; 95] = [

const PMF: f64 = 1.0 / (ALPHANUMERIC_ORDER.len() as f64);

lazy_static! {
static ref CDF: HashMap<char, f64> = {
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: LazyLock<HashMap<char, f64>> = 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() {
cdf.insert(char, (index as f64) / (length as f64));
}
cdf
});

pub fn encode(string: &str) -> f64 {
let mut left = 0.0;
Expand Down