Remove manual memory management in hashlib_hasher in favor of using Python's built-in buffering

This commit is contained in:
Brandon Rising 2024-03-07 14:08:15 -05:00
parent e1c16c33a4
commit 119d26e102

View File

@ -163,13 +163,11 @@ class ModelHash:
"""
def hashlib_hasher(file_path: Path) -> str:
"""Hashes a file using a hashlib algorithm. Uses `memoryview` to avoid reading the entire file into memory."""
"""Hashes a file using a hashlib algorithm."""
hasher = hashlib.new(algorithm)
buffer = bytearray(128 * 1024)
mv = memoryview(buffer)
with open(file_path, "rb", buffering=0) as f:
while n := f.readinto(mv):
hasher.update(mv[:n])
with open(file_path, "rb") as f:
for chunk in iter(lambda: f.read(128 * 1024), b""):
hasher.update(chunk)
return hasher.hexdigest()
return hashlib_hasher