Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@
## 2024-05-18 - Pre-compile Regex in model importers
**Learning:** `TranslationModelImporter` and `HandwritingModelImporter` create `Regex` instances inside `detectLanguageCode` and `detectLanguageTag` methods, sometimes inside a loop parsing zip entries. This causes unnecessary overhead on every detection call.
**Action:** Extract frequently used `Regex` instances into `companion object`s as `private val`s to ensure they are compiled only once, reducing execution time and object allocation.
## 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.
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,12 @@ abstract class AndroidWordLevelSpellCheckerSession(
val locale = mLocale ?: return AndroidSpellCheckerService.getNotInDictEmptySuggestions(false)
var text = textInfo.text
.replace(AndroidSpellCheckerService.APOSTROPHE, AndroidSpellCheckerService.SINGLE_QUOTE)
.replace(Regex("^$quotesRegexp"), "")
.replace(Regex("$quotesRegexp$"), "")
.replace(quotesStartRegex, "")
.replace(quotesEndRegex, "")

val localeRegex = scriptToPunctuationRegexMap[locale.script()]
if (localeRegex != null) {
text = text.replace(Regex(localeRegex), "")
text = text.replace(localeRegex, "")
}

val cachedSuggestionsParams = mSuggestionsCache.getSuggestionsFromCache(text)
Expand All @@ -148,7 +148,7 @@ abstract class AndroidWordLevelSpellCheckerSession(
if (CHECKABILITY_CHECKABLE != checkability) {
val periodOnlyAtLastIndex = text.indexOf(Constants.CODE_PERIOD.toChar()) == (text.length - 1)
if (CHECKABILITY_CONTAINS_PERIOD == checkability) {
val splitText = text.split(Regex(Constants.REGEXP_PERIOD))
val splitText = text.split(periodRegex)
var allWordsAreValid = true
for (word in splitText) {
if (word.isNotEmpty() && !mService.isValidWord(locale, word) && !mService.isValidWord(locale, word.lowercase(locale))) {
Expand Down Expand Up @@ -218,8 +218,11 @@ abstract class AndroidWordLevelSpellCheckerSession(
val EMPTY_STRING_ARRAY = emptyArray<String>()

private const val quotesRegexp = "([\u0022\u0027\u0060\u00B4\u2018\u2019\u201C\u201D])"
private val scriptToPunctuationRegexMap = TreeMap<String, String>().apply {
put(ScriptUtils.SCRIPT_ARMENIAN, "(\u0028|\u0029|\u0027|\u2026|\u055E|\u055C|\u055B|\u055D|\u058A|\u2015|\u00AB|\u00BB|\u002C|\u0589|\u2024)")
private val quotesStartRegex = Regex("^$quotesRegexp")
private val quotesEndRegex = Regex("$quotesRegexp$")
private val periodRegex = Regex(Constants.REGEXP_PERIOD)
private val scriptToPunctuationRegexMap = TreeMap<String, Regex>().apply {
put(ScriptUtils.SCRIPT_ARMENIAN, Regex("(\u0028|\u0029|\u0027|\u2026|\u055E|\u055C|\u055B|\u055D|\u058A|\u2015|\u00AB|\u00BB|\u002C|\u0589|\u2024)"))
}

private const val CHECKABILITY_CHECKABLE = 0
Expand Down
Loading