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 54d8c84 commit f760731
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
3 changes: 2 additions & 1 deletion dissect/esedb/esedb.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ def __init__(self, fh: BinaryIO, impacket_compat: bool = False):

self.catalog = Catalog(self, pgnoFDPMSO)

self.page = lru_cache(4096)(self.page)

@cached_property
def has_small_pages(self) -> bool:
"""Return whether this database has small pages (<= 8K)."""
Expand Down Expand Up @@ -82,7 +84,6 @@ def read_page(self, num: int) -> bytes:

return buf

@lru_cache(maxsize=4096)
def page(self, num: int) -> Page:
"""Get a logical page.
Expand Down
5 changes: 3 additions & 2 deletions dissect/esedb/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ def __init__(self, table: Table, node: Node):
self._tagged_data_view = xmemoryview(tagged_field_data, "<I")
self._tagged_fields[first_tagged_field.identifier] = first_tagged_field

self._get_tag_field = lru_cache(4096)(self._get_tag_field)
self._find_tag_field_idx = lru_cache(4096)(self._find_tag_field_idx)

def get(self, column: Column, raw: bool = False) -> RecordValue:
"""Retrieve the value for the specified column.
Expand Down Expand Up @@ -355,12 +358,10 @@ def _get_tagged(self, column: Column) -> Optional[bytes]:

return tag_field, value

@lru_cache(4096)
def _get_tag_field(self, idx: int) -> TagField:
"""Retrieve the :class:`TagField` at the given index in the ``TAGFLD`` array."""
return TagField(self, self._tagged_data_view[idx])

@lru_cache(4096)
def _find_tag_field_idx(self, identifier: int, is_derived: bool = False) -> Optional[TagField]:
"""Find a tag field by identifier and optional derived flag.
Expand Down

0 comments on commit f760731

Please sign in to comment.