Skip to content

Commit 5ec8fad

Browse files
committed
Improve performance of BlockValuesRefs._clear_dead_references(...)
1 parent 91541c1 commit 5ec8fad

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

pandas/_libs/internals.pyx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
from collections import defaultdict
22

33
cimport cython
4+
from cpython.object cimport PyObject
45
from cpython.pyport cimport PY_SSIZE_T_MAX
56
from cpython.slice cimport PySlice_GetIndicesEx
6-
from cpython.weakref cimport PyWeakref_NewRef
7+
from cpython.weakref cimport (
8+
PyWeakref_GetObject,
9+
PyWeakref_NewRef,
10+
)
711
from cython cimport Py_ssize_t
812

913
import numpy as np
@@ -25,6 +29,9 @@ from pandas._libs.util cimport (
2529
is_integer_object,
2630
)
2731

32+
cdef extern from "Python.h":
33+
PyObject* Py_None
34+
2835

2936
@cython.final
3037
@cython.freelist(32)
@@ -902,7 +909,8 @@ cdef class BlockValuesRefs:
902909
# see GH#55245 and GH#55008
903910
if force or len(self.referenced_blocks) > self.clear_counter:
904911
self.referenced_blocks = [
905-
ref for ref in self.referenced_blocks if ref() is not None
912+
ref for ref in self.referenced_blocks
913+
if PyWeakref_GetObject(ref) != Py_None
906914
]
907915
nr_of_refs = len(self.referenced_blocks)
908916
if nr_of_refs < self.clear_counter // 2:

0 commit comments

Comments
 (0)