summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@oath.com>2019-06-05 12:39:43 +0000
committerHenning Baldersheim <balder@oath.com>2019-06-05 12:39:43 +0000
commita350e862c57a477388679e871de40d009cc24921 (patch)
tree730402c0d659b14884b78cfec1d205459491af37
parent7d13aabe7132fc2aac1b30623793bb52b342b9da (diff)
Let capacity be set based on amount of memory allocated.
-rw-r--r--searchlib/src/tests/common/bitvector/bitvector_test.cpp17
-rw-r--r--searchlib/src/vespa/searchlib/attribute/singleboolattribute.cpp32
-rw-r--r--searchlib/src/vespa/searchlib/attribute/singleboolattribute.h1
-rw-r--r--searchlib/src/vespa/searchlib/common/allocatedbitvector.cpp25
-rw-r--r--searchlib/src/vespa/searchlib/common/allocatedbitvector.h2
-rw-r--r--searchlib/src/vespa/searchlib/common/growablebitvector.h3
-rw-r--r--searchlib/src/vespa/searchlib/common/partialbitvector.cpp2
7 files changed, 50 insertions, 32 deletions
diff --git a/searchlib/src/tests/common/bitvector/bitvector_test.cpp b/searchlib/src/tests/common/bitvector/bitvector_test.cpp
index 22a47952acb..e61c21bee1c 100644
--- a/searchlib/src/tests/common/bitvector/bitvector_test.cpp
+++ b/searchlib/src/tests/common/bitvector/bitvector_test.cpp
@@ -548,38 +548,39 @@ TEST("requireThatGrowWorks")
v.setBit(103);
EXPECT_EQUAL(200u, v.size());
+ EXPECT_EQUAL(1023u, v.capacity());
v.invalidateCachedCount();
EXPECT_TRUE(assertBV("[7,39,71,103]", v));
EXPECT_EQUAL(4u, v.countTrueBits());
- EXPECT_TRUE(v.reserve(204));
+ EXPECT_TRUE(v.reserve(1024));
EXPECT_EQUAL(200u, v.size());
- EXPECT_EQUAL(204u, v.capacity());
+ EXPECT_EQUAL(2047u, v.capacity());
EXPECT_TRUE(assertBV("[7,39,71,103]", v));
EXPECT_EQUAL(4u, v.countTrueBits());
EXPECT_FALSE(v.extend(202));
EXPECT_EQUAL(202u, v.size());
- EXPECT_EQUAL(204u, v.capacity());
+ EXPECT_EQUAL(2047u, v.capacity());
EXPECT_TRUE(assertBV("[7,39,71,103]", v));
EXPECT_EQUAL(4u, v.countTrueBits());
EXPECT_FALSE(v.shrink(200));
EXPECT_EQUAL(200u, v.size());
- EXPECT_EQUAL(204u, v.capacity());
+ EXPECT_EQUAL(2047u, v.capacity());
EXPECT_TRUE(assertBV("[7,39,71,103]", v));
EXPECT_EQUAL(4u, v.countTrueBits());
- EXPECT_FALSE(v.reserve(204));
+ EXPECT_FALSE(v.reserve(2047));
EXPECT_EQUAL(200u, v.size());
- EXPECT_EQUAL(204u, v.capacity());
+ EXPECT_EQUAL(2047u, v.capacity());
EXPECT_TRUE(assertBV("[7,39,71,103]", v));
EXPECT_EQUAL(4u, v.countTrueBits());
EXPECT_FALSE(v.shrink(202));
EXPECT_EQUAL(202u, v.size());
- EXPECT_EQUAL(204u, v.capacity());
+ EXPECT_EQUAL(2047u, v.capacity());
EXPECT_TRUE(assertBV("[7,39,71,103]", v));
EXPECT_EQUAL(4u, v.countTrueBits());
EXPECT_FALSE(v.shrink(100));
EXPECT_EQUAL(100u, v.size());
- EXPECT_EQUAL(204u, v.capacity());
+ EXPECT_EQUAL(2047u, v.capacity());
EXPECT_TRUE(assertBV("[7,39,71]", v));
EXPECT_EQUAL(3u, v.countTrueBits());
g.transferHoldLists(1);
diff --git a/searchlib/src/vespa/searchlib/attribute/singleboolattribute.cpp b/searchlib/src/vespa/searchlib/attribute/singleboolattribute.cpp
index 3e260e8453a..75a0e4b8c71 100644
--- a/searchlib/src/vespa/searchlib/attribute/singleboolattribute.cpp
+++ b/searchlib/src/vespa/searchlib/attribute/singleboolattribute.cpp
@@ -25,24 +25,28 @@ SingleBoolAttribute::~SingleBoolAttribute()
getGenerationHolder().clearHoldLists();
}
-bool
-SingleBoolAttribute::addDoc(DocId & doc) {
- size_t needSize = getNumDocs() + 1;
- bool incGen = false;
- if (_bv.capacity() < needSize) {
+void
+SingleBoolAttribute::ensureRoom(DocId docIdLimit) {
+ if (_bv.capacity() < docIdLimit) {
const GrowStrategy & gs = this->getConfig().getGrowStrategy();
- uint32_t newSize = needSize + (needSize * gs.getDocsGrowFactor()) + gs.getDocsGrowDelta();
- incGen = _bv.reserve(newSize);
+ uint32_t newSize = docIdLimit + (docIdLimit * gs.getDocsGrowFactor()) + gs.getDocsGrowDelta();
+ bool incGen = _bv.reserve(newSize);
+ if (incGen) {
+ incGeneration();
+ }
}
- incGen = _bv.extend(needSize) || incGen;
+}
+
+bool
+SingleBoolAttribute::addDoc(DocId & doc) {
+ DocId docIdLimit = getNumDocs()+1;
+ ensureRoom(docIdLimit);
+ bool incGen = _bv.extend(docIdLimit);
+ assert( ! incGen);
incNumDocs();
doc = getNumDocs() - 1;
updateUncommittedDocIdLimit(doc);
- if (incGen) {
- incGeneration();
- } else {
- removeAllOldGenerations();
- }
+ removeAllOldGenerations();
return true;
}
@@ -85,7 +89,7 @@ SingleBoolAttribute::onCommit() {
void
SingleBoolAttribute::onAddDocs(DocId docIdLimit) {
- _bv.reserve(docIdLimit);
+ ensureRoom(docIdLimit);
}
void
diff --git a/searchlib/src/vespa/searchlib/attribute/singleboolattribute.h b/searchlib/src/vespa/searchlib/attribute/singleboolattribute.h
index 789948838cb..20ec0b6d077 100644
--- a/searchlib/src/vespa/searchlib/attribute/singleboolattribute.h
+++ b/searchlib/src/vespa/searchlib/attribute/singleboolattribute.h
@@ -101,6 +101,7 @@ protected:
return false;
}
private:
+ void ensureRoom(DocId docIdLimit);
int8_t getFromEnum(EnumHandle) const override {
return 0;
}
diff --git a/searchlib/src/vespa/searchlib/common/allocatedbitvector.cpp b/searchlib/src/vespa/searchlib/common/allocatedbitvector.cpp
index dcee48aed1a..0141576c0c8 100644
--- a/searchlib/src/vespa/searchlib/common/allocatedbitvector.cpp
+++ b/searchlib/src/vespa/searchlib/common/allocatedbitvector.cpp
@@ -9,14 +9,25 @@ using vespalib::GenerationHeldBase;
using vespalib::GenerationHeldAlloc;
using vespalib::GenerationHolder;
-//////////////////////////////////////////////////////////////////////
-// Parameterized Constructor
-//////////////////////////////////////////////////////////////////////
+namespace {
+
+size_t computeCapacity(size_t capacity, size_t allocatedBytes) {
+ size_t possibleCapacity = (allocatedBytes * 8) - 1;
+ if (possibleCapacity < capacity) {
+ printf("capacity=%ld bytes=%ld possible=%ld\n", capacity, allocatedBytes, possibleCapacity);
+ }
+ assert(possibleCapacity >= capacity);
+ return possibleCapacity;
+}
+
+}
+
AllocatedBitVector::AllocatedBitVector(Index numberOfElements) :
BitVector(),
_capacityBits(numberOfElements),
_alloc(allocatePaddedAndAligned(numberOfElements))
{
+ _capacityBits = computeCapacity(_capacityBits, _alloc.size());
init(_alloc.get(), 0, numberOfElements);
clear();
}
@@ -33,6 +44,7 @@ AllocatedBitVector::AllocatedBitVector(Index numberOfElements, Index capacityBit
_capacityBits(capacityBits),
_alloc(allocatePaddedAndAligned(0, numberOfElements, capacityBits))
{
+ _capacityBits = computeCapacity(_capacityBits, _alloc.size());
init(_alloc.get(), 0, numberOfElements);
clear();
if (rhsSize > 0) {
@@ -58,6 +70,7 @@ AllocatedBitVector::AllocatedBitVector(const BitVector & rhs, Index capacity_) :
_capacityBits(capacity_),
_alloc(allocatePaddedAndAligned(0, rhs.size(), capacity_))
{
+ _capacityBits = computeCapacity(_capacityBits, _alloc.size());
memcpy(_alloc.get(), rhs.getStart(), rhs.sizeBytes());
init(_alloc.get(), 0, rhs.size());
}
@@ -65,7 +78,7 @@ AllocatedBitVector::AllocatedBitVector(const BitVector & rhs, Index capacity_) :
//////////////////////////////////////////////////////////////////////
// Destructor
//////////////////////////////////////////////////////////////////////
-AllocatedBitVector::~AllocatedBitVector() { }
+AllocatedBitVector::~AllocatedBitVector() = default;
void
AllocatedBitVector::cleanup()
@@ -78,8 +91,8 @@ AllocatedBitVector::cleanup()
void
AllocatedBitVector::resize(Index newLength)
{
- _capacityBits = newLength;
- _alloc = allocatePaddedAndAligned(_capacityBits);
+ _alloc = allocatePaddedAndAligned(newLength);
+ _capacityBits = computeCapacity(newLength, _alloc.size());
init(_alloc.get(), 0, newLength);
clear();
}
diff --git a/searchlib/src/vespa/searchlib/common/allocatedbitvector.h b/searchlib/src/vespa/searchlib/common/allocatedbitvector.h
index 6de255c48c9..c52c52354a1 100644
--- a/searchlib/src/vespa/searchlib/common/allocatedbitvector.h
+++ b/searchlib/src/vespa/searchlib/common/allocatedbitvector.h
@@ -32,7 +32,7 @@ public:
AllocatedBitVector(Index numberOfElements, Alloc buffer, size_t offset);
/**
- * Creates a new bitvector with room for numberOfElements bits.
+ * Creates a new bitvector with size of numberOfElements bits and at least a capacity of capacity.
* Copies what it can from the original vector. This is used for extending vector.
*/
AllocatedBitVector(Index numberOfElements, Index capacity, const void * rhsBuf, size_t rhsSize);
diff --git a/searchlib/src/vespa/searchlib/common/growablebitvector.h b/searchlib/src/vespa/searchlib/common/growablebitvector.h
index ff5d878063d..e13e3b42e3d 100644
--- a/searchlib/src/vespa/searchlib/common/growablebitvector.h
+++ b/searchlib/src/vespa/searchlib/common/growablebitvector.h
@@ -9,8 +9,7 @@ namespace search {
class GrowableBitVector : public AllocatedBitVector
{
public:
- GrowableBitVector(Index newSize, Index newCapacity,
- GenerationHolder &generationHolder);
+ GrowableBitVector(Index newSize, Index newCapacity, GenerationHolder &generationHolder);
/** Will return true if a a buffer is held */
bool reserve(Index newCapacity);
diff --git a/searchlib/src/vespa/searchlib/common/partialbitvector.cpp b/searchlib/src/vespa/searchlib/common/partialbitvector.cpp
index e57396c0dfa..e1dc144541e 100644
--- a/searchlib/src/vespa/searchlib/common/partialbitvector.cpp
+++ b/searchlib/src/vespa/searchlib/common/partialbitvector.cpp
@@ -29,6 +29,6 @@ PartialBitVector::PartialBitVector(const BitVector & org, Index start, Index end
setBit(size());
}
-PartialBitVector::~PartialBitVector() { }
+PartialBitVector::~PartialBitVector() = default;
} // namespace search