Skip to content

Commit 6d8fc26

Browse files
committed
refactor: optimize loops in score set search filter options
1 parent f5a7578 commit 6d8fc26

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

src/mavedb/lib/score_sets.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -306,18 +306,33 @@ def fetch_score_set_search_filter_options(
306306

307307
query = db.query(ScoreSet)
308308
query = build_search_score_sets_query_filter(db, query, owner_or_contributor, search)
309-
310309
score_sets: list[ScoreSet] = query.all()
311310
if not score_sets:
312311
score_sets = []
313312

314-
score_sets = [score_set for score_set in score_sets if has_permission(requester, score_set, Action.READ).permitted]
315-
313+
# Target related counters
316314
target_category_counter: Counter[str] = Counter()
317315
target_name_counter: Counter[str] = Counter()
318316
target_organism_name_counter: Counter[str] = Counter()
319317
target_accession_counter: Counter[str] = Counter()
318+
# Publication related counters
319+
publication_author_name_counter: Counter[str] = Counter()
320+
publication_db_name_counter: Counter[str] = Counter()
321+
publication_journal_counter: Counter[str] = Counter()
322+
323+
# --- PERFORMANCE NOTE ---
324+
# The following counter construction loop is a bottleneck for large score set queries.
325+
# Practical future optimizations might include:
326+
# - Batch permission checks and attribute access outside the loop if possible
327+
# - Use parallelization (e.g., multiprocessing or concurrent.futures) for large datasets
328+
# - Pre-fetch or denormalize target/publication data in the DB query
329+
# - Profile and refactor nested attribute lookups to minimize Python overhead
320330
for score_set in score_sets:
331+
# Check read permission for each score set, skip if no permission
332+
if not has_permission(requester, score_set, Action.READ).permitted:
333+
continue
334+
335+
# Target related options
321336
for target in getattr(score_set, "target_genes", []):
322337
category = getattr(target, "category", None)
323338
if category:
@@ -340,10 +355,7 @@ def fetch_score_set_search_filter_options(
340355
if accession:
341356
target_accession_counter[accession] += 1
342357

343-
publication_author_name_counter: Counter[str] = Counter()
344-
publication_db_name_counter: Counter[str] = Counter()
345-
publication_journal_counter: Counter[str] = Counter()
346-
for score_set in score_sets:
358+
# Publication related options
347359
for publication_association in getattr(score_set, "publication_identifier_associations", []):
348360
publication = getattr(publication_association, "publication", None)
349361

0 commit comments

Comments
 (0)