diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..d8ccbb4 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2024-08-29 - [Cache deterministic regex compilations] +**Learning:** Deterministic regular expression compilation during text processing (e.g. NLP tasks) creates significant overhead when performed repeatedly, blocking the CPU. The `openmed.clinical.context` module builds heavy and complex alternated regular expressions out of cue lexicons over and over. +**Action:** When working on NLP pipelines, aggressively identify regex compilation and text span transformations. Memoize expensive object constructions (like `_compiled_context_lexicon`) using `functools.lru_cache` to drastically reduce processing time. diff --git a/openmed/openmed/clinical/context.py b/openmed/openmed/clinical/context.py index 9fd11df..03779af 100644 --- a/openmed/openmed/clinical/context.py +++ b/openmed/openmed/clinical/context.py @@ -40,6 +40,7 @@ from dataclasses import dataclass, replace from datetime import date from typing import Any, Literal +from functools import lru_cache from openmed.clinical.lexicons import ( ClinicalCueLexicon, @@ -154,6 +155,7 @@ class _CompiledContextLexicon: backward_context_cues: frozenset[str] +@lru_cache(maxsize=32) def _compiled_context_lexicon(language: str | None = None) -> _CompiledContextLexicon: lexicon = get_clinical_cue_lexicon(language) token_boundaries = lexicon.token_boundaries