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

support 3.13t free-threaded python #471

Merged
merged 12 commits into from
Feb 19, 2025
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ jobs:
"3.11",
"3.12",
"3.13",
"3.13t",
"pypy-3.9",
"pypy-3.10",
]
Expand Down Expand Up @@ -108,7 +109,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
uses: Quansight-Labs/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
architecture: ${{ matrix.platform.python-architecture }}
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- removed the `gil-refs` feature
- reintroduced function names without `_bound` suffix + deprecating the old names
- switched to `IntoPyObject` as trait bound
- Support Python 3.13t "free-threaded" Python. ([#471](https://github.com/PyO3/rust-numpy/pull/471)

- v0.22.1
- Fix building on 32-bit Windows. ([#463](https://github.com/PyO3/rust-numpy/pull/463))
Expand Down
12 changes: 6 additions & 6 deletions src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@
//! [scalars-datetime64]: https://numpy.org/doc/stable/reference/arrays.scalars.html#numpy.datetime64
//! [scalars-timedelta64]: https://numpy.org/doc/stable/reference/arrays.scalars.html#numpy.timedelta64

use std::cell::RefCell;
use std::collections::hash_map::Entry;
use std::fmt;
use std::hash::Hash;
use std::marker::PhantomData;
use std::sync::Mutex;

use pyo3::{sync::GILProtected, Bound, Py, Python};
use pyo3::{Bound, Py, Python};
use rustc_hash::FxHashMap;

use crate::dtype::{clone_methods_impl, Element, PyArrayDescr, PyArrayDescrMethods};
Expand Down Expand Up @@ -209,22 +209,22 @@ impl<U: Unit> fmt::Debug for Timedelta<U> {

struct TypeDescriptors {
npy_type: NPY_TYPES,
#[allow(clippy::type_complexity)]
dtypes: GILProtected<RefCell<Option<FxHashMap<NPY_DATETIMEUNIT, Py<PyArrayDescr>>>>>,
dtypes: Mutex<Option<FxHashMap<NPY_DATETIMEUNIT, Py<PyArrayDescr>>>>,
}

impl TypeDescriptors {
/// `npy_type` must be either `NPY_DATETIME` or `NPY_TIMEDELTA`.
const unsafe fn new(npy_type: NPY_TYPES) -> Self {
Self {
npy_type,
dtypes: GILProtected::new(RefCell::new(None)),
dtypes: Mutex::new(None),
}
}

#[allow(clippy::wrong_self_convention)]
fn from_unit<'py>(&self, py: Python<'py>, unit: NPY_DATETIMEUNIT) -> Bound<'py, PyArrayDescr> {
let mut dtypes = self.dtypes.get(py).borrow_mut();
// FIXME probably a deadlock risk here due to the GIL? Might need MutexExt trait in PyO3
let mut dtypes = self.dtypes.lock().expect("dtype cache poisoned");

let dtype = match dtypes.get_or_insert_with(Default::default).entry(unit) {
Entry::Occupied(entry) => entry.into_mut(),
Expand Down
11 changes: 5 additions & 6 deletions src/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
//! [ascii]: https://numpy.org/doc/stable/reference/c-api/dtype.html#c.NPY_STRING
//! [ucs4]: https://numpy.org/doc/stable/reference/c-api/dtype.html#c.NPY_UNICODE

use std::cell::RefCell;
use std::collections::hash_map::Entry;
use std::fmt;
use std::mem::size_of;
use std::os::raw::c_char;
use std::str;
use std::sync::Mutex;

use pyo3::{
ffi::{Py_UCS1, Py_UCS4},
sync::GILProtected,
Bound, Py, Python,
};
use rustc_hash::FxHashMap;
Expand Down Expand Up @@ -160,14 +159,13 @@ unsafe impl<const N: usize> Element for PyFixedUnicode<N> {
}

struct TypeDescriptors {
#[allow(clippy::type_complexity)]
dtypes: GILProtected<RefCell<Option<FxHashMap<usize, Py<PyArrayDescr>>>>>,
dtypes: Mutex<Option<FxHashMap<usize, Py<PyArrayDescr>>>>,
}

impl TypeDescriptors {
const fn new() -> Self {
Self {
dtypes: GILProtected::new(RefCell::new(None)),
dtypes: Mutex::new(None),
}
}

Expand All @@ -180,7 +178,8 @@ impl TypeDescriptors {
byteorder: c_char,
size: usize,
) -> Bound<'py, PyArrayDescr> {
let mut dtypes = self.dtypes.get(py).borrow_mut();
// FIXME probably a deadlock risk here due to the GIL? Might need MutexExt trait in PyO3
let mut dtypes = self.dtypes.lock().expect("dtype cache poisoned");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choice to use Mutex here makes me want to add a MutexExt trait to PyO3 similar to OnceLockExt which we already added.

Given that writes to this should be infrequent (it's a global cache, as far as I can tell), I also wonder if RwLock is appropriate here. Readers are extremely short-lived so the analysis in https://blog.nelhage.com/post/rwlock-contention/ seems to be a non-issue (cc @alex)

... in which case I want RwLockExt too 😂

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I guess I should finally write MutexExt...


let dtype = match dtypes.get_or_insert_with(Default::default).entry(size) {
Entry::Occupied(entry) => entry.into_mut(),
Expand Down
Loading