Skip to content

Commit

Permalink
Move lru_cache definitions to __init__ (#18)
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 ba8de52 commit 8d53824
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions dissect/sql/sqlite3.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ def __init__(self, fh, wal_fh=None):
if self.usable_page_size < 480:
raise InvalidDatabase("Usable page size is too small")

self.page = lru_cache(256)(self.page)

def open_wal(self, fh):
self.wal = WAL(fh)

Expand Down Expand Up @@ -81,7 +83,6 @@ def raw_page(self, num):
self.fh.seek((num - 1) * self.page_size)
return self.fh.read(self.header.page_size)

@lru_cache(maxsize=256)
def page(self, num):
return Page(self, num)

Expand Down Expand Up @@ -298,14 +299,15 @@ def __init__(self, sqlite, num):

self.cell_pointers = c_sqlite3.uint16[self.header.cell_count](buf[fp : fp + (self.header.cell_count * 2)])

self.cell = lru_cache(256)(self.cell)

def __repr__(self):
page_type = PAGE_TYPES[self.header.flags]
return f"<Page num={self.num} type={page_type} offset=0x{self.offset:x}>"

def open(self):
return BytesIO(self.data)

@lru_cache(maxsize=256)
def cell(self, num):
if num >= self.header.cell_count or num < 0:
raise IndexError("Invalid cell number")
Expand Down Expand Up @@ -443,7 +445,8 @@ def __init__(self, fh):
self.checksum_endian = "<" if self.header.magic == WAL_HEADER_MAGIC_LE else ">"
self._checkpoints = None

@lru_cache(maxsize=1024)
self.frame = lru_cache(1024)(self.frame)

def frame(self, frame_idx):
frame_size = len(c_sqlite3.wal_frame) + self.header.page_size
offset = len(c_sqlite3.wal_header) + frame_idx * frame_size
Expand Down

0 comments on commit 8d53824

Please sign in to comment.