Skip to content
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
2 changes: 0 additions & 2 deletions 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ num-integer = "0.1.46"
num-traits = "0.2"
num_enum = { version = "0.7", default-features = false }
optional = "0.5"
once_cell = "1.20.3"
parking_lot = "0.12.3"
paste = "1.0.15"
proc-macro2 = "1.0.105"
Expand Down
1 change: 0 additions & 1 deletion crates/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ malachite-bigint = { workspace = true }
malachite-q = { workspace = true }
malachite-base = { workspace = true }
num-traits = { workspace = true }
once_cell = { workspace = true }
parking_lot = { workspace = true, optional = true }
unicode_names2 = { workspace = true }
radium = { workspace = true }
Expand Down
4 changes: 2 additions & 2 deletions crates/common/src/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ cfg_if::cfg_if! {
if #[cfg(feature = "threading")] {
pub use parking_lot::{RawMutex, RawRwLock, RawThreadId};

pub use once_cell::sync::{Lazy, OnceCell};
pub use std::sync::{LazyLock as Lazy, OnceLock as OnceCell};
} else {
mod cell_lock;
pub use cell_lock::{RawCellMutex as RawMutex, RawCellRwLock as RawRwLock, SingleThreadId as RawThreadId};

pub use once_cell::unsync::{Lazy, OnceCell};
pub use core::cell::{LazyCell as Lazy, OnceCell};
}
}

Expand Down
25 changes: 21 additions & 4 deletions crates/common/src/static_cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ mod threading {
where
F: FnOnce() -> Result<T, E>,
{
self.inner.get_or_try_init(f)
if let Some(val) = self.inner.get() {
return Ok(val);
}
let val = f()?;
let _ = self.inner.set(val);
Ok(self.inner.get().unwrap())
}
}

Expand Down Expand Up @@ -92,8 +97,15 @@ mod non_threading {
where
F: FnOnce() -> Result<T, E>,
{
self.inner
.with(|x| x.get_or_try_init(|| f().map(leak)).copied())
self.inner.with(|x| {
if let Some(val) = x.get() {
Ok(*val)
} else {
let val = leak(f()?);
let _ = x.set(val);
Ok(val)
}
})
}
}

Expand Down Expand Up @@ -156,7 +168,12 @@ mod no_std {
where
F: FnOnce() -> Result<T, E>,
{
self.inner.0.get_or_try_init(f)
if let Some(val) = self.inner.0.get() {
return Ok(val);
}
let val = f()?;
let _ = self.inner.0.set(val);
Ok(self.inner.0.get().unwrap())
}
}

