Move probability updating during GC to PtNodeWriter.

Bug: 11073222

Change-Id: Id452f953c4f72c4e83f6866a87be5f3766d00a31
main
Keisuke Kuroyanagi 2013-12-09 20:39:06 +09:00
parent 88af9eb862
commit ebb57c02c2
4 changed files with 49 additions and 12 deletions

View File

@ -54,6 +54,10 @@ class PtNodeWriter {
virtual bool updatePtNodeProbability(const PtNodeParams *const toBeUpdatedPtNodeParams, virtual bool updatePtNodeProbability(const PtNodeParams *const toBeUpdatedPtNodeParams,
const int probability, const int timestamp) = 0; const int probability, const int timestamp) = 0;
virtual bool updatePtNodeProbabilityAndGetNeedsToKeepPtNodeAfterGC(
const PtNodeParams *const toBeUpdatedPtNodeParams,
bool *const outNeedsToKeepPtNode) = 0;
virtual bool updateChildrenPosition(const PtNodeParams *const toBeUpdatedPtNodeParams, virtual bool updateChildrenPosition(const PtNodeParams *const toBeUpdatedPtNodeParams,
const int newChildrenPosition) = 0; const int newChildrenPosition) = 0;

View File

@ -30,22 +30,16 @@ bool DynamicPatriciaTrieGcEventListeners
// PtNode is useless when the PtNode is not a terminal and doesn't have any not useless // PtNode is useless when the PtNode is not a terminal and doesn't have any not useless
// children. // children.
bool isUselessPtNode = !ptNodeParams->isTerminal(); bool isUselessPtNode = !ptNodeParams->isTerminal();
// TODO: Quit checking mNeedsToDecayWhenUpdating.
if (ptNodeParams->isTerminal() && mNeedsToDecayWhenUpdating) { if (ptNodeParams->isTerminal() && mNeedsToDecayWhenUpdating) {
// TODO: Avoid decaying probability during GC. bool needsToKeepPtNode = true;
const int newProbability = if (!mPtNodeWriter->updatePtNodeProbabilityAndGetNeedsToKeepPtNodeAfterGC(ptNodeParams,
ForgettingCurveUtils::getEncodedProbabilityToSave(ptNodeParams->getProbability(), &needsToKeepPtNode)) {
mHeaderPolicy); AKLOGE("Cannot update PtNode probability or get needs to keep PtNode after GC.");
// Update probability.
if (!mPtNodeWriter->updatePtNodeProbability(ptNodeParams, newProbability,
0 /* timestamp */)) {
return false; return false;
} }
if (!ForgettingCurveUtils::isValidEncodedProbability(newProbability)) { if (!needsToKeepPtNode) {
isUselessPtNode = true; isUselessPtNode = true;
if (!mPtNodeWriter->markPtNodeAsWillBecomeNonTerminal(ptNodeParams)) {
AKLOGE("Cannot mark PtNode as willBecomeNonTerminal.");
return false;
}
} }
} }
if (mChildrenValue > 0) { if (mChildrenValue > 0) {

View File

@ -141,6 +141,42 @@ bool Ver4PatriciaTrieNodeWriter::updatePtNodeProbability(
toBeUpdatedPtNodeParams->getTerminalId(), &probabilityEntry); toBeUpdatedPtNodeParams->getTerminalId(), &probabilityEntry);
} }
bool Ver4PatriciaTrieNodeWriter::updatePtNodeProbabilityAndGetNeedsToKeepPtNodeAfterGC(
const PtNodeParams *const toBeUpdatedPtNodeParams, bool *const outNeedsToKeepPtNode) {
if (!toBeUpdatedPtNodeParams->isTerminal()) {
AKLOGE("updatePtNodeProbabilityAndGetNeedsToSaveForGC is called for non-terminal PtNode.");
return false;
}
if (mBuffers->getHeaderPolicy()->isDecayingDict()) {
const ProbabilityEntry originalProbabilityEntry =
mBuffers->getProbabilityDictContent()->getProbabilityEntry(
toBeUpdatedPtNodeParams->getTerminalId());
// TODO: Use historical info.
const int newProbability = ForgettingCurveUtils::getEncodedProbabilityToSave(
originalProbabilityEntry.getProbability(), mBuffers->getHeaderPolicy());
const ProbabilityEntry probabilityEntry =
originalProbabilityEntry.createEntryWithUpdatedProbability(newProbability);
if (!mBuffers->getMutableProbabilityDictContent()->setProbabilityEntry(
toBeUpdatedPtNodeParams->getTerminalId(), &probabilityEntry)) {
AKLOGE("Cannot write updated probability entry. terminalId: %d",
toBeUpdatedPtNodeParams->getTerminalId());
return false;
}
const bool isValid = ForgettingCurveUtils::isValidEncodedProbability(newProbability);
if (!isValid) {
if (!markPtNodeAsWillBecomeNonTerminal(toBeUpdatedPtNodeParams)) {
AKLOGE("Cannot mark PtNode as willBecomeNonTerminal.");
return false;
}
}
*outNeedsToKeepPtNode = isValid;
} else {
// No need to update probability.
*outNeedsToKeepPtNode = true;
}
return true;
}
bool Ver4PatriciaTrieNodeWriter::updateChildrenPosition( bool Ver4PatriciaTrieNodeWriter::updateChildrenPosition(
const PtNodeParams *const toBeUpdatedPtNodeParams, const int newChildrenPosition) { const PtNodeParams *const toBeUpdatedPtNodeParams, const int newChildrenPosition) {
int childrenPosFieldPos = toBeUpdatedPtNodeParams->getChildrenPosFieldPos(); int childrenPosFieldPos = toBeUpdatedPtNodeParams->getChildrenPosFieldPos();

View File

@ -60,6 +60,9 @@ class Ver4PatriciaTrieNodeWriter : public PtNodeWriter {
virtual bool updatePtNodeProbability(const PtNodeParams *const toBeUpdatedPtNodeParams, virtual bool updatePtNodeProbability(const PtNodeParams *const toBeUpdatedPtNodeParams,
const int newProbability, const int timestamp); const int newProbability, const int timestamp);
virtual bool updatePtNodeProbabilityAndGetNeedsToKeepPtNodeAfterGC(
const PtNodeParams *const toBeUpdatedPtNodeParams, bool *const outNeedsToKeepPtNode);
virtual bool updateChildrenPosition(const PtNodeParams *const toBeUpdatedPtNodeParams, virtual bool updateChildrenPosition(const PtNodeParams *const toBeUpdatedPtNodeParams,
const int newChildrenPosition); const int newChildrenPosition);