]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Optimize hash_file()
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Mon, 8 Jul 2024 21:26:10 +0000 (23:26 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 9 Jul 2024 11:16:18 +0000 (13:16 +0200)
Let's reduce unnecessary copies.

mkosi/util.py

index 8b1d1f199a49424773d50dbff72d69557b2a4f61..637b918b50714651c405f8fe756c8b3f8b07a6b9 100644 (file)
@@ -309,12 +309,13 @@ def resource_path(mod: ModuleType) -> Iterator[Path]:
 
 def hash_file(path: Path) -> str:
     # TODO Replace with hashlib.file_digest after dropping support for Python 3.10.
-    bs = 16 * 1024**2
     h = hashlib.sha256()
+    b  = bytearray(16 * 1024**2)
+    mv = memoryview(b)
 
-    with path.open("rb") as sf:
-        while (buf := sf.read(bs)):
-            h.update(buf)
+    with path.open("rb", buffering=0) as f:
+        while n := f.readinto(mv):
+            h.update(mv[:n])
 
     return h.hexdigest()