From de75698354447f9975c8e5cb0e92f1b13ab94b96 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Fri, 28 Aug 2026 17:23:22 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Cache=20context=20lexicon?= =?UTF-8?q?=20compilation=20for=20performance?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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..3bd58a5 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2024-05-24 - Memoize deterministic regex compilations and lexicon generation in text-processing pipelines +**Learning:** In NLP or text-processing pipelines (e.g., within `openmed.clinical`), deterministic operations like regex compilations (`re.compile`) and lexicon generation are extremely expensive if called repeatedly per string evaluation. Without memoization, functions like `_compiled_context_lexicon` cause severe performance bottlenecks during span evaluations. +**Action:** Always memoize deterministic regex compilations and lexicon generation (e.g., using `@functools.lru_cache`) when they are repeatedly called per string or span evaluation, provided the return type is immutable (e.g., a frozen dataclass). 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