Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify entropy calculation to use mmap instead of read_bytes() to reduce memory usage for large files #138

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion fact_extractor/helperFunctions/statistics.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from configparser import ConfigParser
from contextlib import suppress
from mmap import mmap
from pathlib import Path
from typing import Dict, List

Expand All @@ -23,7 +24,7 @@ def add_unpack_statistics(extraction_dir: Path, meta_data: Dict):
meta_data['number_of_unpacked_directories'] = unpacked_directories


def get_unpack_status(file_path: str, binary: bytes, extracted_files: List[Path], meta_data: Dict, config: ConfigParser):
def get_unpack_status(file_path: str, binary: bytes | mmap, extracted_files: List[Path], meta_data: Dict, config: ConfigParser):
meta_data['summary'] = []
meta_data['entropy'] = avg_entropy(binary)

Expand Down
6 changes: 4 additions & 2 deletions fact_extractor/unpacker/unpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import json
import logging
import mmap
import shutil
from pathlib import Path
from tempfile import TemporaryDirectory
Expand Down Expand Up @@ -52,9 +53,10 @@ def unpack(self, file_path):

compute_stats = self.config.getboolean('ExpertSettings', 'statistics', fallback=True)
if compute_stats:
binary = Path(file_path).read_bytes()
add_unpack_statistics(self._file_folder, meta_data)
get_unpack_status(file_path, binary, extracted_files, meta_data, self.config)
with open(file_path, 'rb') as f:
with mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) as mm:
get_unpack_status(file_path, mm, extracted_files, meta_data, self.config)

self.cleanup(tmp_dir)

Expand Down