Skip to content

Commit

Permalink
Move lru_cache definitions to __init__ (#30)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
Miauwkeru authored Feb 20, 2024
1 parent 0b22e50 commit a3ea169
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 4 deletions.
3 changes: 2 additions & 1 deletion dissect/hypervisor/disk/qcow2.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ def __init__(self, fh, data_file=None, backing_file=None):
if backing_file != ALLOW_NO_BACKING_FILE:
self.backing_file = backing_file

self.l2_table = lru_cache(128)(self.l2_table)

super().__init__(self.header.size)

def _read_extensions(self):
Expand Down Expand Up @@ -167,7 +169,6 @@ def l1_table(self):
self.fh.seek(self.header.l1_table_offset)
return c_qcow2.uint64[self.header.l1_size](self.fh)

@lru_cache(maxsize=128)
def l2_table(self, l2_offset):
return L2Table(self, l2_offset)

Expand Down
3 changes: 2 additions & 1 deletion dissect/hypervisor/disk/vhd.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ def __init__(self, fh, offset, max_entries):
self.offset = offset
self.max_entries = max_entries

@lru_cache(4096)
self.get = lru_cache(4096)(self.get)

def get(self, block):
# This could be improved by caching the entire BAT (or chunks if too large)
if block + 1 > self.max_entries:
Expand Down
3 changes: 2 additions & 1 deletion dissect/hypervisor/disk/vhdx.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ def __init__(self, vhdx, offset):
else:
self.entry_count = self._pb_count + ((self._pb_count - 1) // self.chunk_ratio)

@lru_cache(4096)
self.get = lru_cache(4096)(self.get)

def get(self, entry):
"""Get a BAT entry."""
if entry + 1 > self.entry_count:
Expand Down
3 changes: 2 additions & 1 deletion dissect/hypervisor/disk/vmdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ def __init__(self, fh, parent=None, offset=0, sector_offset=0):
self.size = self.header.capacity * SECTOR_SIZE
self.sector_count = self.header.capacity

@lru_cache(128)
self._lookup_grain_table = lru_cache(128)(self._lookup_grain_table)

def _lookup_grain_table(self, directory):
gtbl_offset = self._grain_directory[directory]

Expand Down

0 comments on commit a3ea169

Please sign in to comment.