Skip to content

Commit 73f438c

Browse files
committed
fixed memory issues with getitem fastpath
1 parent 202de07 commit 73f438c

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

pandas/_libs/arrays.pyx

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ cdef class BitmaskArray:
397397

398398
ArrowBitmapInit(&bitmap)
399399
ArrowBitmapReserve(&bitmap, total_bits)
400+
400401
ConcatenateBitmapData(bitmaps, nbitmaps, &bitmap)
401402
free(bitmaps)
402403

@@ -492,14 +493,14 @@ cdef class BitmaskArray:
492493
else:
493494
nbits = stop
494495

495-
nbytes = (stop + 7) // 8
496+
nbytes = (nbits + 7) // 8
496497

497498
bma = BitmaskArray.__new__(BitmaskArray)
498499
ArrowBitmapInit(&bitmap)
499500
ArrowBitmapReserve(&bitmap, nbits)
500501
memcpy(bitmap.buffer.data, self_.bitmap.buffer.data, nbytes)
501502
bitmap.buffer.size_bytes = nbytes
502-
bitmap.size_bits = stop
503+
bitmap.size_bits = nbits
503504

504505
bma.bitmap = bitmap
505506
bma.buffer_owner = True
@@ -557,10 +558,11 @@ cdef class BitmaskArray:
557558
BitmapAnd(&self_.bitmap, &other_bma.bitmap, &bitmap)
558559

559560
result = np.empty(self_.bitmap.size_bits, dtype=bool)
560-
BitmaskArray.buffer_to_array_1d(
561-
result,
561+
ArrowBitsUnpackInt8(
562562
bitmap.buffer.data,
563-
bitmap.size_bits
563+
0,
564+
bitmap.size_bits,
565+
<int8_t*>&result[0]
564566
)
565567
ArrowBitmapReset(&bitmap)
566568

@@ -592,10 +594,11 @@ cdef class BitmaskArray:
592594
BitmapOr(&self_.bitmap, &other_bma.bitmap, &bitmap)
593595

594596
result = np.empty(self_.bitmap.size_bits, dtype=bool)
595-
BitmaskArray.buffer_to_array_1d(
596-
result,
597+
ArrowBitsUnpackInt8(
597598
bitmap.buffer.data,
598-
bitmap.size_bits
599+
0,
600+
bitmap.size_bits,
601+
<int8_t*>&result[0]
599602
)
600603
ArrowBitmapReset(&bitmap)
601604

@@ -627,10 +630,11 @@ cdef class BitmaskArray:
627630
BitmapXor(&self_.bitmap, &other_bma.bitmap, &bitmap)
628631

629632
result = np.empty(self_.bitmap.size_bits, dtype=bool)
630-
BitmaskArray.buffer_to_array_1d(
631-
result,
633+
ArrowBitsUnpackInt8(
632634
bitmap.buffer.data,
633-
bitmap.size_bits
635+
0,
636+
bitmap.size_bits,
637+
<int8_t*>&result[0]
634638
)
635639
ArrowBitmapReset(&bitmap)
636640
if self_.ndim == 2:
@@ -737,7 +741,7 @@ cdef class BitmaskArray:
737741
self_.bitmap.buffer.data,
738742
0,
739743
self_.bitmap.size_bits,
740-
<const int8_t*>self_.memview_buf
744+
<int8_t*>self_.memview_buf
741745
)
742746

743747
buffer.buf = self_.memview_buf
@@ -840,19 +844,15 @@ cdef class BitmaskArray:
840844
def copy(self):
841845
return BitmaskArray.copy_from_bitmaskarray(self)
842846

843-
@cython.boundscheck(False) # TODO: Removing this causes an IndexError? Zero size?
844-
@cython.wraparound(False)
845-
@staticmethod
846-
cdef void buffer_to_array_1d(uint8_t[:] out, const uint8_t* buf, Py_ssize_t size):
847-
ArrowBitsUnpackInt8(buf, 0, size, <const int8_t*>&out[0])
848-
849847
def to_numpy(self) -> ndarray:
850848
cdef BitmaskArray self_ = self
851-
cdef ndarray[uint8_t] result = np.empty(self.bitmap.size_bits, dtype=bool)
852-
BitmaskArray.buffer_to_array_1d(
853-
result,
854-
self.bitmap.buffer.data,
855-
self.bitmap.size_bits
849+
cdef ndarray[uint8_t] result = np.empty(self_.bitmap.size_bits, dtype=bool)
850+
851+
ArrowBitsUnpackInt8(
852+
self_.bitmap.buffer.data,
853+
0,
854+
self_.bitmap.size_bits,
855+
<int8_t*>cnp.PyArray_BYTES(result),
856856
)
857857

858858
if self_.ndim == 2:

0 commit comments

Comments
 (0)