Skip to content

Commit

Permalink
fix: Improve memory usage during 7zip decompression
Browse files Browse the repository at this point in the history
This change improves memory usage, by only keeping a single archive's
member file in memory at a time during 7zip decompression.

The `py7zr` library does not support streaming decompression yet, so
this change is the best we can do for now.

Potential fix for #1211, but it won't improve memory usage for
single-file 7zip archives.
  • Loading branch information
adamantike committed Oct 6, 2024
1 parent 0b62fe2 commit 149098f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
14 changes: 11 additions & 3 deletions backend/handler/filesystem/roms_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,17 @@ def read_gz_file(file_path: Path) -> Iterator[bytes]:
def read_7z_file(file_path: Path) -> Iterator[bytes]:
try:
with py7zr.SevenZipFile(file_path, "r") as f:
for _name, bio in f.readall().items():
while chunk := bio.read(FILE_READ_CHUNK_SIZE):
yield chunk
for name in f.namelist():
# TODO: This `read` call still reads the member file for this iteration into memory
# (but not the whole 7zip archive). This is because `py7zr` does not support
# streaming decompression yet.
# Related issue: https://github.com/miurahr/py7zr/issues/579
for bio in f.read([name]).values():
while chunk := bio.read(FILE_READ_CHUNK_SIZE):
yield chunk
# Extracting each file separately requires resetting file pointer and decompressor
# between `read` operations.
f.reset()
except (
Bad7zFile,
DecompressionError,
Expand Down
13 changes: 6 additions & 7 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ joserfc = "^0.9.0"
pillow = "^10.3.0"
certifi = "2024.07.04"
python-magic = "^0.4.27"
py7zr = "^0.21.1"
py7zr = "^0.22"
streaming-form-data = "^1.16.0"
zipfile-deflate64 = "^0.2.0"

Expand Down

0 comments on commit 149098f

Please sign in to comment.