From: Daan De Meyer Date: Mon, 8 Jul 2024 21:26:10 +0000 (+0200) Subject: Optimize hash_file() X-Git-Tag: v24~46^2~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2e736afb75726d9afee55ac3ba7e03bd43aa832b;p=thirdparty%2Fmkosi.git Optimize hash_file() Let's reduce unnecessary copies. --- diff --git a/mkosi/util.py b/mkosi/util.py index 8b1d1f199..637b918b5 100644 --- a/mkosi/util.py +++ b/mkosi/util.py @@ -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()