Move correction state to stack memory

*Before
(0)  13.18 (0.01%)
(1)  93025.41 (62.06%)
(2)  10.75 (0.01%)
(3)  10.50 (0.01%)
(4)  117.50 (0.08%)
(5)  55678.98 (37.14%)
(6)  9.09 (0.01%)
(20) 883.84 (0.59%)
Total 149898.24 (sum of others 149749.25)

*After
(0)  17.41 (0.01%)
(1)  92673.41 (61.95%)
(2)  10.62 (0.01%)
(3)  10.37 (0.01%)
(4)  120.96 (0.08%)
(5)  55741.18 (37.26%)
(6)  11.01 (0.01%)
(20) 862.72 (0.58%)
Total 149595.52 (sum of others 149447.68)

Change-Id: Ia5a25a544fc388e4dab1e08d8f78d5117b249cf3
main
satok 2012-06-14 11:25:50 -07:00
parent 78573f2e8a
commit 1bc038c5e4
15 changed files with 35 additions and 26 deletions

View File

@ -26,6 +26,7 @@ namespace latinime {
class AdditionalProximityChars {
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(AdditionalProximityChars);
static const std::string LOCALE_EN_US;
static const int EN_US_ADDITIONAL_A_SIZE = 4;
static const int32_t EN_US_ADDITIONAL_A[];

View File

@ -36,6 +36,7 @@ class BigramDictionary {
bool isValidBigram(const int32_t *word1, int length1, const int32_t *word2, int length2);
~BigramDictionary();
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(BigramDictionary);
bool addWordBigram(unsigned short *word, int length, int frequency);
int getBigramAddress(int *pos, bool advance);
int getBigramFreq(int *pos);

View File

@ -25,6 +25,7 @@ namespace latinime {
class BinaryFormat {
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(BinaryFormat);
const static int32_t MINIMAL_ONE_BYTE_CHARACTER_VALUE = 0x20;
const static int32_t CHARACTER_ARRAY_TERMINATOR = 0x1F;
const static int MULTIPLE_BYTE_CHARACTER_ADDITIONAL_SIZE = 2;

View File

@ -106,11 +106,6 @@ inline bool Correction::isQuote(const unsigned short c) {
// Correction //
////////////////
Correction::Correction(const int typedLetterMultiplier, const int fullWordMultiplier)
: TYPED_LETTER_MULTIPLIER(typedLetterMultiplier), FULL_WORD_MULTIPLIER(fullWordMultiplier) {
initEditDistance(mEditDistanceTable);
}
void Correction::resetCorrection() {
mTotalTraverseCount = 0;
}

View File

@ -94,7 +94,7 @@ class Correction {
}
}
Correction(const int typedLetterMultiplier, const int fullWordMultiplier);
Correction() {};
void resetCorrection();
void initCorrection(
const ProximityInfo *pi, const int inputLength, const int maxWordLength);
@ -175,8 +175,6 @@ class Correction {
private:
static const int CODE_SPACE = ' ';
static const int MAX_INITIAL_SCORE = 255;
static const int TYPED_LETTER_MULTIPLIER = 2;
static const int FULL_WORD_MULTIPLIER = 2;
};
// proximity info state
@ -195,6 +193,7 @@ class Correction {
}
private:
DISALLOW_COPY_AND_ASSIGN(Correction);
inline void incrementInputIndex();
inline void incrementOutputIndex();
inline void startToTraverseAllNodes();
@ -206,8 +205,8 @@ class Correction {
inline int getFinalProbabilityInternal(const int probability, unsigned short **word,
int* wordLength, const int inputLength);
const int TYPED_LETTER_MULTIPLIER;
const int FULL_WORD_MULTIPLIER;
static const int TYPED_LETTER_MULTIPLIER = 2;
static const int FULL_WORD_MULTIPLIER = 2;
const ProximityInfo *mProximityInfo;
bool mUseFullEditDistance;

View File

@ -292,6 +292,14 @@ template<typename T> inline T max(T a, T b) { return a > b ? a : b; }
#define INPUTLENGTH_FOR_DEBUG -1
#define MIN_OUTPUT_INDEX_FOR_DEBUG -1
#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
TypeName(const TypeName&); \
void operator=(const TypeName&)
#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
TypeName(); \
DISALLOW_COPY_AND_ASSIGN(TypeName)
// Used as a return value for character comparison
typedef enum {
// Same char, possibly with different case or accent

View File

@ -38,7 +38,6 @@ Dictionary::Dictionary(void *dict, int dictSize, int mmapFd, int dictBufAdjust,
AKLOGI("IN NATIVE SUGGEST Version: %d", (mDict[0] & 0xFF));
}
}
mCorrection = new Correction(typedLetterMultiplier, fullWordMultiplier);
mWordsPriorityQueuePool = new WordsPriorityQueuePool(
maxWords, SUB_QUEUE_MAX_WORDS, maxWordLength);
const unsigned int headerSize = BinaryFormat::getHeaderSize(mDict);
@ -49,7 +48,6 @@ Dictionary::Dictionary(void *dict, int dictSize, int mmapFd, int dictBufAdjust,
}
Dictionary::~Dictionary() {
delete mCorrection;
delete mWordsPriorityQueuePool;
delete mUnigramDictionary;
delete mBigramDictionary;

View File

@ -21,7 +21,6 @@
#include "bigram_dictionary.h"
#include "char_utils.h"
#include "correction.h"
#include "defines.h"
#include "proximity_info.h"
#include "unigram_dictionary.h"
@ -42,7 +41,7 @@ class Dictionary {
mBigramDictionary->fillBigramAddressToFrequencyMapAndFilter(prevWordChars,
prevWordLength, &bigramMap, bigramFilter);
return mUnigramDictionary->getSuggestions(proximityInfo, mWordsPriorityQueuePool,
mCorrection, xcoordinates, ycoordinates, codes, codesSize, &bigramMap,
xcoordinates, ycoordinates, codes, codesSize, &bigramMap,
bigramFilter, useFullEditDistance, outWords, frequencies);
}
@ -65,6 +64,7 @@ class Dictionary {
static int wideStrLen(unsigned short *str);
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(Dictionary);
const unsigned char *mDict;
// Used only for the mmap version of dictionary loading, but we use these as dummy variables
@ -73,10 +73,9 @@ class Dictionary {
const int mMmapFd;
const int mDictBufAdjust;
UnigramDictionary *mUnigramDictionary;
const UnigramDictionary *mUnigramDictionary;
BigramDictionary *mBigramDictionary;
WordsPriorityQueuePool *mWordsPriorityQueuePool;
Correction *mCorrection;
};
// public static utility methods

View File

@ -99,6 +99,7 @@ class ProximityInfo {
}
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(ProximityInfo);
// The max number of the keys in one keyboard layout
static const int MAX_KEY_COUNT_IN_A_KEYBOARD = 64;
// The upper limit of the char code in mCodeToKeyIndex

View File

@ -49,6 +49,7 @@ class ProximityInfoState {
/////////////////////////////////////////
// Defined here //
/////////////////////////////////////////
ProximityInfoState() {};
inline const int* getProximityCharsAt(const int index) const {
return mInputCodes + (index * MAX_PROXIMITY_CHARS_SIZE_INTERNAL);
}
@ -162,6 +163,7 @@ class ProximityInfoState {
}
private:
DISALLOW_COPY_AND_ASSIGN(ProximityInfoState);
/////////////////////////////////////////
// Defined in proximity_info_state.cpp //
/////////////////////////////////////////

View File

@ -62,6 +62,7 @@ class TerminalAttributes {
};
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(TerminalAttributes);
const uint8_t* const mDict;
const uint8_t mFlags;
const int mStartPos;

View File

@ -170,14 +170,14 @@ void UnigramDictionary::getWordWithDigraphSuggestionsRec(ProximityInfo *proximit
// bigramFilter is a bloom filter for fast rejection: see functions setInFilter and isInFilter
// in bigram_dictionary.cpp
int UnigramDictionary::getSuggestions(ProximityInfo *proximityInfo,
WordsPriorityQueuePool *queuePool, Correction *correction, const int *xcoordinates,
WordsPriorityQueuePool *queuePool, const int *xcoordinates,
const int *ycoordinates, const int *codes, const int codesSize,
const std::map<int, int> *bigramMap, const uint8_t *bigramFilter,
const bool useFullEditDistance, unsigned short *outWords, int *frequencies) const {
queuePool->clearAll();
Correction* masterCorrection = correction;
correction->resetCorrection();
Correction masterCorrection;
masterCorrection.resetCorrection();
if (BinaryFormat::REQUIRES_GERMAN_UMLAUT_PROCESSING & FLAGS)
{ // Incrementally tune the word and try all possibilities
int codesBuffer[getCodesBufferSize(codes, codesSize)];
@ -185,7 +185,7 @@ int UnigramDictionary::getSuggestions(ProximityInfo *proximityInfo,
int yCoordinatesBuffer[codesSize];
getWordWithDigraphSuggestionsRec(proximityInfo, xcoordinates, ycoordinates, codesBuffer,
xCoordinatesBuffer, yCoordinatesBuffer, codesSize, bigramMap, bigramFilter,
useFullEditDistance, codes, codesSize, 0, codesBuffer, masterCorrection,
useFullEditDistance, codes, codesSize, 0, codesBuffer, &masterCorrection,
queuePool, GERMAN_UMLAUT_DIGRAPHS,
sizeof(GERMAN_UMLAUT_DIGRAPHS) / sizeof(GERMAN_UMLAUT_DIGRAPHS[0]));
} else if (BinaryFormat::REQUIRES_FRENCH_LIGATURES_PROCESSING & FLAGS) {
@ -194,28 +194,28 @@ int UnigramDictionary::getSuggestions(ProximityInfo *proximityInfo,
int yCoordinatesBuffer[codesSize];
getWordWithDigraphSuggestionsRec(proximityInfo, xcoordinates, ycoordinates, codesBuffer,
xCoordinatesBuffer, yCoordinatesBuffer, codesSize, bigramMap, bigramFilter,
useFullEditDistance, codes, codesSize, 0, codesBuffer, masterCorrection,
useFullEditDistance, codes, codesSize, 0, codesBuffer, &masterCorrection,
queuePool, FRENCH_LIGATURES_DIGRAPHS,
sizeof(FRENCH_LIGATURES_DIGRAPHS) / sizeof(FRENCH_LIGATURES_DIGRAPHS[0]));
} else { // Normal processing
getWordSuggestions(proximityInfo, xcoordinates, ycoordinates, codes, codesSize,
bigramMap, bigramFilter, useFullEditDistance, masterCorrection, queuePool);
bigramMap, bigramFilter, useFullEditDistance, &masterCorrection, queuePool);
}
PROF_START(20);
if (DEBUG_DICT) {
float ns = queuePool->getMasterQueue()->getHighestNormalizedScore(
correction->getPrimaryInputWord(), codesSize, 0, 0, 0);
masterCorrection.getPrimaryInputWord(), codesSize, 0, 0, 0);
ns += 0;
AKLOGI("Max normalized score = %f", ns);
}
const int suggestedWordsCount =
queuePool->getMasterQueue()->outputSuggestions(
correction->getPrimaryInputWord(), codesSize, frequencies, outWords);
masterCorrection.getPrimaryInputWord(), codesSize, frequencies, outWords);
if (DEBUG_DICT) {
float ns = queuePool->getMasterQueue()->getHighestNormalizedScore(
correction->getPrimaryInputWord(), codesSize, 0, 0, 0);
masterCorrection.getPrimaryInputWord(), codesSize, 0, 0, 0);
ns += 0;
AKLOGI("Returning %d words", suggestedWordsCount);
/// Print the returned words

View File

@ -78,13 +78,14 @@ class UnigramDictionary {
int getFrequency(const int32_t* const inWord, const int length) const;
int getBigramPosition(int pos, unsigned short *word, int offset, int length) const;
int getSuggestions(ProximityInfo *proximityInfo, WordsPriorityQueuePool *queuePool,
Correction *correction, const int *xcoordinates, const int *ycoordinates,
const int *xcoordinates, const int *ycoordinates,
const int *codes, const int codesSize, const std::map<int, int> *bigramMap,
const uint8_t *bigramFilter, const bool useFullEditDistance, unsigned short *outWords,
int *frequencies) const;
virtual ~UnigramDictionary();
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(UnigramDictionary);
void getWordSuggestions(ProximityInfo *proximityInfo, const int *xcoordinates,
const int *ycoordinates, const int *codes, const int inputLength,
const std::map<int, int> *bigramMap, const uint8_t *bigramFilter,

View File

@ -182,6 +182,7 @@ class WordsPriorityQueue {
}
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(WordsPriorityQueue);
struct wordComparator {
bool operator ()(SuggestedWord * left, SuggestedWord * right) {
return left->mScore > right->mScore;

View File

@ -85,6 +85,7 @@ class WordsPriorityQueuePool {
}
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(WordsPriorityQueuePool);
WordsPriorityQueue* mMasterQueue;
WordsPriorityQueue* mSubQueues[SUB_QUEUE_MAX_COUNT * MULTIPLE_WORDS_SUGGESTION_MAX_WORDS];
char mMasterQueueBuf[sizeof(WordsPriorityQueue)];