Skip to content

Commit e38255d

Browse files
nameexhaustionjsjasonseba
authored andcommitted
refactor(rust): Remove once_cell in favor of std equivalents (pola-rs#21639)
1 parent 3ecaeeb commit e38255d

File tree

36 files changed

+84
-107
lines changed

36 files changed

+84
-107
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ ndarray = { version = "0.16", default-features = false }
5555
num-traits = "0.2"
5656
numpy = "0.23"
5757
object_store = { version = "0.11", default-features = false }
58-
once_cell = "1"
5958
parking_lot = "0.12"
6059
percent-encoding = "2.3"
6160
pin-project-lite = "0.2"

crates/polars-core/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ indexmap = { workspace = true }
2929
itoa = { workspace = true }
3030
ndarray = { workspace = true, optional = true }
3131
num-traits = { workspace = true }
32-
once_cell = { workspace = true }
3332
rand = { workspace = true, optional = true, features = ["small_rng", "std"] }
3433
rand_distr = { workspace = true, optional = true }
3534
rayon = { workspace = true }

crates/polars-core/src/chunked_array/logical/categorical/string_cache.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use std::hash::{Hash, Hasher};
22
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
3-
use std::sync::{Mutex, RwLock, RwLockReadGuard, RwLockWriteGuard};
3+
use std::sync::{LazyLock, Mutex, RwLock, RwLockReadGuard, RwLockWriteGuard};
44

55
use hashbrown::hash_table::Entry;
66
use hashbrown::HashTable;
7-
use once_cell::sync::Lazy;
87
use polars_utils::aliases::PlRandomState;
98
use polars_utils::pl_str::PlSmallStr;
109

@@ -250,4 +249,4 @@ impl StringCache {
250249
}
251250
}
252251

253-
pub(crate) static STRING_CACHE: Lazy<StringCache> = Lazy::new(Default::default);
252+
pub(crate) static STRING_CACHE: LazyLock<StringCache> = LazyLock::new(Default::default);

crates/polars-core/src/chunked_array/object/registry.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
use std::any::Any;
66
use std::fmt::{Debug, Formatter};
77
use std::ops::Deref;
8-
use std::sync::{Arc, RwLock};
8+
use std::sync::{Arc, LazyLock, RwLock};
99

1010
use arrow::array::ArrayRef;
1111
use arrow::datatypes::ArrowDataType;
12-
use once_cell::sync::Lazy;
1312
use polars_utils::pl_str::PlSmallStr;
1413

1514
use crate::chunked_array::object::builder::ObjectChunkedBuilder;
@@ -36,7 +35,8 @@ impl Debug for ObjectRegistry {
3635
}
3736
}
3837

39-
static GLOBAL_OBJECT_REGISTRY: Lazy<RwLock<Option<ObjectRegistry>>> = Lazy::new(Default::default);
38+
static GLOBAL_OBJECT_REGISTRY: LazyLock<RwLock<Option<ObjectRegistry>>> =
39+
LazyLock::new(Default::default);
4040

4141
/// This trait can be registered, after which that global registration
4242
/// can be used to materialize object types

crates/polars-core/src/chunked_array/temporal/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ mod datetime;
88
mod duration;
99
#[cfg(feature = "dtype-time")]
1010
mod time;
11+
#[cfg(feature = "timezones")]
12+
use std::sync::LazyLock;
13+
1114
#[cfg(feature = "dtype-date")]
1215
use chrono::NaiveDate;
1316
use chrono::NaiveDateTime;
@@ -16,8 +19,6 @@ use chrono::NaiveTime;
1619
#[cfg(feature = "timezones")]
1720
use chrono_tz::Tz;
1821
#[cfg(feature = "timezones")]
19-
use once_cell::sync::Lazy;
20-
#[cfg(feature = "timezones")]
2122
use polars_utils::pl_str::PlSmallStr;
2223
#[cfg(all(feature = "regex", feature = "timezones"))]
2324
use regex::Regex;
@@ -38,7 +39,8 @@ static FIXED_OFFSET_PATTERN: &str = r#"(?x)
3839
$
3940
"#;
4041
#[cfg(feature = "timezones")]
41-
static FIXED_OFFSET_RE: Lazy<Regex> = Lazy::new(|| Regex::new(FIXED_OFFSET_PATTERN).unwrap());
42+
static FIXED_OFFSET_RE: LazyLock<Regex> =
43+
LazyLock::new(|| Regex::new(FIXED_OFFSET_PATTERN).unwrap());
4244

