Skip to content

Commit c06cf56

Browse files
Replace once_cell with std::sync::OnceLock/core::cell::OnceCell (#7077)
* Replace `once_cell` with `std::sync::OnceLock`/`core::cell::OnceCell` - Replace `once_cell::sync::{Lazy, OnceCell}` with `std::sync::{LazyLock, OnceLock}` - Replace `once_cell::unsync::{Lazy, OnceCell}` with `core::cell::{LazyCell, OnceCell}` - Inline `get_or_try_init` at call sites (unstable in std as of 1.93) - Replace `OnceCell::with_value()` with `OnceCell::from()` in codecs.rs - Remove `once_cell` direct dependency from common and vm crates * Auto-format: cargo fmt --all --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 36ff461 commit c06cf56

10 files changed

Lines changed: 80 additions & 51 deletions

File tree

Cargo.lock

Lines changed: 0 additions & 2 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
@@ -184,7 +184,6 @@ num-integer = "0.1.46"
184184
num-traits = "0.2"
185185
num_enum = { version = "0.7", default-features = false }
186186
optional = "0.5"
187-
once_cell = "1.20.3"
188187
parking_lot = "0.12.3"
189188
paste = "1.0.15"
190189
proc-macro2 = "1.0.105"

crates/common/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ malachite-bigint = { workspace = true }
2828
malachite-q = { workspace = true }
2929
malachite-base = { workspace = true }
3030
num-traits = { workspace = true }
31-
once_cell = { workspace = true }
3231
parking_lot = { workspace = true, optional = true }
3332
unicode_names2 = { workspace = true }
3433
radium = { workspace = true }

crates/common/src/lock.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ cfg_if::cfg_if! {
1010
if #[cfg(feature = "threading")] {
1111
pub use parking_lot::{RawMutex, RawRwLock, RawThreadId};
1212

13-
pub use once_cell::sync::{Lazy, OnceCell};
13+
pub use std::sync::{LazyLock as Lazy, OnceLock as OnceCell};
1414
} else {
1515
mod cell_lock;
1616
pub use cell_lock::{RawCellMutex as RawMutex, RawCellRwLock as RawRwLock, SingleThreadId as RawThreadId};
1717

18-
pub use once_cell::unsync::{Lazy, OnceCell};
18+
pub use core::cell::{LazyCell as Lazy, OnceCell};
1919
}
2020
}
2121

crates/common/src/static_cell.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ mod threading {
3131
where
3232
F: FnOnce() -> Result<T, E>,
3333
{
34-
self.inner.get_or_try_init(f)
34+
if let Some(val) = self.inner.get() {
35+
return Ok(val);
36+
}
37+
let val = f()?;
38+
let _ = self.inner.set(val);
39+
Ok(self.inner.get().unwrap())
3540
}
3641
}
3742

@@ -92,8 +97,15 @@ mod non_threading {
9297
where
9398
F: FnOnce() -> Result<T, E>,
9499
{
95-
self.inner
96-
.with(|x| x.get_or_try_init(|| f().map(leak)).copied())
100+
self.inner.with(|x| {
101+
if let Some(val) = x.get() {
102+
Ok(*val)
103+
} else {
104+
let val = leak(f()?);
105+
let _ = x.set(val);
106+
Ok(val)
107+
}
108+
})
97109
}
98110
}
99111

@@ -156,7 +168,12 @@ mod no_std {
156168
where
157169
F: FnOnce() -> Result<T, E>,
158170
{
159-
self.inner.0.get_or_try_init(f)
171+
if let Some(val) = self.inner.0.get() {
172+
return Ok(val);
173+
}
174+
let val = f()?;
175+
let _ = self.inner.0.set(val);
176+
Ok(self.inner.0.get().unwrap())
160177
}
161178
}
162179

crates/vm/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ num-complex = { workspace = true }
6363
num-integer = { workspace = true }
6464
num-traits = { workspace = true }
6565
num_enum = { workspace = true }
66-
once_cell = { workspace = true }
6766
parking_lot = { workspace = true }
6867
paste = { workspace = true }
6968
scoped-tls = { workspace = true }

