am b118a9d7: Merge "Change cache capacity depending on the dictionary size."

* commit 'b118a9d729b8ea58063a39396f60c7c5aa1c544b':
  Change cache capacity depending on the dictionary size.
main
Keisuke Kuroyanagi 2013-09-12 09:15:24 -07:00 committed by Android Git Automerger
commit 0d86a015be
8 changed files with 48 additions and 21 deletions

View File

@ -43,6 +43,7 @@ public final class BinaryDictionary extends Dictionary {
private long mNativeDict;
private final Locale mLocale;
private final long mDictSize;
private final int[] mInputCodePoints = new int[MAX_WORD_LENGTH];
private final int[] mOutputCodePoints = new int[MAX_WORD_LENGTH * MAX_RESULTS];
private final int[] mSpaceIndices = new int[MAX_RESULTS];
@ -62,7 +63,7 @@ public final class BinaryDictionary extends Dictionary {
if (traverseSession == null) {
traverseSession = mDicTraverseSessions.get(traverseSessionId);
if (traverseSession == null) {
traverseSession = new DicTraverseSession(mLocale, mNativeDict);
traverseSession = new DicTraverseSession(mLocale, mNativeDict, mDictSize);
mDicTraverseSessions.put(traverseSessionId, traverseSession);
}
}
@ -85,6 +86,7 @@ public final class BinaryDictionary extends Dictionary {
final boolean isUpdatable) {
super(dictType);
mLocale = locale;
mDictSize = length;
mNativeSuggestOptions.setUseFullEditDistance(useFullEditDistance);
loadDictionary(filename, offset, length, isUpdatable);
}

View File

@ -25,16 +25,16 @@ public final class DicTraverseSession {
JniUtils.loadNativeLibrary();
}
private static native long setDicTraverseSessionNative(String locale);
private static native long setDicTraverseSessionNative(String locale, long dictSize);
private static native void initDicTraverseSessionNative(long nativeDicTraverseSession,
long dictionary, int[] previousWord, int previousWordLength);
private static native void releaseDicTraverseSessionNative(long nativeDicTraverseSession);
private long mNativeDicTraverseSession;
public DicTraverseSession(Locale locale, long dictionary) {
public DicTraverseSession(Locale locale, long dictionary, long dictSize) {
mNativeDicTraverseSession = createNativeDicTraverseSession(
locale != null ? locale.toString() : "");
locale != null ? locale.toString() : "", dictSize);
initSession(dictionary);
}
@ -51,8 +51,8 @@ public final class DicTraverseSession {
mNativeDicTraverseSession, dictionary, previousWord, previousWordLength);
}
private final long createNativeDicTraverseSession(String locale) {
return setDicTraverseSessionNative(locale);
private final long createNativeDicTraverseSession(String locale, long dictSize) {
return setDicTraverseSessionNative(locale, dictSize);
}
private void closeInternal() {

View File

@ -25,8 +25,9 @@
namespace latinime {
class Dictionary;
static jlong latinime_setDicTraverseSession(JNIEnv *env, jclass clazz, jstring localeJStr) {
void *traverseSession = DicTraverseSession::getSessionInstance(env, localeJStr);
static jlong latinime_setDicTraverseSession(JNIEnv *env, jclass clazz, jstring localeJStr,
jlong dictSize) {
void *traverseSession = DicTraverseSession::getSessionInstance(env, localeJStr, dictSize);
return reinterpret_cast<jlong>(traverseSession);
}
@ -53,7 +54,7 @@ static void latinime_releaseDicTraverseSession(JNIEnv *env, jclass clazz, jlong
static const JNINativeMethod sMethods[] = {
{
const_cast<char *>("setDicTraverseSessionNative"),
const_cast<char *>("(Ljava/lang/String;)J"),
const_cast<char *>("(Ljava/lang/String;J)J"),
reinterpret_cast<void *>(latinime_setDicTraverseSession)
},
{

View File

@ -32,8 +32,6 @@
#define MAX_WORD_LENGTH 48
// Must be equal to BinaryDictionary.MAX_RESULTS in Java
#define MAX_RESULTS 18
// The biggest value among MAX_CACHE_DIC_NODE_SIZE, MAX_CACHE_DIC_NODE_SIZE_FOR_SINGLE_POINT, ...
#define MAX_DIC_NODE_PRIORITY_QUEUE_CAPACITY 310
// Must be equal to ProximityInfo.MAX_PROXIMITY_CHARS_SIZE in Java
#define MAX_PROXIMITY_CHARS_SIZE 16
#define ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE 2

View File

@ -23,6 +23,11 @@
namespace latinime {
// The biggest value among MAX_CACHE_DIC_NODE_SIZE, MAX_CACHE_DIC_NODE_SIZE_FOR_SINGLE_POINT, ...
const int DicNodesCache::LARGE_PRIORITY_QUEUE_CAPACITY = 310;
// Capacity for reducing memory footprint.
const int DicNodesCache::SMALL_PRIORITY_QUEUE_CAPACITY = 100;
/**
* Truncates all of the dicNodes so that they start at the given commit point.
* Only called for multi-word typing input.

View File

@ -31,10 +31,11 @@ class DicNode;
*/
class DicNodesCache {
public:
AK_FORCE_INLINE DicNodesCache()
: mDicNodePriorityQueue0(MAX_DIC_NODE_PRIORITY_QUEUE_CAPACITY),
mDicNodePriorityQueue1(MAX_DIC_NODE_PRIORITY_QUEUE_CAPACITY),
mDicNodePriorityQueue2(MAX_DIC_NODE_PRIORITY_QUEUE_CAPACITY),
AK_FORCE_INLINE explicit DicNodesCache(const bool usesLargeCapacityCache)
: mUsesLargeCapacityCache(usesLargeCapacityCache),
mDicNodePriorityQueue0(getCacheCapacity()),
mDicNodePriorityQueue1(getCacheCapacity()),
mDicNodePriorityQueue2(getCacheCapacity()),
mDicNodePriorityQueueForTerminal(MAX_RESULTS),
mActiveDicNodes(&mDicNodePriorityQueue0),
mNextActiveDicNodes(&mDicNodePriorityQueue1),
@ -50,7 +51,8 @@ class DicNodesCache {
// We want to use the max capacity for the current active dic node queue.
mActiveDicNodes->clearAndResizeToCapacity();
// nextActiveSize is used to limit the next iteration's active dic node size.
mNextActiveDicNodes->clearAndResize(nextActiveSize);
const int nextActiveSizeFittingToTheCapacity = min(nextActiveSize, getCacheCapacity());
mNextActiveDicNodes->clearAndResize(nextActiveSizeFittingToTheCapacity);
mTerminalDicNodes->clearAndResize(terminalSize);
// We want to use the max capacity for the cached dic nodes that will be used for the
// continuous suggestion.
@ -162,12 +164,21 @@ class DicNodesCache {
return tmp;
}
AK_FORCE_INLINE int getCacheCapacity() const {
return mUsesLargeCapacityCache ?
LARGE_PRIORITY_QUEUE_CAPACITY : SMALL_PRIORITY_QUEUE_CAPACITY;
}
AK_FORCE_INLINE void resetTemporaryCaches() {
mActiveDicNodes->clear();
mNextActiveDicNodes->clear();
mTerminalDicNodes->clear();
}
static const int LARGE_PRIORITY_QUEUE_CAPACITY;
static const int SMALL_PRIORITY_QUEUE_CAPACITY;
const bool mUsesLargeCapacityCache;
// Instances
DicNodePriorityQueue mDicNodePriorityQueue0;
DicNodePriorityQueue mDicNodePriorityQueue1;

View File

@ -23,6 +23,11 @@
namespace latinime {
// 256K bytes threshold is heuristically used to distinguish dictionaries containing many unigrams
// (e.g. main dictionary) from small dictionaries (e.g. contacts...)
const int DicTraverseSession::DICTIONARY_SIZE_THRESHOLD_TO_USE_LARGE_CACHE_FOR_SUGGESTION =
256 * 1024;
void DicTraverseSession::init(const Dictionary *const dictionary, const int *prevWord,
int prevWordLength, const SuggestOptions *const suggestOptions) {
mDictionary = dictionary;

View File

@ -37,8 +37,12 @@ class DicTraverseSession {
public:
// A factory method for DicTraverseSession
static AK_FORCE_INLINE void *getSessionInstance(JNIEnv *env, jstring localeStr) {
return new DicTraverseSession(env, localeStr);
static AK_FORCE_INLINE void *getSessionInstance(JNIEnv *env, jstring localeStr,
jlong dictSize) {
// To deal with the trade-off between accuracy and memory space, large cache is used for
// dictionaries larger that the threshold
return new DicTraverseSession(env, localeStr,
dictSize >= DICTIONARY_SIZE_THRESHOLD_TO_USE_LARGE_CACHE_FOR_SUGGESTION);
}
static AK_FORCE_INLINE void initSessionInstance(DicTraverseSession *traverseSession,
@ -54,10 +58,10 @@ class DicTraverseSession {
delete traverseSession;
}
AK_FORCE_INLINE DicTraverseSession(JNIEnv *env, jstring localeStr)
AK_FORCE_INLINE DicTraverseSession(JNIEnv *env, jstring localeStr, bool usesLargeCache)
: mPrevWordPos(NOT_A_VALID_WORD_POS), mProximityInfo(0),
mDictionary(0), mSuggestOptions(0), mDicNodesCache(), mMultiBigramMap(),
mInputSize(0), mPartiallyCommited(false), mMaxPointerCount(1),
mDictionary(0), mSuggestOptions(0), mDicNodesCache(usesLargeCache),
mMultiBigramMap(), mInputSize(0), mPartiallyCommited(false), mMaxPointerCount(1),
mMultiWordCostMultiplier(1.0f) {
// NOTE: mProximityInfoStates is an array of instances.
// No need to initialize it explicitly here.
@ -181,6 +185,7 @@ class DicTraverseSession {
DISALLOW_IMPLICIT_CONSTRUCTORS(DicTraverseSession);
// threshold to start caching
static const int CACHE_START_INPUT_LENGTH_THRESHOLD;
static const int DICTIONARY_SIZE_THRESHOLD_TO_USE_LARGE_CACHE_FOR_SUGGESTION;
void initializeProximityInfoStates(const int *const inputCodePoints, const int *const inputXs,
const int *const inputYs, const int *const times, const int *const pointerIds,
const int inputSize, const float maxSpatialDistance, const int maxPointerCount);