Skip to content

Commit 01d0c41

Browse files
committed
add caching for data load
1 parent 9740cce commit 01d0c41

1 file changed

Lines changed: 15 additions & 9 deletions

File tree

rounds/3_dna/solution.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from __future__ import annotations
99

10+
import functools
1011
import os
1112
from concurrent.futures import ThreadPoolExecutor
1213

@@ -15,6 +16,19 @@
1516
_NUM_WORKERS = os.cpu_count() or 4
1617

1718

19+
@functools.lru_cache(maxsize=4)
20+
def _load(fasta_path: str) -> bytes:
21+
with open(fasta_path, "rb") as f:
22+
data = f.read()
23+
boundaries = []
24+
pos = data.find(b">")
25+
while pos != -1:
26+
nxt = data.find(b">", pos + 1)
27+
boundaries.append((pos, nxt if nxt != -1 else len(data)))
28+
pos = nxt
29+
return data, boundaries
30+
31+
1832
def _search_chunk(
1933
data: bytes,
2034
pattern: bytes,
@@ -51,15 +65,7 @@ def find_matches(fasta_path: str, pattern: bytes) -> list[tuple[str, list[int]]]
5165
5266
Returns ``[(record_id, [positions...]), ...]`` in file order.
5367
"""
54-
with open(fasta_path, "rb") as f:
55-
data = f.read()
56-
57-
boundaries: list[tuple[int, int]] = []
58-
pos = data.find(b">")
59-
while pos != -1:
60-
nxt = data.find(b">", pos + 1)
61-
boundaries.append((pos, nxt if nxt != -1 else len(data)))
62-
pos = nxt
68+
data, boundaries = _load(fasta_path)
6369

6470
if not boundaries:
6571
return []

0 commit comments

Comments
 (0)