Skip to content

Commit 9740cce

Browse files
jrlouis21Copilot
andcommitted
remove mmap
Co-authored-by: Copilot <copilot@github.com>
1 parent 24e1836 commit 9740cce

1 file changed

Lines changed: 6 additions & 15 deletions

File tree

rounds/3_dna/solution.py

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

88
from __future__ import annotations
99

10-
import mmap
1110
import os
1211
from concurrent.futures import ThreadPoolExecutor
1312

@@ -17,24 +16,20 @@
1716

1817

1918
def _search_chunk(
20-
data: bytes | mmap.mmap,
19+
data: bytes,
2120
pattern: bytes,
2221
records: list[tuple[int, int]],
2322
) -> list[tuple[str, list[int]]]:
2423
"""Process a batch of (header_start, next_record_start) pairs."""
2524
results: list[tuple[str, list[int]]] = []
2625
for rec_start, rec_end in records:
2726
nl = data.index(b"\n", rec_start)
28-
raw = data[nl + 1 : rec_end]
27+
seq = data[nl + 1 : rec_end].translate(_DELETE_TABLE, _DELETE_CHARS)
2928

30-
seq = raw.translate(_DELETE_TABLE, _DELETE_CHARS)
31-
32-
# Quick check: if the pattern isn't in the cleaned sequence, skip.
3329
if pattern not in seq:
3430
continue
3531

3632
record_id = data[rec_start + 1 : nl].strip().decode("ascii")
37-
seq = raw.translate(_DELETE_TABLE, _DELETE_CHARS)
3833

3934
positions: list[int] = []
4035
start = 0
@@ -57,22 +52,18 @@ def find_matches(fasta_path: str, pattern: bytes) -> list[tuple[str, list[int]]]
5752
Returns ``[(record_id, [positions...]), ...]`` in file order.
5853
"""
5954
with open(fasta_path, "rb") as f:
60-
mm = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
55+
data = f.read()
6156

6257
boundaries: list[tuple[int, int]] = []
63-
pos = mm.find(b">")
58+
pos = data.find(b">")
6459
while pos != -1:
65-
nxt = mm.find(b">", pos + 1)
66-
boundaries.append((pos, nxt if nxt != -1 else mm.size()))
60+
nxt = data.find(b">", pos + 1)
61+
boundaries.append((pos, nxt if nxt != -1 else len(data)))
6762
pos = nxt
6863

6964
if not boundaries:
70-
mm.close()
7165
return []
7266

73-
data = mm[:]
74-
mm.close()
75-
7667
n = len(boundaries)
7768
chunk_size = max(1, n // _NUM_WORKERS)
7869
chunks = [boundaries[i : i + chunk_size] for i in range(0, n, chunk_size)]

0 commit comments

Comments
 (0)