From 6f6c8d061803a3c89f260fa234388dc27c308bce Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Sun, 16 Jan 2022 13:17:16 +0100 Subject: QVersionNumber: don't allocate in fromString() in the common case Use QVarLengthArray instead of QList to avoid allocations in the case where the result fits into inline storage. Of course, we now perform one allocation more when we need the QList backend, but a) that should be the rarer case and b) we use 32 (more than InlineSegmentsCount) Prealloc for the QVarLengthArray to reliably skip the first few QList non-reserved capacity jumps (measured to be {4, 12, 28} for back-insertion, not all of which is probably available for append()s) to come out ahead either way. Reviewers may object, saying that we could just reserve(32) the QList, too, but while that would skip over the first few QList reallocations alright, it means we'd be carrying the extra capacity around for the duration of the QVersionNumber's lifetime (unless we'd shrink it at the end, bringing back the additional allocation and that solution back to par compared to this one). As a consequence: [ChangeLog][QtCore][QVersionNumber] Can now be also be constructed from QVarLengthArray. Change-Id: I4016367f64f6cefa6ed9147d33b06636f36b02cb Reviewed-by: Thiago Macieira --- src/corelib/tools/qversionnumber.h | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'src/corelib/tools/qversionnumber.h') diff --git a/src/corelib/tools/qversionnumber.h b/src/corelib/tools/qversionnumber.h index ad8367c135..f00ce36437 100644 --- a/src/corelib/tools/qversionnumber.h +++ b/src/corelib/tools/qversionnumber.h @@ -141,12 +141,15 @@ class QVersionNumber else pointer_segments = new QList(std::move(seg)); } - SegmentStorage(std::initializer_list args) + explicit SegmentStorage(std::initializer_list args) + : SegmentStorage(args.begin(), args.end()) {} + + explicit SegmentStorage(const int *first, const int *last) { - if (dataFitsInline(std::data(args), args.size())) { - setInlineData(std::data(args), args.size()); + if (dataFitsInline(first, last - first)) { + setInlineData(first, last - first); } else { - pointer_segments = new QList(args); + pointer_segments = new QList(first, last); } } @@ -234,6 +237,11 @@ public: : m_segments(args) {} + template + explicit QVersionNumber(const QVarLengthArray &sec) + : m_segments(sec.begin(), sec.end()) + {} + inline explicit QVersionNumber(int maj) { m_segments.setSegments(1, maj); } -- cgit v1.2.3