From 5ff197bd542333b65a1de580dfe22ab337b639dc Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 22 Jul 2026 17:27:31 +0000 Subject: [PATCH] perf: add lru_cache to context lexicon generation 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..e142e69 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2024-07-22 - [Missing memoization on regex compilation] +**Learning:** Functions that generate compiled regex patterns on the fly (e.g., `_compiled_context_lexicon` in `openmed.clinical.context`) create severe performance bottlenecks when used repeatedly (e.g., during per-span text evaluations). Without `functools.lru_cache`, the regex compiler is continuously invoked, leading to a massive CPU overhead. +**Action:** When working on text processing pipelines in openmed, always ensure deterministic regex compilation and lexicon generation steps are memoized (e.g., using `@functools.lru_cache`) to prevent unnecessary computation on repeated text segment evaluations. diff --git a/openmed/openmed/clinical/context.py b/openmed/openmed/clinical/context.py index 9fd11df..d08693d 100644 --- a/openmed/openmed/clinical/context.py +++ b/openmed/openmed/clinical/context.py @@ -36,6 +36,7 @@ from __future__ import annotations import re +import functools from collections.abc import Iterable, Iterator, Mapping, Sequence from dataclasses import dataclass, replace from datetime import date @@ -154,6 +155,7 @@ class _CompiledContextLexicon: backward_context_cues: frozenset[str] +@functools.lru_cache(maxsize=16) def _compiled_context_lexicon(language: str | None = None) -> _CompiledContextLexicon: lexicon = get_clinical_cue_lexicon(language) token_boundaries = lexicon.token_boundaries