QHash: centralize the span allocation

Deduplicates code and will allow me to insert some magic.

Pick-to: 6.5
Task-number: QTBUG-113335
Change-Id: Ieab617d69f3b4b54ab30fffd175bb4a2af610ff8
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
This commit is contained in:
Thiago Macieira 2023-05-03 11:11:36 -07:00
parent c6540cb6e4
commit 1acbcc318a

View File

@ -531,11 +531,21 @@ struct Data
} }
}; };
static auto allocateSpans(size_t numBuckets)
{
struct R {
Span *spans;
size_t nSpans;
};
size_t nSpans = numBuckets >> SpanConstants::SpanShift;
return R{ new Span[nSpans], nSpans };
}
Data(size_t reserve = 0) Data(size_t reserve = 0)
{ {
numBuckets = GrowthPolicy::bucketsForCapacity(reserve); numBuckets = GrowthPolicy::bucketsForCapacity(reserve);
size_t nSpans = numBuckets >> SpanConstants::SpanShift; spans = allocateSpans(numBuckets).spans;
spans = new Span[nSpans];
seed = QHashSeed::globalSeed(); seed = QHashSeed::globalSeed();
} }
@ -557,15 +567,14 @@ struct Data
Data(const Data &other) : size(other.size), numBuckets(other.numBuckets), seed(other.seed) Data(const Data &other) : size(other.size), numBuckets(other.numBuckets), seed(other.seed)
{ {
size_t nSpans = numBuckets >> SpanConstants::SpanShift; auto r = allocateSpans(numBuckets);
spans = new Span[nSpans]; spans = r.spans;
reallocationHelper(other, nSpans, false); reallocationHelper(other, r.nSpans, false);
} }
Data(const Data &other, size_t reserved) : size(other.size), seed(other.seed) Data(const Data &other, size_t reserved) : size(other.size), seed(other.seed)
{ {
numBuckets = GrowthPolicy::bucketsForCapacity(qMax(size, reserved)); numBuckets = GrowthPolicy::bucketsForCapacity(qMax(size, reserved));
size_t nSpans = numBuckets >> SpanConstants::SpanShift; spans = allocateSpans(numBuckets).spans;
spans = new Span[nSpans];
size_t otherNSpans = other.numBuckets >> SpanConstants::SpanShift; size_t otherNSpans = other.numBuckets >> SpanConstants::SpanShift;
reallocationHelper(other, otherNSpans, true); reallocationHelper(other, otherNSpans, true);
} }
@ -623,8 +632,7 @@ struct Data
Span *oldSpans = spans; Span *oldSpans = spans;
size_t oldBucketCount = numBuckets; size_t oldBucketCount = numBuckets;
size_t nSpans = newBucketCount >> SpanConstants::SpanShift; spans = allocateSpans(newBucketCount).spans;
spans = new Span[nSpans];
numBuckets = newBucketCount; numBuckets = newBucketCount;
size_t oldNSpans = oldBucketCount >> SpanConstants::SpanShift; size_t oldNSpans = oldBucketCount >> SpanConstants::SpanShift;