From 634c40f8b9cff965787e538f1a810a63b9987959 Mon Sep 17 00:00:00 2001 From: Miauwkeru Date: Tue, 20 Feb 2024 09:57:39 +0000 Subject: [PATCH 1/2] Move lru_cache definitions to __init__ Using the lru_cache decorators on class methods, the ones that have a reference to `self`, will also cache self. So we move it to the __init__ of the class (DIS-2913) --- dissect/squashfs/squashfs.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/dissect/squashfs/squashfs.py b/dissect/squashfs/squashfs.py index 882f199..3ae710a 100644 --- a/dissect/squashfs/squashfs.py +++ b/dissect/squashfs/squashfs.py @@ -29,6 +29,11 @@ class SquashFS: def __init__(self, fh: BinaryIO): self.fh = fh + self._read_block = lru_cache(1024)(self._read_block) + self._lookup_id = lru_cache(1024)(self._lookup_id) + self._lookup_inode = lru_cache(1024)(self._lookup_inode) + self._lookup_fragment = lru_cache(1024)(self._lookup_fragment) + sb = c_squashfs.squashfs_super_block(fh) if sb.s_magic != c_squashfs.SQUASHFS_MAGIC: raise ValueError("Invalid squashfs superblock") @@ -131,7 +136,6 @@ def _read_metadata(self, block: int, offset: int, length: int) -> tuple[int, int return block, offset, b"".join(result) - @lru_cache(1024) def _read_block(self, block: int, length: Optional[int] = None) -> tuple[int, bytes]: if length is not None: # Data block @@ -162,13 +166,11 @@ def _read_fragment(self, fragment: int, offset: int, length: int) -> bytes: _, data = self._read_block(entry.start_block, entry.size) return data[offset : offset + length] - @lru_cache(1024) def _lookup_id(self, id: int) -> int: block, offset = divmod(id * 4, c_squashfs.SQUASHFS_METADATA_SIZE) _, _, data = self._read_metadata(self.id_table[block], offset, 4) return struct.unpack(" INode: if inode_number <= 0 or inode_number > self.sb.inodes: raise IndexError(f"inode number out of bounds (1, {self.sb.inodes}): {inode_number}") @@ -176,7 +178,6 @@ def _lookup_inode(self, inode_number: int) -> INode: _, _, data = self._read_metadata(self.lookup_table[block], offset, 8) return self.get(struct.unpack(" bytes: fragment_offset = fragment * len(c_squashfs.squashfs_fragment_entry) block, offset = divmod(fragment_offset, c_squashfs.SQUASHFS_METADATA_SIZE) From 44471dfd3c1c3dca8a123ffd49205414e8444f3f Mon Sep 17 00:00:00 2001 From: Miauwkeru Date: Tue, 20 Feb 2024 13:09:57 +0000 Subject: [PATCH 2/2] Move lru_cache definitions down --- dissect/squashfs/squashfs.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/dissect/squashfs/squashfs.py b/dissect/squashfs/squashfs.py index 3ae710a..abced67 100644 --- a/dissect/squashfs/squashfs.py +++ b/dissect/squashfs/squashfs.py @@ -29,11 +29,6 @@ class SquashFS: def __init__(self, fh: BinaryIO): self.fh = fh - self._read_block = lru_cache(1024)(self._read_block) - self._lookup_id = lru_cache(1024)(self._lookup_id) - self._lookup_inode = lru_cache(1024)(self._lookup_inode) - self._lookup_fragment = lru_cache(1024)(self._lookup_fragment) - sb = c_squashfs.squashfs_super_block(fh) if sb.s_magic != c_squashfs.SQUASHFS_MAGIC: raise ValueError("Invalid squashfs superblock") @@ -51,6 +46,11 @@ def __init__(self, fh: BinaryIO): self.minor = self.sb.s_minor self.size = self.sb.bytes_used + self._read_block = lru_cache(1024)(self._read_block) + self._lookup_id = lru_cache(1024)(self._lookup_id) + self._lookup_inode = lru_cache(1024)(self._lookup_inode) + self._lookup_fragment = lru_cache(1024)(self._lookup_fragment) + self._compression_options = None if (self.sb.flags >> c_squashfs.SQUASHFS_COMP_OPT) & 1: self._compression_options = self._read_block(len(self.sb))[1]