Skip to content

Commit

Permalink
QHash: centralize the span allocation
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
thiagomacieira committed May 16, 2023
1 parent c6540cb commit 1acbcc3
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/corelib/tools/qhash.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
numBuckets = GrowthPolicy::bucketsForCapacity(reserve);
size_t nSpans = numBuckets >> SpanConstants::SpanShift;
spans = new Span[nSpans];
spans = allocateSpans(numBuckets).spans;
seed = QHashSeed::globalSeed();
}

Expand All @@ -557,15 +567,14 @@ struct Data

Data(const Data &other) : size(other.size), numBuckets(other.numBuckets), seed(other.seed)
{
size_t nSpans = numBuckets >> SpanConstants::SpanShift;
spans = new Span[nSpans];
reallocationHelper(other, nSpans, false);
auto r = allocateSpans(numBuckets);
spans = r.spans;
reallocationHelper(other, r.nSpans, false);
}
Data(const Data &other, size_t reserved) : size(other.size), seed(other.seed)
{
numBuckets = GrowthPolicy::bucketsForCapacity(qMax(size, reserved));
size_t nSpans = numBuckets >> SpanConstants::SpanShift;
spans = new Span[nSpans];
spans = allocateSpans(numBuckets).spans;
size_t otherNSpans = other.numBuckets >> SpanConstants::SpanShift;
reallocationHelper(other, otherNSpans, true);
}
Expand Down Expand Up @@ -623,8 +632,7 @@ struct Data

Span *oldSpans = spans;
size_t oldBucketCount = numBuckets;
size_t nSpans = newBucketCount >> SpanConstants::SpanShift;
spans = new Span[nSpans];
spans = allocateSpans(newBucketCount).spans;
numBuckets = newBucketCount;
size_t oldNSpans = oldBucketCount >> SpanConstants::SpanShift;

Expand Down

0 comments on commit 1acbcc3

Please sign in to comment.