start using State pointer

This commit is contained in:
Hieu Hoang 2016-02-18 12:51:42 +00:00
parent 8dcdd4c1b7
commit c28010e9f9
2 changed files with 11 additions and 8 deletions

View File

@ -303,24 +303,27 @@ lm::WordIndex *KENLM::LastIDs(const Hypothesis &hypo, lm::WordIndex *indices) co
float KENLM::ScoreAndCache(const Manager &mgr, const lm::ngram::State &in_state, const lm::WordIndex new_word, lm::ngram::State &out_state) const float KENLM::ScoreAndCache(const Manager &mgr, const lm::ngram::State &in_state, const lm::WordIndex new_word, lm::ngram::State &out_state) const
{ {
MemPool &pool = mgr.GetPool();
//cerr << "score="; //cerr << "score=";
float score; float score;
CacheColl &m_lmCache = *((CacheColl*)mgr.lmCache); CacheColl &lmCache = *((CacheColl*)mgr.lmCache);
LMCacheKey key(in_state, new_word); LMCacheKey key(in_state, new_word);
CacheColl::iterator iter; CacheColl::iterator iter;
iter = m_lmCache.find(key); iter = lmCache.find(key);
if (iter == m_lmCache.end()) { if (iter == lmCache.end()) {
score = m_ngram->Score(in_state, new_word, out_state); lm::ngram::State *newState = new (pool.Allocate<lm::ngram::State>()) lm::ngram::State();
score = m_ngram->Score(in_state, new_word, *newState);
LMCacheValue &val = m_lmCache[key]; LMCacheValue &val = lmCache[key];
val.first = score; val.first = score;
val.second = out_state; val.second = newState;
out_state = *newState;
} }
else { else {
const LMCacheValue &val = iter->second; const LMCacheValue &val = iter->second;
score = val.first; score = val.first;
out_state = val.second; out_state = *val.second;
} }
//score = m_ngram->Score(in_state, new_word, out_state); //score = m_ngram->Score(in_state, new_word, out_state);

View File

@ -81,7 +81,7 @@ protected:
std::vector<lm::WordIndex> m_lmIdLookup; std::vector<lm::WordIndex> m_lmIdLookup;
typedef std::pair<lm::ngram::State, lm::WordIndex> LMCacheKey; typedef std::pair<lm::ngram::State, lm::WordIndex> LMCacheKey;
typedef std::pair<float, lm::ngram::State> LMCacheValue; typedef std::pair<float, lm::ngram::State*> LMCacheValue;
typedef boost::unordered_map<LMCacheKey, LMCacheValue> CacheColl; typedef boost::unordered_map<LMCacheKey, LMCacheValue> CacheColl;
mutable boost::thread_specific_ptr<CacheColl> m_cache; mutable boost::thread_specific_ptr<CacheColl> m_cache;