Skip to content

Commit 82a595b

Browse files
authored
PERF: Fix performance regression due to CoW ref tracking (#51390)
1 parent 5700ada commit 82a595b

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

pandas/_libs/internals.pyx

+6-3
Original file line numberDiff line numberDiff line change
@@ -875,10 +875,10 @@ cdef class BlockValuesRefs:
875875
data.
876876
"""
877877
cdef:
878-
public object referenced_blocks
878+
public list referenced_blocks
879879

880880
def __cinit__(self, blk: SharedBlock) -> None:
881-
self.referenced_blocks = weakref.WeakSet([blk])
881+
self.referenced_blocks = [weakref.ref(blk)]
882882

883883
def add_reference(self, blk: SharedBlock) -> None:
884884
"""Adds a new reference to our reference collection.
@@ -888,7 +888,7 @@ cdef class BlockValuesRefs:
888888
blk: SharedBlock
889889
The block that the new references should point to.
890890
"""
891-
self.referenced_blocks.add(blk)
891+
self.referenced_blocks.append(weakref.ref(blk))
892892

893893
def has_reference(self) -> bool:
894894
"""Checks if block has foreign references.
@@ -900,5 +900,8 @@ cdef class BlockValuesRefs:
900900
-------
901901
bool
902902
"""
903+
self.referenced_blocks = [
904+
ref for ref in self.referenced_blocks if ref() is not None
905+
]
903906
# Checking for more references than block pointing to itself
904907
return len(self.referenced_blocks) > 1

0 commit comments

Comments
 (0)