From 8a984ab772dd194e39094e728b869e65912912a7 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Fri, 7 Feb 2020 13:15:50 +0100 Subject: [PATCH] Deduplicate some code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Requires one more branch inside the loop, but that should not really matter performance wise. And it should expand to less code. Change-Id: I4619dd2a2e6fedf8d109009a5b6d7410ed89f1fb Reviewed-by: MÃ¥rten Nordheim --- src/corelib/tools/qhash.h | 40 ++++++--------------------------------- 1 file changed, 6 insertions(+), 34 deletions(-) diff --git a/src/corelib/tools/qhash.h b/src/corelib/tools/qhash.h index 6b0b6525ace..be2f1537fe4 100644 --- a/src/corelib/tools/qhash.h +++ b/src/corelib/tools/qhash.h @@ -439,33 +439,14 @@ struct Data spans = new Span[nSpans]; seed = qGlobalQHashSeed(); } - Data(const Data &other) + Data(const Data &other, size_t reserved = 0) : size(other.size), numBuckets(other.numBuckets), seed(other.seed) { - size_t nSpans = (other.numBuckets + Span::LocalBucketMask) / Span::NEntries; - spans = new Span[nSpans]; - - for (size_t s = 0; s < nSpans; ++s) { - const Span &span = other.spans[s]; - for (size_t index = 0; index < Span::NEntries; ++index) { - if (!span.hasNode(index)) - continue; - const Node &n = span.at(index); - iterator it{ this, s*Span::NEntries + index }; - Q_ASSERT(it.isUnused()); - - Node *newNode = spans[it.span()].insert(it.index()); - new (newNode) Node(n); - } - } - } - Data(const Data &other, size_t reserved) - : size(other.size), - seed(other.seed) - { - numBuckets = GrowthPolicy::bucketsForCapacity(qMax(size, reserved)); + if (reserved) + numBuckets = GrowthPolicy::bucketsForCapacity(qMax(size, reserved)); + bool resized = numBuckets != other.numBuckets; size_t nSpans = (numBuckets + Span::LocalBucketMask) / Span::NEntries; spans = new Span[nSpans]; @@ -475,7 +456,7 @@ struct Data if (!span.hasNode(index)) continue; const Node &n = span.at(index); - iterator it = find(n.key); + iterator it = resized ? find(n.key) : iterator{ this, s*Span::NEntries + index }; Q_ASSERT(it.isUnused()); Node *newNode = spans[it.span()].insert(it.index()); new (newNode) Node(n); @@ -483,16 +464,7 @@ struct Data } } - static Data *detached(Data *d) - { - if (!d) - return new Data; - Data *dd = new Data(*d); - if (!d->ref.deref()) - delete d; - return dd; - } - static Data *detached(Data *d, size_t size) + static Data *detached(Data *d, size_t size = 0) { if (!d) return new Data(size);