void test1() { ObjectPool<SmallObject> op; SmallObject* s = op.get(); op.put(s); s = op.get(); s = op.get(); s = op.get(); s = op.get(); printf("++++++++++++++++\n"); }
void LexicalReorderingTableTree::auxCacheForSrcPhrase(const Phrase& f) { if(m_FactorsE.empty()) { //f is all of key... Candidates cands; m_Table->GetCandidates(MakeTableKey(f,Phrase(ARRAY_SIZE_INCR)),&cands); m_Cache[MakeCacheKey(f,Phrase(ARRAY_SIZE_INCR))] = cands; } else { ObjectPool<PPimp> pool; PPimp* pPos = m_Table->GetRoot(); //1) goto subtree for f for(size_t i = 0; i < f.GetSize() && 0 != pPos && pPos->isValid(); ++i) { /* old code pPos = m_Table.Extend(pPos, auxClearString(f.GetWord(i).ToString(m_FactorsF)), SourceVocId); */ pPos = m_Table->Extend(pPos, f.GetWord(i).GetString(m_FactorsF, false), SourceVocId); } if(0 != pPos && pPos->isValid()) { pPos = m_Table->Extend(pPos, PrefixTreeMap::MagicWord); } if(0 == pPos || !pPos->isValid()) { return; } //2) explore whole subtree depth first & cache std::string cache_key = auxClearString(f.GetStringRep(m_FactorsF)) + "|||"; std::vector<State> stack; stack.push_back(State(pool.get(PPimp(pPos->ptr()->getPtr(pPos->idx),0,0)),"")); Candidates cands; while(!stack.empty()) { if(stack.back().pos->isValid()) { LabelId w = stack.back().pos->ptr()->getKey(stack.back().pos->idx); std::string next_path = stack.back().path + " " + m_Table->ConvertWord(w,TargetVocId); //cache this m_Table->GetCandidates(*stack.back().pos,&cands); if(!cands.empty()) { m_Cache[cache_key + auxClearString(next_path)] = cands; } cands.clear(); PPimp* next_pos = pool.get(PPimp(stack.back().pos->ptr()->getPtr(stack.back().pos->idx),0,0)); ++stack.back().pos->idx; stack.push_back(State(next_pos,next_path)); } else { stack.pop_back(); } } } }
int main() { ObjectPool<A> pool; pool.add(std::unique_ptr<A>(new A())); pool.add(std::unique_ptr<A>(new A())); std::cout << "Object pool size: " << pool.size() << std::endl; { auto a = pool.get(); a->print(); pool.get(); std::cout << "Object pool size: " << pool.size() << std::endl; } std::cout << "Object pool size: " << pool.size() << std::endl; return 0; }
void test2() { TimeFilm tm("object pool: "); ObjectPool<SmallObject> op; SmallObject* s = 0; //int m = 1000000; int m = 100; Random rnd; const char* hello = "hello"; while (--m) { //s = op.get(); s = op.get(m,hello); #if 1 if (s && rnd(2)) { printf("put\n"); op.put(s); } #endif } }