Skip to content

Commit

Permalink
[1.8>1.9] [MERGE #4588 @obastemur] BVSparse: keep 'last found' for fa…
Browse files Browse the repository at this point in the history
…st fromIndex discovery

Merge pull request #4588 from obastemur:faster_bvsparse

Helps the number of tests not to timeout on CI machines.
  • Loading branch information
obastemur committed Jan 23, 2018
2 parents fdd885b + fc29ccd commit 14831da
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions lib/Common/DataStructures/SparseBitVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class BVSparse
// Data
public:
Field(BVSparseNode*, TAllocator) head;
Field(BVSparseNode*, TAllocator) lastFoundIndex;

private:
FieldNoBarrier(TAllocator*) alloc;
Expand Down Expand Up @@ -322,7 +323,8 @@ const SparseBVUnit BVSparse<TAllocator>::s_EmptyUnit(0);
template <class TAllocator>
BVSparse<TAllocator>::BVSparse(TAllocator* allocator) :
alloc(allocator),
head(nullptr)
head(nullptr),
lastFoundIndex(nullptr)
{
this->lastUsedNodePrevNextField = &this->head;
}
Expand Down Expand Up @@ -414,7 +416,10 @@ BVSparse<TAllocator>::NodeFromIndex(BVIndex i, Field(BVSparseNode*, TAllocator)
const BVIndex searchIndex = SparseBVUnit::Floor(i);

Field(BVSparseNode*, TAllocator) const* prevNextField = &this->head;
const BVSparseNode * curNode = *prevNextField;
Field(BVSparseNode*, TAllocator) const* prevLastField = &this->lastFoundIndex;

const BVSparseNode * curNode = *prevNextField,
* lastNode = *prevLastField;
if (curNode != nullptr)
{
if (curNode->startIndex == searchIndex)
Expand All @@ -423,6 +428,21 @@ BVSparse<TAllocator>::NodeFromIndex(BVIndex i, Field(BVSparseNode*, TAllocator)
return curNode;
}

if (lastNode && lastNode->startIndex != curNode->startIndex)
{
if (lastNode->startIndex == searchIndex)
{
*prevNextFieldOut = prevLastField;
return lastNode;
}

if (lastNode->startIndex < searchIndex)
{
prevNextField = &this->lastFoundIndex;
curNode = this->lastFoundIndex;
}
}

if (curNode->startIndex > searchIndex)
{
prevNextField = &this->head;
Expand All @@ -440,6 +460,8 @@ BVSparse<TAllocator>::NodeFromIndex(BVIndex i, Field(BVSparseNode*, TAllocator)
prevNextField = &curNode->next;
}

const_cast<BVSparse<TAllocator>*>(this)->lastFoundIndex = *prevNextField;

if (curNode && searchIndex == curNode->startIndex)
{
*prevNextFieldOut = prevNextField;
Expand Down Expand Up @@ -486,6 +508,7 @@ template <class TAllocator>
BVSparseNode<TAllocator> *
BVSparse<TAllocator>::DeleteNode(BVSparseNode *node, bool bResetLastUsed)
{
this->lastFoundIndex = nullptr;
BVSparseNode *next = node->next;
QueueInFreeList(node);

Expand Down Expand Up @@ -563,6 +586,7 @@ BVSparse<TAllocator>::ClearAll()
QueueInFreeList(node);
}
this->head = nullptr;
this->lastFoundIndex = nullptr;
this->lastUsedNodePrevNextField = &this->head;
}

Expand Down Expand Up @@ -902,6 +926,7 @@ BVSparse<TAllocator>::CopyFromNode(const ::BVSparseNode<TSrcAllocator> * node2)
{
BVSparseNode * node1 = this->head;
Field(BVSparseNode*, TAllocator)* prevNextField = &this->head;
this->lastFoundIndex = nullptr;

while (node1 != nullptr && node2 != nullptr)
{
Expand Down

0 comments on commit 14831da

Please sign in to comment.