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-03-14 - Regex Compilation Bottleneck in ConText Cue Matching
**Learning:** Instantiating complex regex pipelines for every NLP span processed (e.g., repeatedly calling unmemoized regex compilers during ConText modifier scanning) introduces severe latency overhead, inflating processing time by up to 40x.
**Action:** When adding or maintaining deterministic text-processing functions that compile regular expressions or lexicons based on simple inputs (like a language code), always apply `@functools.lru_cache` to memoize the compiled output.
3 changes: 3 additions & 0 deletions openmed/openmed/clinical/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
from collections.abc import Iterable, Iterator, Mapping, Sequence
from dataclasses import dataclass, replace
from datetime import date
from functools import lru_cache
from typing import Any, Literal

from openmed.clinical.lexicons import (
Expand Down Expand Up @@ -154,7 +155,9 @@ class _CompiledContextLexicon:
backward_context_cues: frozenset[str]


@lru_cache(maxsize=16)
def _compiled_context_lexicon(language: str | None = None) -> _CompiledContextLexicon:
"""Return compiled ConText regex lexicons, cached to prevent expensive re-compilation per span."""
lexicon = get_clinical_cue_lexicon(language)
token_boundaries = lexicon.token_boundaries
return _CompiledContextLexicon(
Expand Down