Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 2024-05-18 - Memoizing Regex Compilation in Deterministic Clinical NLP Layers
**Learning:** In openmed, text processing pipelines are highly constrained to be deterministic and offline. However, regex compilation and pattern generation using `get_clinical_cue_lexicon` inside functions like `_compiled_context_lexicon` created severe performance bottlenecks when invoked repeatedly (e.g., millions of times over patient records), turning a sub-second task into a nearly minute-long CPU loop per bulk record. Memory reminds us that deterministic string evaluations must memoize regex compilation using `@functools.lru_cache`.
**Action:** When implementing or fixing deterministic parsing or context engines inside `openmed.clinical` or similar pipeline components, apply `@functools.lru_cache(maxsize=None)` to methods building dictionaries or regex patterns from finite sets of lexical rules (e.g., lexicons by language). This makes downstream runtime evaluation near-instantaneous for subsequent hits.
2 changes: 2 additions & 0 deletions openmed/openmed/clinical/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -154,6 +155,7 @@ class _CompiledContextLexicon:
backward_context_cues: frozenset[str]


@functools.lru_cache(maxsize=None)
def _compiled_context_lexicon(language: str | None = None) -> _CompiledContextLexicon:
lexicon = get_clinical_cue_lexicon(language)
token_boundaries = lexicon.token_boundaries
Expand Down