Skip to content
This repository has been archived by the owner on Aug 4, 2022. It is now read-only.

Commit

Permalink
Bug 1358761 - replace PurpleBlock with SegmentedVector to reduce indi…
Browse files Browse the repository at this point in the history
…rect memory accesses when calling suspect, r=mccr8,nfroyd
  • Loading branch information
Olli Pettay authored and Olli Pettay committed May 4, 2017
1 parent 327a567 commit 6483d7c
Show file tree
Hide file tree
Showing 3 changed files with 240 additions and 151 deletions.
35 changes: 28 additions & 7 deletions mfbt/SegmentedVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ class SegmentedVector : private AllocPolicy
? (IdealSegmentSize - kSingleElementSegmentSize) / sizeof(T) + 1
: 1;

public:
typedef SegmentImpl<kSegmentCapacity> Segment;

public:
// The |aIdealSegmentSize| is only for sanity checking. If it's specified, we
// check that the actual segment size is as close as possible to it. This
// serves as a sanity check for SegmentedVectorCapacity's capacity
Expand Down Expand Up @@ -260,7 +260,6 @@ class SegmentedVector : private AllocPolicy
// than we want to pop.
MOZ_ASSERT(last);
MOZ_ASSERT(last == mSegments.getLast());
MOZ_ASSERT(aNumElements != 0);
MOZ_ASSERT(aNumElements < last->Length());
for (uint32_t i = 0; i < aNumElements; ++i) {
last->PopLast();
Expand All @@ -275,17 +274,25 @@ class SegmentedVector : private AllocPolicy
// f(elem);
// }
//
// Note, adding new entries to the SegmentedVector while using iterators
// is supported, but removing is not!
// If an iterator has entered Done() state, adding more entries to the
// vector doesn't affect it.
class IterImpl
{
friend class SegmentedVector;

Segment* mSegment;
size_t mIndex;

explicit IterImpl(SegmentedVector* aVector)
: mSegment(aVector->mSegments.getFirst())
, mIndex(0)
{}
explicit IterImpl(SegmentedVector* aVector, bool aFromFirst)
: mSegment(aFromFirst ? aVector->mSegments.getFirst() :
aVector->mSegments.getLast())
, mIndex(aFromFirst ? 0 :
(mSegment ? mSegment->Length() - 1 : 0))
{
MOZ_ASSERT_IF(mSegment, mSegment->Length() > 0);
}

public:
bool Done() const { return !mSegment; }
Expand All @@ -311,9 +318,23 @@ class SegmentedVector : private AllocPolicy
mIndex = 0;
}
}

void Prev()
{
MOZ_ASSERT(!Done());
if (mIndex == 0) {
mSegment = mSegment->getPrevious();
if (mSegment) {
mIndex = mSegment->Length() - 1;
}
} else {
--mIndex;
}
}
};

IterImpl Iter() { return IterImpl(this); }
IterImpl Iter() { return IterImpl(this, true); }
IterImpl IterFromLast() { return IterImpl(this, false); }

// Measure the memory consumption of the vector excluding |this|. Note that
// it only measures the vector itself. If the vector elements contain
Expand Down
82 changes: 82 additions & 0 deletions mfbt/tests/TestSegmentedVector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,93 @@ void TestSegmentCapacitiesAndAlignments()
SegmentedVector<mozilla::AlignedElem<16>, 100> v7(100);
}

void
TestIterator()
{
SegmentedVector<int, 4> v;

auto iter = v.Iter();
auto iterFromLast = v.IterFromLast();
MOZ_RELEASE_ASSERT(iter.Done());
MOZ_RELEASE_ASSERT(iterFromLast.Done());

v.Append(1);
iter = v.Iter();
iterFromLast = v.IterFromLast();
MOZ_RELEASE_ASSERT(!iter.Done());
MOZ_RELEASE_ASSERT(!iterFromLast.Done());

iter.Next();
MOZ_RELEASE_ASSERT(iter.Done());
iterFromLast.Next();
MOZ_RELEASE_ASSERT(iterFromLast.Done());

iter = v.Iter();
iterFromLast = v.IterFromLast();
MOZ_RELEASE_ASSERT(!iter.Done());
MOZ_RELEASE_ASSERT(!iterFromLast.Done());

iter.Prev();
MOZ_RELEASE_ASSERT(iter.Done());
iterFromLast.Prev();
MOZ_RELEASE_ASSERT(iterFromLast.Done());

// Append enough entries to ensure we have at least two segments.
v.Append(1);
v.Append(1);
v.Append(1);
v.Append(1);

iter = v.Iter();
iterFromLast = v.IterFromLast();
MOZ_RELEASE_ASSERT(!iter.Done());
MOZ_RELEASE_ASSERT(!iterFromLast.Done());

iter.Prev();
MOZ_RELEASE_ASSERT(iter.Done());
iterFromLast.Next();
MOZ_RELEASE_ASSERT(iterFromLast.Done());

iter = v.Iter();
iterFromLast = v.IterFromLast();
MOZ_RELEASE_ASSERT(!iter.Done());
MOZ_RELEASE_ASSERT(!iterFromLast.Done());

iter.Next();
MOZ_RELEASE_ASSERT(!iter.Done());
iterFromLast.Prev();
MOZ_RELEASE_ASSERT(!iterFromLast.Done());

iter = v.Iter();
iterFromLast = v.IterFromLast();
int count = 0;
for (; !iter.Done() && !iterFromLast.Done(); iter.Next(), iterFromLast.Prev()) {
++count;
}
MOZ_RELEASE_ASSERT(count == 5);

// Modify the vector while using the iterator.
iterFromLast = v.IterFromLast();
v.Append(2);
v.Append(3);
v.Append(4);
iterFromLast.Next();
MOZ_RELEASE_ASSERT(!iterFromLast.Done());
MOZ_RELEASE_ASSERT(iterFromLast.Get() == 2);
iterFromLast.Next();
MOZ_RELEASE_ASSERT(iterFromLast.Get() == 3);
iterFromLast.Next();
MOZ_RELEASE_ASSERT(iterFromLast.Get() == 4);
iterFromLast.Next();
MOZ_RELEASE_ASSERT(iterFromLast.Done());
}

int main(void)
{
TestBasics();
TestConstructorsAndDestructors();
TestSegmentCapacitiesAndAlignments();
TestIterator();

return 0;
}
Loading

0 comments on commit 6483d7c

Please sign in to comment.