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