From 76983a1662abb6267b111264e9de6cb1e9f26554 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 10 May 2026 02:45:28 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Extract=20dynamic=20Regex?= =?UTF-8?q?=20to=20optimize=20HintExtractor=20performance?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 💡 What: Extracted `Regex("""\s+""")` to a private object-level constant `WHITESPACE_PATTERN` inside `HintExtractor.kt`. 🎯 Why: In Kotlin, dynamically defining `Regex` objects inside functions incurs a performance penalty since they get recompiled on every invocation. Hoisting it prevents this redundant overhead in what could be a hot path. 📊 Impact: Eliminates repetitive regex compilation on string formatting, reducing memory allocations and CPU overhead during hint extraction. 🔬 Measurement: Covered by the existing `HintExtractorTest.kt` test cases. Run `./gradlew :halogen-engine:test` to verify correctness. Co-authored-by: himattm <6266621+himattm@users.noreply.github.com> --- .jules/bolt.md | 3 +++ .../src/commonMain/kotlin/halogen/engine/HintExtractor.kt | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 .jules/bolt.md diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..4fa1b15 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2026-05-10 - Extract Dynamic Regex Declarations +**Learning:** In Kotlin, declaring `Regex` objects dynamically within functions incurs a performance penalty due to recompilation on every invocation. +**Action:** Always hoist frequently used regexes (e.g., `Regex("""\s+""")`) to class-level or object-level constants (e.g., `private val`) to optimize performance in hot paths. diff --git a/halogen-engine/src/commonMain/kotlin/halogen/engine/HintExtractor.kt b/halogen-engine/src/commonMain/kotlin/halogen/engine/HintExtractor.kt index c5cb8cb..da90f01 100644 --- a/halogen-engine/src/commonMain/kotlin/halogen/engine/HintExtractor.kt +++ b/halogen-engine/src/commonMain/kotlin/halogen/engine/HintExtractor.kt @@ -14,6 +14,7 @@ internal object HintExtractor { private val CAMEL_SPLIT = Regex("""(?<=[a-z])(?=[A-Z])""") private val ID_PATTERN = Regex("""^[0-9a-f]{8,}$""", RegexOption.IGNORE_CASE) private val NUMERIC_ONLY = Regex("""^\d+$""") + private val WHITESPACE_PATTERN = Regex("""\s+""") fun extract(key: String): String? { if (key.isBlank()) return null @@ -36,7 +37,7 @@ internal object HintExtractor { cleaned = cleaned.replace('_', ' ').replace('-', ' ') // Normalize whitespace - cleaned = cleaned.trim().replace(Regex("""\s+"""), " ") + cleaned = cleaned.trim().replace(WHITESPACE_PATTERN, " ") if (cleaned.isBlank()) return null