From ed4939eaac529e40a52762118ddbf153c2bf0c16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Abecasis?= Date: Mon, 30 Jul 2012 00:33:18 +0200 Subject: Improve performance of QArrayData::Grow autotest Doing element-wise insertions for the full range of the test made testing under valgrind extremely slow. When a reallocation is detected we now resize() the container close to capacity(), while verifying this doesn't unnecessarily re-allocate either. Change-Id: Idf7015cf390e366fe444e7ca14c904a2d54ff48b Reviewed-by: Thiago Macieira --- .../corelib/tools/qarraydata/tst_qarraydata.cpp | 31 ++++++++++++++++++---- 1 file changed, 26 insertions(+), 5 deletions(-) (limited to 'tests') diff --git a/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp b/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp index d3cc92080d..dea777d652 100644 --- a/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp +++ b/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp @@ -1654,26 +1654,47 @@ void tst_QArrayData::grow() SimpleVector vector; QCOMPARE(vector.size(), size_t(0)); + QCOMPARE(vector.capacity(), size_t(0)); - size_t previousCapacity = vector.capacity(); + size_t previousCapacity = 0; size_t allocations = 0; - for (size_t i = 1; i <= (1 << 20); ++i) { + for (size_t i = 1; i < (1 << 20); ++i) { int source[1] = { int(i) }; vector.append(source, source + 1); QCOMPARE(vector.size(), i); if (vector.capacity() != previousCapacity) { + // Don't re-allocate until necessary + QVERIFY(previousCapacity < i); + previousCapacity = vector.capacity(); ++allocations; + + // Going element-wise is slow under valgrind + if (previousCapacity - i > 10) { + i = previousCapacity - 5; + vector.back() = -i; + vector.resize(i); + + // It's still not the time to re-allocate + QCOMPARE(vector.capacity(), previousCapacity); + } } } - QCOMPARE(vector.size(), size_t(1 << 20)); + QVERIFY(vector.size() >= size_t(1 << 20)); // QArrayData::Grow prevents excessive allocations on a growing container QVERIFY(allocations > 20 / 2); QVERIFY(allocations < 20 * 2); - for (size_t i = 0; i < (1 << 20); ++i) - QCOMPARE(const_(vector).at(i), int(i + 1)); + for (size_t i = 0; i < vector.size(); ++i) { + int value = const_(vector).at(i); + if (value < 0) { + i = -value; + continue; + } + + QCOMPARE(value, int(i + 1)); + } } QTEST_APPLESS_MAIN(tst_QArrayData) -- cgit v1.2.3