From da76296cc4a4bc49bb50f78977d10baed3b00bd0 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sat, 29 Aug 2026 17:33:11 +0000 Subject: [PATCH] Perf: Cache compilation of clinical context lexicons\n\nWrap `_compiled_context_lexicon` with `@lru_cache(maxsize=32)` to\nmemoize the heavy and deterministic creation and combination of multiple\nalternated regular expressions. Eliminates repetitive compilation overhead\nthat occurred across every text scan operation. Co-authored-by: zrt219 <199104500+zrt219@users.noreply.github.com> --- .jules/bolt.md | 3 +++ openmed/openmed/clinical/context.py | 2 ++ 2 files changed, 5 insertions(+) create mode 100644 .jules/bolt.md diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..d8ccbb4 --- /dev/null +++ b/.jules/bolt.md @@ -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. diff --git a/openmed/openmed/clinical/context.py b/openmed/openmed/clinical/context.py index 9fd11df..03779af 100644 --- a/openmed/openmed/clinical/context.py +++ b/openmed/openmed/clinical/context.py @@ -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, @@ -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