Skip to content

Commit

Permalink
Backport PR #55008 on branch 2.1.x (CoW: Clear dead references every …
Browse files Browse the repository at this point in the history
…time we add a new one) (#55220)

CoW: Clear dead references every time we add a new one (#55008)

(cherry picked from commit 7134f2c)
  • Loading branch information
phofl committed Sep 20, 2023
1 parent 21d8cdd commit 7c93cc9
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions pandas/_libs/internals.pyx
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 7c93cc9

Please sign in to comment.