diff --git a/java/src/org/futo/inputmethod/latin/LatinIME.kt b/java/src/org/futo/inputmethod/latin/LatinIME.kt index fc60e5a97..b3166ca93 100644 --- a/java/src/org/futo/inputmethod/latin/LatinIME.kt +++ b/java/src/org/futo/inputmethod/latin/LatinIME.kt @@ -381,6 +381,7 @@ class LatinIME : InputMethodService(), LifecycleOwner, ViewModelStoreOwner, Save override fun onStartInput(attribute: EditorInfo?, restarting: Boolean) { super.onStartInput(attribute, restarting) latinIMELegacy.onStartInput(attribute, restarting) + languageModelFacilitator.onStartInput() } override fun onStartInputView(info: EditorInfo?, restarting: Boolean) { diff --git a/java/src/org/futo/inputmethod/latin/xlm/LanguageModelFacilitator.kt b/java/src/org/futo/inputmethod/latin/xlm/LanguageModelFacilitator.kt index 209b376bf..84361499e 100644 --- a/java/src/org/futo/inputmethod/latin/xlm/LanguageModelFacilitator.kt +++ b/java/src/org/futo/inputmethod/latin/xlm/LanguageModelFacilitator.kt @@ -133,6 +133,9 @@ public class LanguageModelFacilitator( public fun hasPendingUpdate(): Boolean = computationSemaphore.availablePermits == 0 + + private var numConsecutiveTimeouts = 0 + private var transformerDisabled = false public fun blockUntilComplete() { runBlocking { try { @@ -145,8 +148,14 @@ public class LanguageModelFacilitator( } } + numConsecutiveTimeouts = 0 } catch(e: TimeoutCancellationException) { Log.d("LanguageModelFacilitator", "Failed to complete prediction within 1000ms!") + numConsecutiveTimeouts += 1 + if(numConsecutiveTimeouts > 5) { + transformerDisabled = true + Log.w("LanguageModelFacilitator", "Temporarily disabling transformer due to continuous timeouts") + } } } } @@ -171,6 +180,8 @@ public class LanguageModelFacilitator( } private suspend fun runLanguageModel(values: PredictionInputValues): ArrayList? { + if(transformerDisabled) return null + val locale = dictionaryFacilitator.locale ?: return null if (languageModel == null || (languageModel?.locale?.language != locale.language)) { Log.d( @@ -602,4 +613,9 @@ public class LanguageModelFacilitator( assert(historyLog.isEmpty()) loadHistoryLogBackup(context, historyLog) } + + public fun onStartInput() { + transformerDisabled = false + numConsecutiveTimeouts = 0 + } } \ No newline at end of file