Expand Down
1 change: 0 additions & 1 deletion crates/vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ num-complex = { workspace = true }
num-integer = { workspace = true }
num-traits = { workspace = true }
num_enum = { workspace = true }
once_cell = { workspace = true }
parking_lot = { workspace = true }
paste = { workspace = true }
scoped-tls = { workspace = true }
Expand Down
19 changes: 10 additions & 9 deletions crates/vm/src/builtins/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -816,15 +816,16 @@ impl PyFunction {
#[cfg(feature = "jit")]
#[pymethod]
fn __jit__(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult<()> {
zelf.jitted_code
.get_or_try_init(|| {
let arg_types = jit::get_jit_arg_types(&zelf, vm)?;
let ret_type = jit::jit_ret_type(&zelf, vm)?;
let code = zelf.code.lock();
rustpython_jit::compile(&code.code, &arg_types, ret_type)
.map_err(|err| jit::new_jit_error(err.to_string(), vm))
})
.map(drop)
if zelf.jitted_code.get().is_some() {
return Ok(());
}
let arg_types = jit::get_jit_arg_types(&zelf, vm)?;
let ret_type = jit::jit_ret_type(&zelf, vm)?;
let code = zelf.code.lock();
let compiled = rustpython_jit::compile(&code.code, &arg_types, ret_type)
.map_err(|err| jit::new_jit_error(err.to_string(), vm))?;
let _ = zelf.jitted_code.set(compiled);
Ok(())
}
}

Expand Down
19 changes: 10 additions & 9 deletions crates/vm/src/builtins/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1095,15 +1095,16 @@ impl Comparable for PyMemoryView {

impl Hashable for PyMemoryView {
fn hash(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyHash> {
zelf.hash
.get_or_try_init(|| {
zelf.try_not_released(vm)?;
if !zelf.desc.readonly {
return Err(vm.new_value_error("cannot hash writable memoryview object"));
}
Ok(zelf.contiguous_or_collect(|bytes| vm.state.hash_secret.hash_bytes(bytes)))
})
.copied()
if let Some(val) = zelf.hash.get() {
return Ok(*val);
}
zelf.try_not_released(vm)?;
if !zelf.desc.readonly {
return Err(vm.new_value_error("cannot hash writable memoryview object"));
}
let val = zelf.contiguous_or_collect(|bytes| vm.state.hash_secret.hash_bytes(bytes));
let _ = zelf.hash.set(val);
Ok(*zelf.hash.get().unwrap())
}
}

Expand Down
33 changes: 18 additions & 15 deletions crates/vm/src/codecs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use rustpython_common::{
wtf8::{CodePoint, Wtf8, Wtf8Buf},
};

use crate::common::lock::OnceCell;
use crate::{
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyResult, TryFromBorrowedObject,
TryFromObject, VirtualMachine,
Expand All @@ -18,7 +19,6 @@ use crate::{
};
use alloc::borrow::Cow;
use core::ops::{self, Range};
use once_cell::unsync::OnceCell;
use std::collections::HashMap;

pub struct CodecsRegistry {
Expand Down Expand Up @@ -861,22 +861,25 @@ impl<'a> ErrorsHandler<'a> {
},
None => Self {
errors: identifier!(vm, strict).as_ref(),
resolved: OnceCell::with_value(ResolvedError::Standard(StandardError::Strict)),
resolved: OnceCell::from(ResolvedError::Standard(StandardError::Strict)),
},
}
}
#[inline]
fn resolve(&self, vm: &VirtualMachine) -> PyResult<&ResolvedError> {
self.resolved.get_or_try_init(|| {
if let Ok(standard) = self.errors.as_str().parse() {
Ok(ResolvedError::Standard(standard))
} else {
vm.state
.codec_registry
.lookup_error(self.errors.as_str(), vm)
.map(ResolvedError::Handler)
}
})
if let Some(val) = self.resolved.get() {
return Ok(val);
}
let val = if let Ok(standard) = self.errors.as_str().parse() {
ResolvedError::Standard(standard)
} else {
vm.state
.codec_registry
.lookup_error(self.errors.as_str(), vm)
.map(ResolvedError::Handler)?
};
let _ = self.resolved.set(val);
Ok(self.resolved.get().unwrap())
}
}
impl StrBuffer for PyStrRef {
Expand Down Expand Up @@ -998,7 +1001,7 @@ where
encoding: s_encoding.as_str(),
data: &s,
pos: StrSize::default(),
exception: OnceCell::with_value(err.downcast().unwrap()),
exception: OnceCell::from(err.downcast().unwrap()),
};
let mut iter = s.as_wtf8().code_point_indices();
let start = StrSize {
Expand Down Expand Up @@ -1038,7 +1041,7 @@ where
data: PyDecodeData::Original(s.borrow_buf()),
orig_bytes: s.as_object().downcast_ref(),
pos: 0,
exception: OnceCell::with_value(err.downcast().unwrap()),
exception: OnceCell::from(err.downcast().unwrap()),
};
let (replace, restart) = handler.handle_decode_error(&mut ctx, range, None)?;
Ok((replace.into(), restart))
Expand All @@ -1061,7 +1064,7 @@ where
encoding: "",
data: &s,
pos: StrSize::default(),
exception: OnceCell::with_value(err.downcast().unwrap()),
exception: OnceCell::from(err.downcast().unwrap()),
};
let mut iter = s.as_wtf8().code_point_indices();
let start = StrSize {
Expand Down
26 changes: 19 additions & 7 deletions crates/vm/src/stdlib/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -657,16 +657,28 @@ pub(super) mod _os {
vm,
)
};
let lstat = || self.lstat.get_or_try_init(|| do_stat(false));
let lstat = || match self.lstat.get() {
Some(val) => Ok(val),
None => {
let val = do_stat(false)?;
let _ = self.lstat.set(val);
Ok(self.lstat.get().unwrap())
}
};
let stat = if follow_symlinks.0 {
// if follow_symlinks == true and we aren't a symlink, cache both stat and lstat
self.stat.get_or_try_init(|| {
if self.is_symlink(vm)? {
do_stat(true)
} else {
lstat().cloned()
match self.stat.get() {
Some(val) => val,
None => {
let val = if self.is_symlink(vm)? {
do_stat(true)?
} else {
lstat()?.clone()
};
let _ = self.stat.set(val);
self.stat.get().unwrap()
}
})?
}
} else {
lstat()?
};
Expand Down
Loading