Skip to content

Commit

Permalink
Merge pull request #1246 from alex/freelist-sound
Browse files Browse the repository at this point in the history
Make get_free_list API sound
  • Loading branch information
davidhewitt authored Oct 18, 2020
2 parents 46dc8b0 + bb3b812 commit 95ed5bb
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pyo3-derive-backend/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ fn impl_class(
quote! {
impl pyo3::freelist::PyClassWithFreeList for #cls {
#[inline]
fn get_free_list() -> &'static mut pyo3::freelist::FreeList<*mut pyo3::ffi::PyObject> {
fn get_free_list(_py: pyo3::Python) -> &'static mut pyo3::freelist::FreeList<*mut pyo3::ffi::PyObject> {
static mut FREELIST: *mut pyo3::freelist::FreeList<*mut pyo3::ffi::PyObject> = 0 as *mut _;
unsafe {
if FREELIST.is_null() {
Expand Down
6 changes: 3 additions & 3 deletions src/freelist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::os::raw::c_void;
/// The performance improvement applies to types that are often created and deleted in a row,
/// so that they can benefit from a freelist.
pub trait PyClassWithFreeList {
fn get_free_list() -> &'static mut FreeList<*mut ffi::PyObject>;
fn get_free_list(py: Python) -> &'static mut FreeList<*mut ffi::PyObject>;
}

pub enum Slot<T> {
Expand Down Expand Up @@ -74,7 +74,7 @@ where
unsafe fn new(py: Python, subtype: *mut ffi::PyTypeObject) -> *mut Self::Layout {
// if subtype is not equal to this type, we cannot use the freelist
if subtype == Self::type_object_raw(py) {
if let Some(obj) = <Self as PyClassWithFreeList>::get_free_list().pop() {
if let Some(obj) = <Self as PyClassWithFreeList>::get_free_list(py).pop() {
ffi::PyObject_Init(obj, subtype);
return obj as _;
}
Expand All @@ -91,7 +91,7 @@ where
return;
}

if let Some(obj) = <Self as PyClassWithFreeList>::get_free_list().insert(obj.as_ptr()) {
if let Some(obj) = <Self as PyClassWithFreeList>::get_free_list(py).insert(obj.as_ptr()) {
match (*ffi::Py_TYPE(obj)).tp_free {
Some(free) => free(obj as *mut c_void),
None => tp_free_fallback(obj),
Expand Down

0 comments on commit 95ed5bb

Please sign in to comment.