Skip to content
This repository has been archived by the owner on Jan 3, 2024. It is now read-only.

Commit

Permalink
add_memory_pressure() tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
arigo committed Nov 13, 2018
1 parent 96c1910 commit 7f2f6b5
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions pypy/module/array/interp_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ def __del__(self):
lltype.free(self._buffer, flavor='raw')

def setlen(self, size, zero=False, overallocate=True):
if self._buffer:
delta_memory_pressure = -self.allocated * self.itemsize
else:
delta_memory_pressure = 0
if size > 0:
if size > self.allocated or size < self.allocated / 2:
if overallocate:
Expand All @@ -171,14 +175,13 @@ def setlen(self, size, zero=False, overallocate=True):
some = 0
self.allocated = size + some
byte_size = self.allocated * self.itemsize
delta_memory_pressure += byte_size
if zero:
new_buffer = lltype.malloc(
rffi.CCHARP.TO, byte_size, flavor='raw',
add_memory_pressure=True, zero=True)
rffi.CCHARP.TO, byte_size, flavor='raw', zero=True)
else:
new_buffer = lltype.malloc(
rffi.CCHARP.TO, byte_size, flavor='raw',
add_memory_pressure=True)
rffi.CCHARP.TO, byte_size, flavor='raw')
copy_bytes = min(size, self.len) * self.itemsize
rffi.c_memcpy(rffi.cast(rffi.VOIDP, new_buffer),
rffi.cast(rffi.VOIDP, self._buffer),
Expand All @@ -195,6 +198,11 @@ def setlen(self, size, zero=False, overallocate=True):
lltype.free(self._buffer, flavor='raw')
self._buffer = new_buffer
self.len = size
# adds the difference between the old and the new raw-malloced
# size. If setlen() is called a lot on the same array object,
# it is important to take into account the fact that we also do
# lltype.free() above.
rgc.add_memory_pressure(delta_memory_pressure)

def _fromiterable(self, w_seq):
# used by fromsequence().
Expand Down Expand Up @@ -239,8 +247,10 @@ def delitem(self, space, i, j):
return None
oldbuffer = self._buffer
self._buffer = lltype.malloc(rffi.CCHARP.TO,
(self.len - (j - i)) * self.itemsize, flavor='raw',
add_memory_pressure=True)
(self.len - (j - i)) * self.itemsize, flavor='raw')
# Issue #2913: don't pass add_memory_pressure here, otherwise
# memory pressure grows but actual raw memory usage doesn't---we
# are freeing the old buffer at the end of this function.
if i:
rffi.c_memcpy(
rffi.cast(rffi.VOIDP, self._buffer),
Expand Down

0 comments on commit 7f2f6b5

Please sign in to comment.