Bug 1109928, part 1 - Move graph building inside CCGraphBuilder. r=smaug

This lets us hide a lot of the internal state of graph building from other classes.
This commit is contained in:
Andrew McCreight 2014-12-20 13:35:23 -05:00
parent e37f271a6c
commit 2111e0992c

View File

@ -1260,7 +1260,6 @@ class nsCycleCollector : public nsIMemoryReporter
ccPhase mIncrementalPhase;
CCGraph mGraph;
nsAutoPtr<CCGraphBuilder> mBuilder;
nsAutoPtr<NodePool::Enumerator> mCurrNode;
nsCOMPtr<nsICycleCollectorListener> mListener;
nsIThread* mThread;
@ -2050,6 +2049,7 @@ private:
nsICycleCollectorListener* mListener;
bool mMergeZones;
bool mRanOutOfMemory;
nsAutoPtr<NodePool::Enumerator> mCurrNode;
public:
CCGraphBuilder(CCGraph& aGraph,
@ -2075,6 +2075,12 @@ public:
return mRanOutOfMemory;
}
// This is called when all roots have been added to the graph, to prepare for BuildGraph().
void DoneAddingRoots();
// Do some work traversing nodes in the graph. Returns true if this graph building is finished.
bool BuildGraph(SliceBudget& aBudget);
private:
void DescribeNode(uint32_t aRefCount, const char* aObjName)
{
@ -2210,6 +2216,56 @@ CCGraphBuilder::AddNode(void* aPtr, nsCycleCollectionParticipant* aParticipant)
return result;
}
void
CCGraphBuilder::DoneAddingRoots()
{
// We've finished adding roots, and everything in the graph is a root.
mGraph.mRootCount = mGraph.MapCount();
mCurrNode = new NodePool::Enumerator(mGraph.mNodes);
}
bool
CCGraphBuilder::BuildGraph(SliceBudget& aBudget)
{
const intptr_t kNumNodesBetweenTimeChecks = 1000;
const intptr_t kStep = SliceBudget::CounterReset / kNumNodesBetweenTimeChecks;
MOZ_ASSERT(mCurrNode);
while (!aBudget.isOverBudget() && !mCurrNode->IsDone()) {
PtrInfo* pi = mCurrNode->GetNext();
if (!pi) {
MOZ_CRASH();
}
// We need to call Traverse() method on deleted nodes, to set their
// firstChild() that may be read by a prior non-deleted neighbor.
Traverse(pi);
if (mCurrNode->AtBlockEnd()) {
SetLastChild();
}
aBudget.step(kStep);
}
if (!mCurrNode->IsDone()) {
return false;
}
if (mGraph.mRootCount > 0) {
SetLastChild();
}
if (RanOutOfMemory()) {
MOZ_ASSERT(false, "Ran out of memory while building cycle collector graph");
CC_TELEMETRY(_OOM, true);
}
mCurrNode = nullptr;
return true;
}
MOZ_NEVER_INLINE void
CCGraphBuilder::Traverse(PtrInfo* aPtrInfo)
{
@ -2817,48 +2873,20 @@ nsCycleCollector::ForgetSkippable(bool aRemoveChildlessNodes,
MOZ_NEVER_INLINE void
nsCycleCollector::MarkRoots(SliceBudget& aBudget)
{
const intptr_t kNumNodesBetweenTimeChecks = 1000;
const intptr_t kStep = SliceBudget::CounterReset / kNumNodesBetweenTimeChecks;
TimeLog timeLog;
AutoRestore<bool> ar(mScanInProgress);
MOZ_ASSERT(!mScanInProgress);
mScanInProgress = true;
MOZ_ASSERT(mIncrementalPhase == GraphBuildingPhase);
MOZ_ASSERT(mCurrNode);
while (!aBudget.isOverBudget() && !mCurrNode->IsDone()) {
PtrInfo* pi = mCurrNode->GetNext();
if (!pi) {
MOZ_CRASH();
}
bool doneBuilding = mBuilder->BuildGraph(aBudget);
// We need to call the builder's Traverse() method on deleted nodes, to
// set their firstChild() that may be read by a prior non-deleted
// neighbor.
mBuilder->Traverse(pi);
if (mCurrNode->AtBlockEnd()) {
mBuilder->SetLastChild();
}
aBudget.step(kStep);
}
if (!mCurrNode->IsDone()) {
if (!doneBuilding) {
timeLog.Checkpoint("MarkRoots()");
return;
}
if (mGraph.mRootCount > 0) {
mBuilder->SetLastChild();
}
if (mBuilder->RanOutOfMemory()) {
MOZ_ASSERT(false, "Ran out of memory while building cycle collector graph");
CC_TELEMETRY(_OOM, true);
}
mBuilder = nullptr;
mCurrNode = nullptr;
mIncrementalPhase = ScanAndCollectWhitePhase;
timeLog.Checkpoint("MarkRoots()");
}
@ -3790,10 +3818,7 @@ nsCycleCollector::BeginCollection(ccType aCCType,
mPurpleBuf.SelectPointers(*mBuilder);
timeLog.Checkpoint("SelectPointers()");
// We've finished adding roots, and everything in the graph is a root.
mGraph.mRootCount = mGraph.MapCount();
mCurrNode = new NodePool::Enumerator(mGraph.mNodes);
mBuilder->DoneAddingRoots();
mIncrementalPhase = GraphBuildingPhase;
}