am ebb57c02: Move probability updating during GC to PtNodeWriter.

* commit 'ebb57c02c2c2dd97fab118b77cd9217e5ad03ec1':
  Move probability updating during GC to PtNodeWriter.
main
Keisuke Kuroyanagi 2013-12-09 03:53:37 -08:00 committed by Android Git Automerger
commit 6b02622694
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);