From 16208b3a7a27b0bf946c1bf710278149938ce7b5 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 28 Jul 2026 17:29:31 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Cache=20deterministic=20con?= =?UTF-8?q?text=20lexicons?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Added `functools.lru_cache` to `_compiled_context_lexicon` in `openmed.clinical.context`. - Prevents expensive regex compilation loops across span evaluations. - Created learning log in `.jules/bolt.md`. Co-authored-by: zrt219 <199104500+zrt219@users.noreply.github.com> --- .jules/bolt.md | 3 +++ openmed/openmed/clinical/context.py | 4 ++++ 2 files changed, 7 insertions(+) create mode 100644 .jules/bolt.md diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..c61fe04 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2024-06-25 - Context Lexicon Regex Compilation Bottleneck +**Learning:** Evaluated span assertions in the openmed NLP pipeline heavily depend on repeated deterministic cue compilations via `_compiled_context_lexicon`. This function constructs and formats numerous regexes for every evaluated axis (negation, temporality, certainty, etc). Doing this on every decision axis per text/span iteration caused massive redundant regex compilations and became a significant CPU bottleneck on the main thread. +**Action:** Always memoize (e.g. `functools.lru_cache`) deterministic regex lexicons and configuration functions where inputs are purely hashable parameters like `language` strings. diff --git a/openmed/openmed/clinical/context.py b/openmed/openmed/clinical/context.py index 9fd11df..9546489 100644 --- a/openmed/openmed/clinical/context.py +++ b/openmed/openmed/clinical/context.py @@ -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 ( @@ -154,6 +155,9 @@ class _CompiledContextLexicon: backward_context_cues: frozenset[str] +# ⚡ Bolt: Cache deterministic context lexicons to avoid compiling the same +# regex patterns thousands of times during span/text evaluations. +@lru_cache(maxsize=32) def _compiled_context_lexicon(language: str | None = None) -> _CompiledContextLexicon: lexicon = get_clinical_cue_lexicon(language) token_boundaries = lexicon.token_boundaries