Skip to content

Commit fa251df

Browse files
committed
more aggressive caching for speed up
- cache `is_mac` and `smart_sort_comparator`
1 parent 17462ad commit fa251df

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

pyard/ard.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import sys
2727
from typing import Iterable, List
2828

29-
from . import broad_splits
29+
from . import broad_splits, smart_sort
3030
from . import data_repository as dr
3131
from . import db
3232
from .exceptions import InvalidAlleleError, InvalidMACError, InvalidTypingError
@@ -41,7 +41,6 @@
4141
expression_chars,
4242
DEFAULT_CACHE_SIZE,
4343
)
44-
from .smart_sort import smart_sort_comparator
4544

4645
default_config = {
4746
"reduce_serology": True,
@@ -128,6 +127,12 @@ def __init__(
128127
self._redux_allele
129128
)
130129
self.redux = functools.lru_cache(maxsize=max_cache_size)(self.redux)
130+
self.is_mac = functools.lru_cache(maxsize=max_cache_size)(self.is_mac)
131+
self.smart_sort_comparator = functools.lru_cache(maxsize=max_cache_size)(
132+
smart_sort.smart_sort_comparator
133+
)
134+
else:
135+
self.smart_sort_comparator = smart_sort.smart_sort_comparator
131136

132137
# reference data is read-only and can be frozen
133138
# Works only for Python >= 3.9
@@ -256,8 +261,7 @@ def _redux_allele(
256261
else:
257262
raise InvalidAlleleError(f"{allele} is an invalid allele.")
258263

259-
@staticmethod
260-
def _sorted_unique_gl(gls: List[str], delim: str) -> str:
264+
def _sorted_unique_gl(self, gls: List[str], delim: str) -> str:
261265
"""
262266
Make a list of sorted unique GL Strings separated by delim.
263267
As the list may itself contains elements that are separated by the
@@ -274,7 +278,7 @@ def _sorted_unique_gl(gls: List[str], delim: str) -> str:
274278
if delim == "+":
275279
# No need to make unique. eg. homozygous cases are valid for SLUGs
276280
return delim.join(
277-
sorted(gls, key=functools.cmp_to_key(smart_sort_comparator))
281+
sorted(gls, key=functools.cmp_to_key(self.smart_sort_comparator))
278282
)
279283

280284
# generate a unique list over a delimiter
@@ -284,7 +288,7 @@ def _sorted_unique_gl(gls: List[str], delim: str) -> str:
284288
all_gls += gl.split(delim)
285289
unique_gls = set(all_gls)
286290
return delim.join(
287-
sorted(unique_gls, key=functools.cmp_to_key(smart_sort_comparator))
291+
sorted(unique_gls, key=functools.cmp_to_key(self.smart_sort_comparator))
288292
)
289293

290294
@functools.lru_cache(maxsize=DEFAULT_CACHE_SIZE)
@@ -427,6 +431,7 @@ def is_serology(self, allele: str) -> bool:
427431

428432
return db.is_valid_serology(self.db_connection, allele)
429433

434+
@functools.lru_cache(maxsize=DEFAULT_CACHE_SIZE)
430435
def is_mac(self, allele: str) -> bool:
431436
"""
432437
MAC has non-digit characters after the : character.

pyard/smart_sort.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@
2424
import functools
2525
import re
2626

27+
from pyard import constants
28+
2729
expr_regex = re.compile("[PNQLSGg]")
2830
glstring_chars = re.compile("[/|+^~]")
2931

3032

31-
@functools.lru_cache(maxsize=1000)
33+
@functools.lru_cache(maxsize=constants.DEFAULT_CACHE_SIZE)
3234
def smart_sort_comparator(a1, a2):
3335
"""
3436
Natural sort 2 given alleles.

0 commit comments

Comments
 (0)