Skip to content

Commit

Permalink
CoW: Clear dead references every time we add a new one
Browse files Browse the repository at this point in the history
  • Loading branch information
phofl committed Sep 5, 2023
1 parent f034820 commit 3bc7d5d
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 @@ -897,6 +897,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: Block) -> None:
"""Adds a new reference to our reference collection.

Expand All @@ -905,6 +910,7 @@ cdef class BlockValuesRefs:
blk : Block
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 @@ -915,6 +921,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 @@ -927,8 +934,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 3bc7d5d

Please sign in to comment.