Patchwork [3,of,4] worker: stop relying on garbage collection to release memoryview

login
register
mail settings
Submitter Manuel Jacob
Date May 22, 2022, 12:37 a.m.
Message ID <f8b3781ee6a2241fe40d.1653179833@tmp>
Download mbox | patch
Permalink /patch/50985/
State New
Headers show

Comments

Manuel Jacob - May 22, 2022, 12:37 a.m.
# HG changeset patch
# User Manuel Jacob <me@manueljacob.de>
# Date 1653168690 -7200
#      Sat May 21 23:31:30 2022 +0200
# Node ID f8b3781ee6a2241fe40d5420f5d0293947f94022
# Parent  ff98395f2742f27fa1e2e7ddbe31a3b810e470bc
# EXP-Topic worker-improvements
worker: stop relying on garbage collection to release memoryview

On CPython, before resizing the bytearray, all memoryviews referencing it must
be released. Before this change, we ensured that all references to them were
deleted. On CPython, this was enough to set the reference count to zero, which
results in garbage collecting and releasing them.

On PyPy, releasing the memoryviews is not necessary because they are implemented
differently. If it would be necessary however, ensuring that all references are
deleted would not be suffient because PyPy doesn’t use reference counting.

By using with statements that take care of releasing the memoryviews, we ensure
that the bytearray is resizable without relying on implementation details. So
while this doesn’t fix any observable bug, it increases compatiblity with other
and future Python implementations.

Patch

diff --git a/mercurial/worker.py b/mercurial/worker.py
--- a/mercurial/worker.py
+++ b/mercurial/worker.py
@@ -92,16 +92,16 @@ 
             return self._wrapped.readall()
 
         buf = bytearray(size)
-        view = memoryview(buf)
         pos = 0
 
-        while pos < size:
-            ret = self._wrapped.readinto(view[pos:])
-            if not ret:
-                break
-            pos += ret
+        with memoryview(buf) as view:
+            while pos < size:
+                with view[pos:] as subview:
+                    ret = self._wrapped.readinto(subview)
+                if not ret:
+                    break
+                pos += ret
 
-        del view
         del buf[pos:]
         return bytes(buf)