From ba8ad726e002a3f5d00af37eec154e5c83ad8ffc Mon Sep 17 00:00:00 2001 From: seonghobae <8172694+seonghobae@users.noreply.github.com> Date: Sat, 18 Jul 2026 17:12:17 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20[=ED=85=8D=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=ED=86=A0=ED=81=B0=ED=99=94=20=EC=A0=95=EA=B7=9C?= =?UTF-8?q?=EC=8B=9D=20=EC=B5=9C=EC=A0=81=ED=99=94]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Added fast-path `isalnum()` checks to string tokenization paths in `summarize.py` and `transcript_search.py` to bypass expensive regular expressions for purely alphanumeric inputs. --- .jules/bolt.md | 3 +++ summarize.py | 5 ++++- transcript_search.py | 3 +++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/.jules/bolt.md b/.jules/bolt.md index 341c7c9..457d6b0 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -67,3 +67,6 @@ ## 2025-02-12 - [Fast Path Execution in Directory Traversal and Log Parsing] **Learning:** Checking for string existence (`if "silence_" not in stderr`) before invoking regex matchers provides significant speed improvements when parsing large blocks of text. Similarly, moving expensive I/O operations like `os.path.realpath` inside conditional blocks prevents redundant disk access when configuration (like path exclusions) isn't utilized. **Action:** When working on large text processing or disk operations, verify if early exit conditions or conditional execution can bypass the expensive system or library calls. +## 2024-07-18 - [Optimize tokenization and word stripping with fast path string checks] +**Learning:** Regex execution in Python, even when compiled, is slower than simple string methods. When parsing or tokenizing text to strip non-word characters, prepend a fast-path check using `str.isalnum()` to bypass the regex overhead for purely alphanumeric words, yielding significant performance gains. +**Action:** When tokenizing or processing text using regular expressions to strip non-word characters, prepend a fast-path check using `str.isalnum()` (or similar) to bypass the regex overhead for purely alphanumeric words. diff --git a/summarize.py b/summarize.py index a5b56fe..ad383d9 100644 --- a/summarize.py +++ b/summarize.py @@ -94,7 +94,10 @@ def _content_words(sentence): """ words = [] for raw in sentence.split(): - token = _TOKEN_STRIP_RE.sub("", raw).lower() + if raw.isalnum(): + token = raw.lower() + else: + token = _TOKEN_STRIP_RE.sub("", raw).lower() if token and token not in _STOPWORDS: words.append(token) return words diff --git a/transcript_search.py b/transcript_search.py index 5bb2fac..e25537a 100644 --- a/transcript_search.py +++ b/transcript_search.py @@ -67,6 +67,9 @@ def tokenize(text: str) -> list[str]: Returns: List of lowercase tokens (possibly empty). """ + # Fast path: skip regex if text is purely alphanumeric and spaces + if text.replace(" ", "").isalnum(): + return text.lower().split() return _WORD_RE.findall(text.lower())