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-24 - Memoizing NLP Regex Compilation
**Learning:** NLP and clinical text evaluation repetitively instantiates the same compiled regexes and lexicons if missing memoization, leading to heavy redundant object compilation overhead.
**Action:** Always memoize deterministic lexicon object/regex instantiations (e.g. using @functools.lru_cache) and wrap return types in immutables to avoid performance bottlenecks in loops/text processing pipelines.
6 changes: 5 additions & 1 deletion openmed/openmed/clinical/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@

from __future__ import annotations

import functools
import re
import types
from collections.abc import Iterable, Iterator, Mapping, Sequence
from dataclasses import dataclass, replace
from datetime import date
Expand Down Expand Up @@ -154,7 +156,9 @@ class _CompiledContextLexicon:
backward_context_cues: frozenset[str]


@functools.lru_cache(maxsize=32)
def _compiled_context_lexicon(language: str | None = None) -> _CompiledContextLexicon:
"""Returns a memoized compiled lexicon to avoid repeated regex compilation."""
lexicon = get_clinical_cue_lexicon(language)
token_boundaries = lexicon.token_boundaries
return _CompiledContextLexicon(
Expand Down Expand Up @@ -195,7 +199,7 @@ def _compiled_context_lexicon(language: str | None = None) -> _CompiledContextLe
),
token_boundaries=token_boundaries,
),
category_by_text=_cue_category_lookup(lexicon),
category_by_text=types.MappingProxyType(_cue_category_lookup(lexicon)),
backward_context_cues=frozenset(
_normalize_cue_text(cue) for cue in lexicon.backward
),
Expand Down