From 1f705364f398efc6f84ea0741f895ac8532404f3 Mon Sep 17 00:00:00 2001 From: LeanBitLab <245915690+LeanBitLab@users.noreply.github.com> Date: Sat, 10 Oct 2026 10:15:28 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Pre-compile=20punctuation?= =?UTF-8?q?=20Regex=20in=20VoiceInputManager?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Extract repeated `Regex("[,.?!;:]")` instantiation inside `syncRecognizedText` into a companion object constant `PUNCTUATION_REGEX` to reduce GC pressure on voice typing hot path. --- .jules/bolt.md | 3 +++ .../java/helium314/keyboard/latin/voice/VoiceInputManager.kt | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/.jules/bolt.md b/.jules/bolt.md index 4a3d4b649..c6c0104c8 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -15,3 +15,6 @@ ## 2024-05-18 - Pre-compile Regex in SpellCheckerSession **Learning:** `AndroidWordLevelSpellCheckerSession` previously instantiated new `Regex` objects for every quote matching and punctuation splitting operation during `onGetSuggestionsInternal`, which is a highly invoked method on the spellchecker hot-path. **Action:** Always extract frequently instantiated `Regex` objects from hot paths (like text replacement or splitting) into `companion object` private constants (e.g. `periodRegex`, `quotesStartRegex`) to reduce allocation and regex compilation overhead per keystroke. +## 2024-05-18 - Pre-compile Regex in VoiceInputManager +**Learning:** `VoiceInputManager.syncRecognizedText` instantiates `Regex("[,.?!;:]")` inside a frequent method call, increasing allocation pressure on voice typing hot paths. +**Action:** Extract repeated `Regex` instantiations in input hot paths (such as `syncRecognizedText`) into companion object private constants to reduce memory allocation and compilation overhead. diff --git a/app/src/main/java/helium314/keyboard/latin/voice/VoiceInputManager.kt b/app/src/main/java/helium314/keyboard/latin/voice/VoiceInputManager.kt index f42853ae2..7322b164a 100644 --- a/app/src/main/java/helium314/keyboard/latin/voice/VoiceInputManager.kt +++ b/app/src/main/java/helium314/keyboard/latin/voice/VoiceInputManager.kt @@ -885,7 +885,7 @@ class VoiceInputManager( val isSmartPunctuationEnabled = ims.prefs().getBoolean(VoiceConstants.PREF_VOICE_SMART_PUNCTUATION, true) val processedRaw = if (!isSmartPunctuationEnabled) { - rawText.replace(Regex("[,.?!;:]"), "") + rawText.replace(PUNCTUATION_REGEX, "") } else { rawText } @@ -1058,6 +1058,7 @@ class VoiceInputManager( companion object { private const val TAG = "VoiceInputManager" + private val PUNCTUATION_REGEX = Regex("[,.?!;:]") private const val SAMPLE_RATE = 16000 private const val FRAME_SIZE_MS = 30 private const val FRAME_SIZE_SHORTS = SAMPLE_RATE * FRAME_SIZE_MS / 1000 // 480 shorts