diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2019-06-11 12:50:01 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-06-11 12:50:01 +0200 |
commit | bf0f7ced58968e26eafa632cb68cfb3757d4dd2f (patch) | |
tree | bb753a8c37affcd19a122ad76f9ee4dad89932b2 | |
parent | 89e40d3684d48b94e82a7199d17b0e32dd07faab (diff) | |
parent | deb20312a19f5ace6bc38a703f5af534ec278769 (diff) |
Merge pull request #9694 from vespa-engine/balder/use-all-memory-we-got
Balder/use all memory we got.
8 files changed, 48 insertions, 33 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..3de0c1f1320 100644 --- a/searchlib/src/vespa/searchlib/common/allocatedbitvector.cpp +++ b/searchlib/src/vespa/searchlib/common/allocatedbitvector.cpp @@ -9,14 +9,22 @@ 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; + 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 +41,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 +67,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 +75,7 @@ AllocatedBitVector::AllocatedBitVector(const BitVector & rhs, Index capacity_) : ////////////////////////////////////////////////////////////////////// // Destructor ////////////////////////////////////////////////////////////////////// -AllocatedBitVector::~AllocatedBitVector() { } +AllocatedBitVector::~AllocatedBitVector() = default; void AllocatedBitVector::cleanup() @@ -78,8 +88,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/bitvector.cpp b/searchlib/src/vespa/searchlib/common/bitvector.cpp index 2c45bc8f69a..7296842f2c1 100644 --- a/searchlib/src/vespa/searchlib/common/bitvector.cpp +++ b/searchlib/src/vespa/searchlib/common/bitvector.cpp @@ -52,7 +52,7 @@ BitVector::allocatePaddedAndAligned(Index start, Index end, Index capacity) assert(alloc.size()/sizeof(Word) >= words); // Clear padding size_t usedBytes = numBytes(end - start); - memset(static_cast<char *>(alloc.get()) + usedBytes, 0, sz - usedBytes); + memset(static_cast<char *>(alloc.get()) + usedBytes, 0, alloc.size() - usedBytes); return alloc; } 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 |