summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLars Knoll <lars.knoll@qt.io>2020-02-07 13:15:50 +0100
committerLars Knoll <lars.knoll@qt.io>2020-04-09 20:04:07 +0200
commit8a984ab772dd194e39094e728b869e65912912a7 (patch)
treebf30eabecca1f202be20f58c487e8872b521b95a
parent73ddfa25b41ddf475b70b7089e3b4d2402494f01 (diff)
Deduplicate some code
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 <marten.nordheim@qt.io>
-rw-r--r--src/corelib/tools/qhash.h40
1 files changed, 6 insertions, 34 deletions
diff --git a/src/corelib/tools/qhash.h b/src/corelib/tools/qhash.h
index 6b0b6525ac..be2f1537fe 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);