From 0002ed63eb8bd42d2106b71450eec2ee17f56e2e Mon Sep 17 00:00:00 2001 From: Helium314 Date: Mon, 20 Nov 2023 01:11:06 +0100 Subject: [PATCH] enable validSpellingWordCache for spell checker --- .../latin/DictionaryFacilitatorImpl.java | 25 ++++++++++--------- .../latin/DictionaryFacilitatorProvider.java | 7 +++++- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/org/dslul/openboard/inputmethod/latin/DictionaryFacilitatorImpl.java b/app/src/main/java/org/dslul/openboard/inputmethod/latin/DictionaryFacilitatorImpl.java index cd40c1d90..0538fff75 100644 --- a/app/src/main/java/org/dslul/openboard/inputmethod/latin/DictionaryFacilitatorImpl.java +++ b/app/src/main/java/org/dslul/openboard/inputmethod/latin/DictionaryFacilitatorImpl.java @@ -88,15 +88,8 @@ public class DictionaryFacilitatorImpl implements DictionaryFacilitator { private static final Class[] DICT_FACTORY_METHOD_ARG_TYPES = new Class[] { Context.class, Locale.class, File.class, String.class, String.class }; - // todo: these caches are never even set, as the corresponding functions are not called... - // and even if they were set, one is only written, but never read, and the other one - // is only read and thus empty and useless -> why? - // anyway, we could just set the same cache using the set functions - // but before doing this, check the potential performance gains - // i.e. how long does a "isValidWord" check take -> on S4 mini 300 µs per dict if ok, but - // sometimes it can also be a few ms - // os if the spell checker is enabled, it's definitely reasonable to cache the results - // but this needs to be done internally, as it should be by language + // todo: write cache never set, and never read (only written) + // (initially was the same for the read cache, why?) private LruCache mValidSpellingWordReadCache; private LruCache mValidSpellingWordWriteCache; @@ -461,6 +454,9 @@ public class DictionaryFacilitatorImpl implements DictionaryFacilitator { if (mValidSpellingWordWriteCache != null) { mValidSpellingWordWriteCache.evictAll(); } + if (mValidSpellingWordReadCache != null) { + mValidSpellingWordReadCache.evictAll(); + } } private void asyncReloadUninitializedMainDictionaries(final Context context, @@ -912,11 +908,16 @@ public class DictionaryFacilitatorImpl implements DictionaryFacilitator { return cachedValue; } } + boolean result = false; for (DictionaryGroup dictionaryGroup : mDictionaryGroups) { - if (isValidWord(word, ALL_DICTIONARY_TYPES, dictionaryGroup)) - return true; + if (isValidWord(word, ALL_DICTIONARY_TYPES, dictionaryGroup)) { + result = true; + break; + } } - return false; + if (mValidSpellingWordReadCache != null) + mValidSpellingWordReadCache.put(word, result); + return result; } // this is unused, so leave it for now (redirecting to isValidWord seems to defeat the purpose...) diff --git a/app/src/main/java/org/dslul/openboard/inputmethod/latin/DictionaryFacilitatorProvider.java b/app/src/main/java/org/dslul/openboard/inputmethod/latin/DictionaryFacilitatorProvider.java index 718bacaee..a5086ddcb 100644 --- a/app/src/main/java/org/dslul/openboard/inputmethod/latin/DictionaryFacilitatorProvider.java +++ b/app/src/main/java/org/dslul/openboard/inputmethod/latin/DictionaryFacilitatorProvider.java @@ -6,11 +6,16 @@ package org.dslul.openboard.inputmethod.latin; +import android.util.LruCache; + /** * Factory for instantiating DictionaryFacilitator objects. */ public class DictionaryFacilitatorProvider { public static DictionaryFacilitator getDictionaryFacilitator(boolean isNeededForSpellChecking) { - return new DictionaryFacilitatorImpl(); + final DictionaryFacilitator facilitator = new DictionaryFacilitatorImpl(); + if (isNeededForSpellChecking) + facilitator.setValidSpellingWordReadCache(new LruCache<>(200)); + return facilitator; } }