SmallVector: Move, don't copy, elements to make space for an insertion.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@210432 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
David Blaikie 2014-06-08 19:12:28 +00:00
parent aa53e49779
commit aa72ac7dad
2 changed files with 40 additions and 1 deletions

View File

@ -555,7 +555,8 @@ public:
// reallocate the vector. // reallocate the vector.
if (size_t(this->end()-I) >= NumToInsert) { if (size_t(this->end()-I) >= NumToInsert) {
T *OldEnd = this->end(); T *OldEnd = this->end();
append(this->end()-NumToInsert, this->end()); append(std::move_iterator<iterator>(this->end() - NumToInsert),
std::move_iterator<iterator>(this->end()));
// Copy the existing elements that get replaced. // Copy the existing elements that get replaced.
this->move_backward(I, OldEnd-NumToInsert, OldEnd); this->move_backward(I, OldEnd-NumToInsert, OldEnd);

View File

@ -26,8 +26,12 @@ namespace {
class Constructable { class Constructable {
private: private:
static int numConstructorCalls; static int numConstructorCalls;
static int numMoveConstructorCalls;
static int numCopyConstructorCalls;
static int numDestructorCalls; static int numDestructorCalls;
static int numAssignmentCalls; static int numAssignmentCalls;
static int numMoveAssignmentCalls;
static int numCopyAssignmentCalls;
bool constructed; bool constructed;
int value; int value;
@ -44,11 +48,13 @@ public:
Constructable(const Constructable & src) : constructed(true) { Constructable(const Constructable & src) : constructed(true) {
value = src.value; value = src.value;
++numConstructorCalls; ++numConstructorCalls;
++numCopyConstructorCalls;
} }
Constructable(Constructable && src) : constructed(true) { Constructable(Constructable && src) : constructed(true) {
value = src.value; value = src.value;
++numConstructorCalls; ++numConstructorCalls;
++numMoveConstructorCalls;
} }
~Constructable() { ~Constructable() {
@ -61,6 +67,7 @@ public:
EXPECT_TRUE(constructed); EXPECT_TRUE(constructed);
value = src.value; value = src.value;
++numAssignmentCalls; ++numAssignmentCalls;
++numCopyAssignmentCalls;
return *this; return *this;
} }
@ -68,6 +75,7 @@ public:
EXPECT_TRUE(constructed); EXPECT_TRUE(constructed);
value = src.value; value = src.value;
++numAssignmentCalls; ++numAssignmentCalls;
++numMoveAssignmentCalls;
return *this; return *this;
} }
@ -77,18 +85,42 @@ public:
static void reset() { static void reset() {
numConstructorCalls = 0; numConstructorCalls = 0;
numMoveConstructorCalls = 0;
numCopyConstructorCalls = 0;
numDestructorCalls = 0; numDestructorCalls = 0;
numAssignmentCalls = 0; numAssignmentCalls = 0;
numMoveAssignmentCalls = 0;
numCopyAssignmentCalls = 0;
} }
static int getNumConstructorCalls() { static int getNumConstructorCalls() {
return numConstructorCalls; return numConstructorCalls;
} }
static int getNumMoveConstructorCalls() {
return numMoveConstructorCalls;
}
static int getNumCopyConstructorCalls() {
return numCopyConstructorCalls;
}
static int getNumDestructorCalls() { static int getNumDestructorCalls() {
return numDestructorCalls; return numDestructorCalls;
} }
static int getNumAssignmentCalls() {
return numAssignmentCalls;
}
static int getNumMoveAssignmentCalls() {
return numMoveAssignmentCalls;
}
static int getNumCopyAssignmentCalls() {
return numCopyAssignmentCalls;
}
friend bool operator==(const Constructable & c0, const Constructable & c1) { friend bool operator==(const Constructable & c0, const Constructable & c1) {
return c0.getValue() == c1.getValue(); return c0.getValue() == c1.getValue();
} }
@ -100,8 +132,12 @@ public:
}; };
int Constructable::numConstructorCalls; int Constructable::numConstructorCalls;
int Constructable::numCopyConstructorCalls;
int Constructable::numMoveConstructorCalls;
int Constructable::numDestructorCalls; int Constructable::numDestructorCalls;
int Constructable::numAssignmentCalls; int Constructable::numAssignmentCalls;
int Constructable::numCopyAssignmentCalls;
int Constructable::numMoveAssignmentCalls;
// Test fixture class // Test fixture class
template <typename VectorT> template <typename VectorT>
@ -430,8 +466,10 @@ TYPED_TEST(SmallVectorTest, InsertRepeatedTest) {
SCOPED_TRACE("InsertRepeatedTest"); SCOPED_TRACE("InsertRepeatedTest");
this->makeSequence(this->theVector, 10, 15); this->makeSequence(this->theVector, 10, 15);
Constructable::reset();
typename TypeParam::iterator I = typename TypeParam::iterator I =
this->theVector.insert(this->theVector.begin() + 1, 2, Constructable(16)); this->theVector.insert(this->theVector.begin() + 1, 2, Constructable(16));
EXPECT_EQ(0, Constructable::getNumCopyConstructorCalls());
EXPECT_EQ(this->theVector.begin() + 1, I); EXPECT_EQ(this->theVector.begin() + 1, I);
this->assertValuesInOrder(this->theVector, 8u, this->assertValuesInOrder(this->theVector, 8u,
10, 16, 16, 11, 12, 13, 14, 15); 10, 16, 16, 11, 12, 13, 14, 15);