From 498aa959e07a347ae7f563aca494236753d8d772 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 25 Aug 2026 17:36:57 +0000 Subject: [PATCH] Performance improvement by caching deterministic regex compilations and lexicons with `@functools.lru_cache` in the `clinical` module. Co-authored-by: zrt219 <199104500+zrt219@users.noreply.github.com> --- .jules/bolt.md | 3 +++ openmed/openmed/clinical/context.py | 2 ++ openmed/openmed/clinical/status_vocab.py | 2 +- 3 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 .jules/bolt.md diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..f987448 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2024-08-25 - [Memoizing Deterministic Regex Compilations] +**Learning:** NLP and text-processing pipelines, especially within `openmed.clinical`, heavily rely on compiling sets of strings into regex patterns (`_cue_pattern`). Without caching, iterating over sentences/documents triggers repetitive regex parsing, slowing down throughput dramatically. +**Action:** Always memoize deterministic regex compilations and lexicon generation (e.g., using `@functools.lru_cache`) with tuple parameters instead of lists to prevent severe performance bottlenecks during repeated string/span evaluations. diff --git a/openmed/openmed/clinical/context.py b/openmed/openmed/clinical/context.py index 9fd11df..e78f5ad 100644 --- a/openmed/openmed/clinical/context.py +++ b/openmed/openmed/clinical/context.py @@ -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 @@ -154,6 +155,7 @@ class _CompiledContextLexicon: backward_context_cues: frozenset[str] +@functools.lru_cache def _compiled_context_lexicon(language: str | None = None) -> _CompiledContextLexicon: lexicon = get_clinical_cue_lexicon(language) token_boundaries = lexicon.token_boundaries diff --git a/openmed/openmed/clinical/status_vocab.py b/openmed/openmed/clinical/status_vocab.py index 3b87adb..6020ece 100644 --- a/openmed/openmed/clinical/status_vocab.py +++ b/openmed/openmed/clinical/status_vocab.py @@ -186,7 +186,7 @@ def _cue_matches(text: str, cue: object) -> bool: return _cue_pattern(normalized_cue).search(text) is not None -@lru_cache(maxsize=512) +@lru_cache(maxsize=1024) def _cue_pattern(cue: str) -> re.Pattern[str]: escaped = re.escape(cue).replace(r"\ ", r"\s+") prefix = r"(?