Skip to content

Commit

Permalink
CoW: Clear dead references every time we add a new one (pandas-dev#55008
Browse files Browse the repository at this point in the history
)

(cherry picked from commit 7134f2c)
  • Loading branch information
phofl committed Sep 20, 2023
1 parent 652fd13 commit ab3dc20
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions pandas/_libs/internals.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,11 @@ cdef class BlockValuesRefs:
else:
self.referenced_blocks = []

def _clear_dead_references(self) -> None:
self.referenced_blocks = [
ref for ref in self.referenced_blocks if ref() is not None
]

def add_reference(self, blk: SharedBlock) -> None:
"""Adds a new reference to our reference collection.

Expand All @@ -959,6 +964,7 @@ cdef class BlockValuesRefs:
blk: SharedBlock
The block that the new references should point to.
"""
self._clear_dead_references()
self.referenced_blocks.append(weakref.ref(blk))

def add_index_reference(self, index: object) -> None:
Expand All @@ -969,6 +975,7 @@ cdef class BlockValuesRefs:
index : Index
The index that the new reference should point to.
"""
self._clear_dead_references()
self.referenced_blocks.append(weakref.ref(index))

def has_reference(self) -> bool:
Expand All @@ -981,8 +988,6 @@ cdef class BlockValuesRefs:
-------
bool
"""
self.referenced_blocks = [
ref for ref in self.referenced_blocks if ref() is not None
]
self._clear_dead_references()
# Checking for more references than block pointing to itself
return len(self.referenced_blocks) > 1

0 comments on commit ab3dc20

Please sign in to comment.