diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..f987448 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2024-08-25 - [Memoizing Deterministic Regex Compilations] +**Learning:** NLP and text-processing pipelines, especially within `openmed.clinical`, heavily rely on compiling sets of strings into regex patterns (`_cue_pattern`). Without caching, iterating over sentences/documents triggers repetitive regex parsing, slowing down throughput dramatically. +**Action:** Always memoize deterministic regex compilations and lexicon generation (e.g., using `@functools.lru_cache`) with tuple parameters instead of lists to prevent severe performance bottlenecks during repeated string/span evaluations. diff --git a/openmed/openmed/clinical/context.py b/openmed/openmed/clinical/context.py index 9fd11df..e78f5ad 100644 --- a/openmed/openmed/clinical/context.py +++ b/openmed/openmed/clinical/context.py @@ -35,6 +35,7 @@ from __future__ import annotations +import functools import re from collections.abc import Iterable, Iterator, Mapping, Sequence from dataclasses import dataclass, replace @@ -154,6 +155,7 @@ class _CompiledContextLexicon: backward_context_cues: frozenset[str] +@functools.lru_cache def _compiled_context_lexicon(language: str | None = None) -> _CompiledContextLexicon: lexicon = get_clinical_cue_lexicon(language) token_boundaries = lexicon.token_boundaries diff --git a/openmed/openmed/clinical/status_vocab.py b/openmed/openmed/clinical/status_vocab.py index 3b87adb..6020ece 100644 --- a/openmed/openmed/clinical/status_vocab.py +++ b/openmed/openmed/clinical/status_vocab.py @@ -186,7 +186,7 @@ def _cue_matches(text: str, cue: object) -> bool: return _cue_pattern(normalized_cue).search(text) is not None -@lru_cache(maxsize=512) +@lru_cache(maxsize=1024) def _cue_pattern(cue: str) -> re.Pattern[str]: escaped = re.escape(cue).replace(r"\ ", r"\s+") prefix = r"(?