crates/vm/src/builtins/function.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -816,15 +816,16 @@ impl PyFunction {
816816
#[cfg(feature = "jit")]
817817
#[pymethod]
818818
fn __jit__(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult<()> {
819-
zelf.jitted_code
820-
.get_or_try_init(|| {
821-
let arg_types = jit::get_jit_arg_types(&zelf, vm)?;
822-
let ret_type = jit::jit_ret_type(&zelf, vm)?;
823-
let code = zelf.code.lock();
824-
rustpython_jit::compile(&code.code, &arg_types, ret_type)
825-
.map_err(|err| jit::new_jit_error(err.to_string(), vm))
826-
})
827-
.map(drop)
819+
if zelf.jitted_code.get().is_some() {
820+
return Ok(());
821+
}
822+
let arg_types = jit::get_jit_arg_types(&zelf, vm)?;
823+
let ret_type = jit::jit_ret_type(&zelf, vm)?;
824+
let code = zelf.code.lock();
825+
let compiled = rustpython_jit::compile(&code.code, &arg_types, ret_type)
826+
.map_err(|err| jit::new_jit_error(err.to_string(), vm))?;
827+
let _ = zelf.jitted_code.set(compiled);
828+
Ok(())
828829
}
829830
}
830831

crates/vm/src/builtins/memory.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,15 +1095,16 @@ impl Comparable for PyMemoryView {
10951095

10961096
impl Hashable for PyMemoryView {
10971097
fn hash(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyHash> {
1098-
zelf.hash
1099-
.get_or_try_init(|| {
1100-
zelf.try_not_released(vm)?;
1101-
if !zelf.desc.readonly {
1102-
return Err(vm.new_value_error("cannot hash writable memoryview object"));
1103-
}
1104-
Ok(zelf.contiguous_or_collect(|bytes| vm.state.hash_secret.hash_bytes(bytes)))
1105-
})
1106-
.copied()
1098+
if let Some(val) = zelf.hash.get() {
1099+
return Ok(*val);
1100+
}
1101+
zelf.try_not_released(vm)?;
1102+
if !zelf.desc.readonly {
1103+
return Err(vm.new_value_error("cannot hash writable memoryview object"));
1104+
}
1105+
let val = zelf.contiguous_or_collect(|bytes| vm.state.hash_secret.hash_bytes(bytes));
1106+
let _ = zelf.hash.set(val);
1107+
Ok(*zelf.hash.get().unwrap())
11071108
}
11081109
}
11091110

crates/vm/src/codecs.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rustpython_common::{
88
wtf8::{CodePoint, Wtf8, Wtf8Buf},
99
};
1010

11+
use crate::common::lock::OnceCell;
1112
use crate::{
1213
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyResult, TryFromBorrowedObject,
1314
TryFromObject, VirtualMachine,
@@ -18,7 +19,6 @@ use crate::{
1819
};
1920
use alloc::borrow::Cow;
2021
use core::ops::{self, Range};
21-
use once_cell::unsync::OnceCell;
2222
use std::collections::HashMap;
2323

2424
pub struct CodecsRegistry {
@@ -861,22 +861,25 @@ impl<'a> ErrorsHandler<'a> {
861861
},
862862
None => Self {
863863
errors: identifier!(vm, strict).as_ref(),
864-
resolved: OnceCell::with_value(ResolvedError::Standard(StandardError::Strict)),
864+
resolved: OnceCell::from(ResolvedError::Standard(StandardError::Strict)),
865865
},
866866
}
867867
}
868868
#[inline]
869869
fn resolve(&self, vm: &VirtualMachine) -> PyResult<&ResolvedError> {
870-
self.resolved.get_or_try_init(|| {
871-
if let Ok(standard) = self.errors.as_str().parse() {
872-
Ok(ResolvedError::Standard(standard))
873-
} else {
874-
vm.state
875-
.codec_registry
876-
.lookup_error(self.errors.as_str(), vm)
877-
.map(ResolvedError::Handler)
878-
}
879-
})
870+
if let Some(val) = self.resolved.get() {
871+
return Ok(val);
872+
}
873+
let val = if let Ok(standard) = self.errors.as_str().parse() {
874+
ResolvedError::Standard(standard)
875+
} else {
876+
vm.state
877+
.codec_registry
878+
.lookup_error(self.errors.as_str(), vm)
879+
.map(ResolvedError::Handler)?
880+
};
881+
let _ = self.resolved.set(val);
882+
Ok(self.resolved.get().unwrap())
880883
}
881884
}
882885
impl StrBuffer for PyStrRef {
@@ -998,7 +1001,7 @@ where
9981001
encoding: s_encoding.as_str(),
9991002
data: &s,
10001003
pos: StrSize::default(),
1001-
exception: OnceCell::with_value(err.downcast().unwrap()),
1004+
exception: OnceCell::from(err.downcast().unwrap()),
10021005
};
10031006
let mut iter = s.as_wtf8().code_point_indices();
10041007
let start = StrSize {
@@ -1038,7 +1041,7 @@ where
10381041
data: PyDecodeData::Original(s.borrow_buf()),
10391042
orig_bytes: s.as_object().downcast_ref(),
10401043
pos: 0,
1041-
exception: OnceCell::with_value(err.downcast().unwrap()),
1044+
exception: OnceCell::from(err.downcast().unwrap()),
10421045
};
10431046
let (replace, restart) = handler.handle_decode_error(&mut ctx, range, None)?;
10441047
Ok((replace.into(), restart))
@@ -1061,7 +1064,7 @@ where
10611064
encoding: "",
10621065
data: &s,
10631066
pos: StrSize::default(),
1064-
exception: OnceCell::with_value(err.downcast().unwrap()),
1067+
exception: OnceCell::from(err.downcast().unwrap()),
10651068
};
10661069
let mut iter = s.as_wtf8().code_point_indices();
10671070
let start = StrSize {

crates/vm/src/stdlib/os.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -657,16 +657,28 @@ pub(super) mod _os {
657657
vm,
658658
)
659659
};
660-
let lstat = || self.lstat.get_or_try_init(|| do_stat(false));
660+
let lstat = || match self.lstat.get() {
661+
Some(val) => Ok(val),
662+
None => {
663+
let val = do_stat(false)?;
664+
let _ = self.lstat.set(val);
665+
Ok(self.lstat.get().unwrap())
666+
}
667+
};
661668
let stat = if follow_symlinks.0 {
662669
// if follow_symlinks == true and we aren't a symlink, cache both stat and lstat
663-
self.stat.get_or_try_init(|| {
664-
if self.is_symlink(vm)? {
665-
do_stat(true)
666-
} else {
667-
lstat().cloned()
670+
match self.stat.get() {
671+
Some(val) => val,
672+
None => {
673+
let val = if self.is_symlink(vm)? {
674+
do_stat(true)?
675+
} else {
676+
lstat()?.clone()
677+
};
678+
let _ = self.stat.set(val);
679+
self.stat.get().unwrap()
668680
}
669-
})?
681+
}
670682
} else {
671683
lstat()?
672684
};

0 commit comments

Comments
 (0)