From a7d1c48ca3162a805708d059da87ca3d742863f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20L=C3=B6hning?= Date: Mon, 18 Oct 2021 15:00:09 +0200 Subject: QVarLengthArray: Reduce memory allocations in emplace() Currently, we allocate memory for elements one by one which can get pretty slow when adding many elements. [ChangeLog][QtCore][QVarLengthArray] Reduced number of memory allocations in emplace() by allocating more memory at once. Fixes: QTBUG-97489 Pick-to: 6.2 Change-Id: Idfb5b5946b047d5215c8ed00770574249f9f5d40 Reviewed-by: Andrei Golubev Reviewed-by: Allan Sandfeld Jensen Reviewed-by: Qt CI Bot Reviewed-by: Thiago Macieira --- .../tools/qvarlengtharray/tst_qvarlengtharray.cpp | 27 ++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'tests/auto') diff --git a/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp b/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp index 6e95577655..1d30ca3d03 100644 --- a/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp +++ b/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp @@ -180,6 +180,33 @@ void tst_QVarLengthArray::emplace() QCOMPARE(&r, &strings.back()); QCOMPARE(strings.size(), 3); QCOMPARE(strings.back(), QString(42, u'y')); + + // test growing from empty arrays + QVarLengthArray emptyArrDefaultPrealloc; + QCOMPARE(emptyArrDefaultPrealloc.size(), 0); + emptyArrDefaultPrealloc.emplace_back(); + QCOMPARE(emptyArrDefaultPrealloc.size(), 1); + emptyArrDefaultPrealloc.resize(1024); + QCOMPARE(emptyArrDefaultPrealloc.size(), 1024); + emptyArrDefaultPrealloc.resize(0); + QCOMPARE(emptyArrDefaultPrealloc.size(), 0); + emptyArrDefaultPrealloc.squeeze(); + QCOMPARE(emptyArrDefaultPrealloc.size(), 0); + emptyArrDefaultPrealloc.emplace_back(); + QCOMPARE(emptyArrDefaultPrealloc.size(), 1); + + QVarLengthArray emptyArrSmallPrealloc; + QCOMPARE(emptyArrSmallPrealloc.size(), 0); + emptyArrSmallPrealloc.emplace_back(); + QCOMPARE(emptyArrSmallPrealloc.size(), 1); + emptyArrSmallPrealloc.resize(1024); + QCOMPARE(emptyArrSmallPrealloc.size(), 1024); + emptyArrSmallPrealloc.resize(0); + QCOMPARE(emptyArrSmallPrealloc.size(), 0); + emptyArrSmallPrealloc.squeeze(); + QCOMPARE(emptyArrSmallPrealloc.size(), 0); + emptyArrSmallPrealloc.emplace_back(); + QCOMPARE(emptyArrSmallPrealloc.size(), 1); } } -- cgit v1.2.3