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);