QVarLengthArray: optimize pop_back()
Don't call realloc() with all its machinery when we know exactly what to do: destroy the last element and decrease the size by one. Extend the test, removing the unused Foo class for a new Tracker one. Change-Id: I568eef4f6335669689fb16fd23af92cb4d6464bd Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>bb10
parent
944c5f40b3
commit
205824103e
|
|
@ -112,7 +112,9 @@ public:
|
|||
|
||||
inline void removeLast() {
|
||||
Q_ASSERT(s > 0);
|
||||
realloc(s - 1, a);
|
||||
if (QTypeInfo<T>::isComplex)
|
||||
ptr[s - 1].~T();
|
||||
--s;
|
||||
}
|
||||
inline int size() const { return s; }
|
||||
inline int count() const { return s; }
|
||||
|
|
|
|||
|
|
@ -64,21 +64,21 @@ private:
|
|||
void initializeList();
|
||||
};
|
||||
|
||||
int fooCtor = 0;
|
||||
int fooDtor = 0;
|
||||
|
||||
struct Foo
|
||||
struct Tracker
|
||||
{
|
||||
int *p;
|
||||
static int count;
|
||||
Tracker() { ++count; }
|
||||
Tracker(const Tracker &) { ++count; }
|
||||
Tracker(Tracker &&) { ++count; }
|
||||
|
||||
Foo() { p = new int; ++fooCtor; }
|
||||
Foo(const Foo &/*other*/) { p = new int; ++fooCtor; }
|
||||
Tracker &operator=(const Tracker &) = default;
|
||||
Tracker &operator=(Tracker &&) = default;
|
||||
|
||||
void operator=(const Foo & /* other */) { }
|
||||
|
||||
~Foo() { delete p; ++fooDtor; }
|
||||
~Tracker() { --count; }
|
||||
};
|
||||
|
||||
int Tracker::count = 0;
|
||||
|
||||
void tst_QVarLengthArray::append()
|
||||
{
|
||||
QVarLengthArray<QString, 2> v;
|
||||
|
|
@ -130,6 +130,23 @@ void tst_QVarLengthArray::removeLast()
|
|||
v.removeLast();
|
||||
QCOMPARE(v.size(), 2);
|
||||
}
|
||||
|
||||
{
|
||||
Tracker t;
|
||||
QCOMPARE(Tracker::count, 1);
|
||||
QVarLengthArray<Tracker, 2> v;
|
||||
v.append(t);
|
||||
v.append({});
|
||||
QCOMPARE(Tracker::count, 3);
|
||||
v.removeLast();
|
||||
QCOMPARE(Tracker::count, 2);
|
||||
v.append(t);
|
||||
v.append({});
|
||||
QCOMPARE(Tracker::count, 4);
|
||||
v.removeLast();
|
||||
QCOMPARE(Tracker::count, 3);
|
||||
}
|
||||
QCOMPARE(Tracker::count, 0);
|
||||
}
|
||||
|
||||
void tst_QVarLengthArray::oldTests()
|
||||
|
|
|
|||
Loading…
Reference in New Issue