mirror of
https://github.com/invoke-ai/InvokeAI
synced 2024-08-30 20:32:17 +00:00
Remove manual memory management in hashlib_hasher in favor of using Python's built-in buffering
This commit is contained in:
parent
e1c16c33a4
commit
119d26e102
@ -163,13 +163,11 @@ class ModelHash:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
def hashlib_hasher(file_path: Path) -> str:
|
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)
|
hasher = hashlib.new(algorithm)
|
||||||
buffer = bytearray(128 * 1024)
|
with open(file_path, "rb") as f:
|
||||||
mv = memoryview(buffer)
|
for chunk in iter(lambda: f.read(128 * 1024), b""):
|
||||||
with open(file_path, "rb", buffering=0) as f:
|
hasher.update(chunk)
|
||||||
while n := f.readinto(mv):
|
|
||||||
hasher.update(mv[:n])
|
|
||||||
return hasher.hexdigest()
|
return hasher.hexdigest()
|
||||||
|
|
||||||
return hashlib_hasher
|
return hashlib_hasher
|
||||||
|
Loading…
Reference in New Issue
Block a user