4345
#[cfg(feature = "timezones")]
4446
pub fn validate_time_zone(tz: &str) -> PolarsResult<()> {

crates/polars-core/src/lib.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,17 @@ pub mod testing;
2727
#[cfg(test)]
2828
mod tests;
2929

30-
use std::sync::Mutex;
30+
use std::sync::{LazyLock, Mutex};
3131
use std::time::{SystemTime, UNIX_EPOCH};
3232

3333
pub use datatypes::SchemaExtPl;
3434
pub use hashing::IdBuildHasher;
35-
use once_cell::sync::Lazy;
3635
use rayon::{ThreadPool, ThreadPoolBuilder};
3736

3837
#[cfg(feature = "dtype-categorical")]
3938
pub use crate::chunked_array::logical::categorical::string_cache::*;
4039

41-
pub static PROCESS_ID: Lazy<u128> = Lazy::new(|| {
40+
pub static PROCESS_ID: LazyLock<u128> = LazyLock::new(|| {
4241
SystemTime::now()
4342
.duration_since(UNIX_EPOCH)
4443
.unwrap()
@@ -47,7 +46,7 @@ pub static PROCESS_ID: Lazy<u128> = Lazy::new(|| {
4746

4847
// this is re-exported in utils for polars child crates
4948
#[cfg(not(target_family = "wasm"))] // only use this on non wasm targets
50-
pub static POOL: Lazy<ThreadPool> = Lazy::new(|| {
49+
pub static POOL: LazyLock<ThreadPool> = LazyLock::new(|| {
5150
let thread_name = std::env::var("POLARS_THREAD_NAME").unwrap_or_else(|_| "polars".to_string());
5251
ThreadPoolBuilder::new()
5352
.num_threads(
@@ -65,7 +64,7 @@ pub static POOL: Lazy<ThreadPool> = Lazy::new(|| {
6564
});
6665

6766
#[cfg(all(target_os = "emscripten", target_family = "wasm"))] // Use 1 rayon thread on emscripten
68-
pub static POOL: Lazy<ThreadPool> = Lazy::new(|| {
67+
pub static POOL: LazyLock<ThreadPool> = LazyLock::new(|| {
6968
ThreadPoolBuilder::new()
7069
.num_threads(1)
7170
.use_current_thread()
@@ -74,10 +73,10 @@ pub static POOL: Lazy<ThreadPool> = Lazy::new(|| {
7473
});
7574

7675
#[cfg(all(not(target_os = "emscripten"), target_family = "wasm"))] // use this on other wasm targets
77-
pub static POOL: Lazy<polars_utils::wasm::Pool> = Lazy::new(|| polars_utils::wasm::Pool);
76+
pub static POOL: LazyLock<polars_utils::wasm::Pool> = LazyLock::new(|| polars_utils::wasm::Pool);
7877

7978
// utility for the tests to ensure a single thread can execute
80-
pub static SINGLE_LOCK: Lazy<Mutex<()>> = Lazy::new(|| Mutex::new(()));
79+
pub static SINGLE_LOCK: LazyLock<Mutex<()>> = LazyLock::new(|| Mutex::new(()));
8180

8281
/// Default length for a `.head()` call
8382
pub(crate) const HEAD_DEFAULT_LENGTH: usize = 10;

crates/polars-core/src/random.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
use std::sync::Mutex;
1+
use std::sync::{LazyLock, Mutex};
22

3-
use once_cell::sync::Lazy;
43
use rand::prelude::*;
54

6-
static POLARS_GLOBAL_RNG_STATE: Lazy<Mutex<SmallRng>> =
7-
Lazy::new(|| Mutex::new(SmallRng::from_entropy()));
5+
static POLARS_GLOBAL_RNG_STATE: LazyLock<Mutex<SmallRng>> =
6+
LazyLock::new(|| Mutex::new(SmallRng::from_entropy()));
87

98
pub(crate) fn get_global_random_u64() -> u64 {
109
POLARS_GLOBAL_RNG_STATE.lock().unwrap().next_u64()

crates/polars-expr/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ arrow = { workspace = true }
1414
bitflags = { workspace = true }
1515
hashbrown = { workspace = true }
1616
num-traits = { workspace = true }
17-
once_cell = { workspace = true }
1817
polars-compute = { workspace = true }
1918
polars-core = { workspace = true, features = ["lazy", "zip_with", "random"] }
2019
polars-io = { workspace = true, features = ["lazy"] }

crates/polars-expr/src/state/execution_state.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use std::borrow::Cow;
22
use std::sync::atomic::{AtomicBool, AtomicI64, AtomicU8, Ordering};
3-
use std::sync::{Mutex, RwLock};
3+
use std::sync::{Mutex, OnceLock, RwLock};
44
use std::time::Duration;
55

66
use bitflags::bitflags;
7-
use once_cell::sync::OnceCell;
87
use polars_core::config::verbose;
98
use polars_core::prelude::*;
109
use polars_ops::prelude::ChunkJoinOptIds;
@@ -101,7 +100,7 @@ impl From<u8> for StateFlags {
101100
}
102101
}
103102

104-
type CachedValue = Arc<(AtomicI64, OnceCell<DataFrame>)>;
103+
type CachedValue = Arc<(AtomicI64, OnceLock<DataFrame>)>;
105104

106105
/// State/ cache that is maintained during the Execution of the physical plan.
107106
pub struct ExecutionState {
@@ -229,7 +228,7 @@ impl ExecutionState {
229228
guard
230229
.entry(key)
231230
.or_insert_with(|| {
232-
Arc::new((AtomicI64::new(cache_hits as i64), OnceCell::new()))
231+
Arc::new((AtomicI64::new(cache_hits as i64), OnceLock::new()))
233232
})
234233
.clone()
235234
},

crates/polars-io/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ memchr = { workspace = true }
3535
memmap = { workspace = true }
3636
num-traits = { workspace = true }
3737
object_store = { workspace = true, optional = true }
38-
once_cell = { workspace = true }
3938
percent-encoding = { workspace = true }
4039
pyo3 = { workspace = true, optional = true }
4140
rayon = { workspace = true }

crates/polars-io/src/cloud/object_store_setup.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
use std::sync::Arc;
1+
use std::sync::{Arc, LazyLock};
22

33
use object_store::local::LocalFileSystem;
44
use object_store::ObjectStore;
5-
use once_cell::sync::Lazy;
65
use polars_core::config::{self, verbose_print_sensitive};
76
use polars_error::{polars_bail, to_compute_err, PolarsError, PolarsResult};
87
use polars_utils::aliases::PlHashMap;
@@ -18,8 +17,8 @@ use crate::cloud::CloudConfig;
1817
/// get rate limited when querying the DNS (can take up to 5s).
1918
/// Other reasons are connection pools that must be shared between as much as possible.
2019
#[allow(clippy::type_complexity)]
21-
static OBJECT_STORE_CACHE: Lazy<RwLock<PlHashMap<Vec<u8>, PolarsObjectStore>>> =
22-
Lazy::new(Default::default);
20+
static OBJECT_STORE_CACHE: LazyLock<RwLock<PlHashMap<Vec<u8>, PolarsObjectStore>>> =
21+
LazyLock::new(Default::default);
2322

2423
#[allow(dead_code)]
2524
fn err_missing_feature(feature: &str, scheme: &str) -> PolarsResult<Arc<dyn ObjectStore>> {

crates/polars-io/src/cloud/options.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::io::Read;
33
#[cfg(feature = "aws")]
44
use std::path::Path;
55
use std::str::FromStr;
6+
use std::sync::LazyLock;
67

78
#[cfg(feature = "aws")]
89
use object_store::aws::AmazonS3Builder;
@@ -20,7 +21,6 @@ pub use object_store::gcp::GoogleConfigKey;
2021
use object_store::ClientOptions;
2122
#[cfg(any(feature = "aws", feature = "gcp", feature = "azure"))]
2223
use object_store::{BackoffConfig, RetryConfig};
23-
use once_cell::sync::Lazy;
2424
use polars_error::*;
2525
#[cfg(feature = "aws")]
2626
use polars_utils::cache::FastFixedCache;
@@ -41,11 +41,11 @@ use crate::file_cache::get_env_file_cache_ttl;
4141
use crate::pl_async::with_concurrency_budget;
4242

4343
#[cfg(feature = "aws")]
44-
static BUCKET_REGION: Lazy<
44+
static BUCKET_REGION: LazyLock<
4545
std::sync::Mutex<
4646
FastFixedCache<polars_utils::pl_str::PlSmallStr, polars_utils::pl_str::PlSmallStr>,
4747
>,
48-
> = Lazy::new(|| std::sync::Mutex::new(FastFixedCache::new(32)));
48+
> = LazyLock::new(|| std::sync::Mutex::new(FastFixedCache::new(32)));
4949

5050
/// The type of the config keys must satisfy the following requirements:
5151
/// 1. must be easily collected into a HashMap, the type required by the object_crate API.
@@ -91,7 +91,7 @@ impl Default for CloudOptions {
9191

9292
impl CloudOptions {
9393
pub fn default_static_ref() -> &'static Self {
94-
static DEFAULT: Lazy<CloudOptions> = Lazy::new(|| CloudOptions {
94+
static DEFAULT: LazyLock<CloudOptions> = LazyLock::new(|| CloudOptions {
9595
max_retries: 2,
9696
#[cfg(feature = "file_cache")]
9797
file_cache_ttl: get_env_file_cache_ttl(),

crates/polars-io/src/file_cache/cache.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use std::path::Path;
22
use std::sync::atomic::AtomicU64;
3-
use std::sync::{Arc, RwLock};
3+
use std::sync::{Arc, LazyLock, RwLock};
44

5-
use once_cell::sync::Lazy;
65
use polars_core::config;
76
use polars_error::PolarsResult;
87
use polars_utils::aliases::PlHashMap;
@@ -13,7 +12,7 @@ use super::file_fetcher::FileFetcher;
1312
use super::utils::FILE_CACHE_PREFIX;
1413
use crate::path_utils::{ensure_directory_init, is_cloud_url};
1514

16-
pub static FILE_CACHE: Lazy<FileCache> = Lazy::new(|| {
15+
pub static FILE_CACHE: LazyLock<FileCache> = LazyLock::new(|| {
1716
let prefix = FILE_CACHE_PREFIX.as_ref();
1817
let prefix = Arc::<Path>::from(prefix);
1918

crates/polars-io/src/file_cache/cache_lock.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
use std::sync::atomic::AtomicBool;
2-
use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard};
2+
use std::sync::{Arc, LazyLock, RwLock, RwLockReadGuard, RwLockWriteGuard};
33
use std::time::Duration;
44

55
use fs4::fs_std::FileExt;
6-
use once_cell::sync::Lazy;
76

87
use super::utils::FILE_CACHE_PREFIX;
98
use crate::pl_async;
109

11-
pub(super) static GLOBAL_FILE_CACHE_LOCK: Lazy<GlobalLock> = Lazy::new(|| {
10+
pub(super) static GLOBAL_FILE_CACHE_LOCK: LazyLock<GlobalLock> = LazyLock::new(|| {
1211
let path = FILE_CACHE_PREFIX.join(".process-lock");
1312

1413
let file = std::fs::OpenOptions::new()

crates/polars-io/src/file_cache/entry.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use std::io::{Seek, SeekFrom};
22
use std::path::{Path, PathBuf};
33
use std::sync::atomic::AtomicU64;
4-
use std::sync::{Arc, Mutex};
4+
use std::sync::{Arc, LazyLock, Mutex};
55

66
use fs4::fs_std::FileExt;
7-
use once_cell::sync::Lazy;
87
use polars_core::config;
98
use polars_error::{polars_bail, to_compute_err, PolarsError, PolarsResult};
109

@@ -156,7 +155,7 @@ impl Inner {
156155
// * Some(true) => always raise
157156
// * Some(false) => never raise
158157
// * None => do not raise if fallocate() is not permitted, otherwise raise.
159-
static RAISE_ALLOC_ERROR: Lazy<Option<bool>> = Lazy::new(|| {
158+
static RAISE_ALLOC_ERROR: LazyLock<Option<bool>> = LazyLock::new(|| {
160159
let v = match std::env::var("POLARS_IGNORE_FILE_CACHE_ALLOCATE_ERROR").as_deref() {
161160
Ok("1") => Some(false),
162161
Ok("0") => Some(true),

0 commit comments

Comments
 (0)