Skip to content

Commit

Permalink
oncecell in favor of lazy_static (#3319)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed May 6, 2022
1 parent 4963e53 commit 1adce2f
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 45 deletions.
1 change: 0 additions & 1 deletion polars/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,6 @@ polars-time = { version = "0.21.1", path = "./polars-time", default-features = f
[dev-dependencies]
ahash = "0.7"
criterion = "0.3"
lazy_static = "1.4"
rand = "0.8"

# see: https://bheisler.github.io/criterion.rs/book/faq.html
Expand Down
2 changes: 1 addition & 1 deletion polars/polars-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,9 @@ hashbrown = { version = "0.12", features = ["rayon"] }
hex = { version = "0.4", optional = true }
indexmap = { version = "1", features = ["std"] }
jsonpath_lib = { version = "0.3.0", optional = true, git = "https://github.com/ritchie46/jsonpath", branch = "improve_compiled" }
lazy_static = "1.4"
ndarray = { version = "0.15", optional = true, default_features = false }
num = "^0.4"
once_cell = "1"
polars-arrow = { version = "0.21.1", path = "../polars-arrow", features = ["compute"] }
polars-utils = { version = "0.21.1", path = "../polars-utils" }
rand = { version = "0.8", optional = true, features = ["small_rng", "std"] }
Expand Down
2 changes: 1 addition & 1 deletion polars/polars-core/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ pub use arrow;
pub use chrono;

#[cfg(feature = "private")]
pub use lazy_static;
pub use once_cell;
#[cfg(feature = "private")]
pub use rayon;
42 changes: 20 additions & 22 deletions polars/polars-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,43 @@ pub mod testing;
#[cfg(test)]
mod tests;
pub(crate) mod vector_hasher;
use once_cell::sync::Lazy;

#[cfg(any(feature = "dtype-categorical", feature = "object"))]
use std::time::{SystemTime, UNIX_EPOCH};

#[cfg(feature = "dtype-categorical")]
use ahash::AHashMap;
use lazy_static::lazy_static;
use rayon::{ThreadPool, ThreadPoolBuilder};
#[cfg(feature = "dtype-categorical")]
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Mutex;
#[cfg(feature = "dtype-categorical")]
use std::sync::{Mutex, MutexGuard};
use std::sync::MutexGuard;

#[cfg(feature = "object")]
lazy_static! {
pub(crate) static ref PROCESS_ID: u128 = SystemTime::now()
pub(crate) static PROCESS_ID: Lazy<u128> = Lazy::new(|| {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_nanos();
}
.as_nanos()
});

// this is re-exported in utils for polars child crates
lazy_static! {
pub static ref POOL: ThreadPool = ThreadPoolBuilder::new()
pub static POOL: Lazy<ThreadPool> = Lazy::new(|| {
ThreadPoolBuilder::new()
.num_threads(
std::env::var("POLARS_MAX_THREADS")
.map(|s| s.parse::<usize>().expect("integer"))
.unwrap_or_else(|_| std::thread::available_parallelism()
.unwrap_or(std::num::NonZeroUsize::new(1).unwrap())
.get())
.unwrap_or_else(|_| {
std::thread::available_parallelism()
.unwrap_or(std::num::NonZeroUsize::new(1).unwrap())
.get()
}),
)
.build()
.expect("could not spawn threads");
}
.expect("could not spawn threads")
});

#[cfg(feature = "dtype-categorical")]
struct SCacheInner {
Expand Down Expand Up @@ -104,17 +107,12 @@ impl Default for StringCache {

#[cfg(feature = "dtype-categorical")]
pub(crate) static USE_STRING_CACHE: AtomicBool = AtomicBool::new(false);
#[cfg(feature = "dtype-categorical")]
lazy_static! {
pub(crate) static ref STRING_CACHE: StringCache = Default::default();
}

#[cfg(test)]
#[cfg(feature = "dtype-categorical")]
lazy_static! {
// utility for the tests to ensure a single thread can execute
pub(crate) static ref SINGLE_LOCK: Mutex<()> = Mutex::new(());
}
pub(crate) static STRING_CACHE: Lazy<StringCache> = Lazy::new(Default::default);

// utility for the tests to ensure a single thread can execute
pub static SINGLE_LOCK: Lazy<Mutex<()>> = Lazy::new(|| Mutex::new(()));

/// Use a global string cache for the Categorical Types.
///
Expand Down
2 changes: 1 addition & 1 deletion polars/polars-io/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ arrow = { package = "arrow2", git = "https://github.com/jorgecarleitao/arrow2",
csv-core = { version = "0.1.10", optional = true }
dirs = "4.0"
flate2 = { version = "1", optional = true, default-features = false }
lazy_static = "1.4"
lexical = { version = "6", optional = true, default-features = false, features = ["std", "parse-floats", "parse-integers"] }
memchr = "2.4"
memmap = { package = "memmap2", version = "0.5.2", optional = true }
num = "^0.4"
once_cell = "1"
polars-arrow = { version = "0.21.1", path = "../polars-arrow" }
polars-core = { version = "0.21.1", path = "../polars-core", features = ["private"], default-features = false }
polars-time = { version = "0.21.1", path = "../polars-time", features = ["private"], default-features = false }
Expand Down
20 changes: 11 additions & 9 deletions polars/polars-io/src/csv_core/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::csv_core::parser::{
};
use crate::mmap::{MmapBytesReader, ReaderBytes};
use crate::prelude::NullValues;
use lazy_static::lazy_static;
use once_cell::sync::Lazy;
use polars_core::datatypes::PlHashSet;
use polars_core::prelude::*;
use polars_time::chunkedarray::utf8::infer as date_infer;
Expand Down Expand Up @@ -75,16 +75,18 @@ pub fn get_reader_bytes<R: Read + MmapBytesReader>(reader: &mut R) -> Result<Rea
}
}

lazy_static! {
static ref FLOAT_RE: Regex =
Regex::new(r"^(\s*-?((\d*\.\d+)[eE]?[-\+]?\d*)|[-+]?inf|[-+]?NaN|\d+[eE][-+]\d+)$")
.unwrap();
static ref INTEGER_RE: Regex = Regex::new(r"^\s*-?(\d+)$").unwrap();
static ref BOOLEAN_RE: Regex = RegexBuilder::new(r"^\s*(true)$|^(false)$")
static FLOAT_RE: Lazy<Regex> = Lazy::new(|| {
Regex::new(r"^(\s*-?((\d*\.\d+)[eE]?[-\+]?\d*)|[-+]?inf|[-+]?NaN|\d+[eE][-+]\d+)$").unwrap()
});

static INTEGER_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"^\s*-?(\d+)$").unwrap());

static BOOLEAN_RE: Lazy<Regex> = Lazy::new(|| {
RegexBuilder::new(r"^\s*(true)$|^(false)$")
.case_insensitive(true)
.build()
.unwrap();
}
.unwrap()
});

/// Infer the data type of a record
fn infer_field_schema(string: &str, parse_dates: bool) -> DataType {
Expand Down
9 changes: 1 addition & 8 deletions polars/polars-lazy/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use polars_core::chunked_array::builder::get_list_builder;
use polars_core::df;
#[cfg(feature = "temporal")]
use polars_core::export::chrono::{NaiveDate, NaiveDateTime, NaiveTime};
use polars_core::export::lazy_static::lazy_static;
pub(crate) use polars_core::SINGLE_LOCK;
use std::iter::FromIterator;

static GLOB_PARQUET: &str = "../../examples/datasets/*.parquet";
Expand All @@ -49,13 +49,6 @@ static FOODS_CSV: &str = "../../examples/datasets/foods1.csv";
static FOODS_IPC: &str = "../../examples/datasets/foods1.ipc";
static FOODS_PARQUET: &str = "../../examples/datasets/foods1.parquet";

lazy_static! {
// needed prevent race conditions during test execution
// for example with env vars set for tests
// or global string cache resets.
pub(crate) static ref SINGLE_LOCK: Mutex<()> = Mutex::new(());
}

fn scan_foods_csv() -> LazyFrame {
LazyCsvReader::new(FOODS_CSV.to_string()).finish().unwrap()
}
Expand Down
4 changes: 2 additions & 2 deletions py-polars/Cargo.lock

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

0 comments on commit 1adce2f

Please sign in to comment.