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-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.
2 changes: 2 additions & 0 deletions openmed/openmed/clinical/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down