Skip to content

Commit

Permalink
extend() avoids fetching descriptor twice.
Browse files Browse the repository at this point in the history
  • Loading branch information
dsm9000 committed Aug 24, 2023
1 parent 484d3f2 commit 6983b9e
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions sdlib/d/gc/tcache.d
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,32 @@ public:
return false;
}

auto pd = maybeGetPageDescriptor(slice.ptr);
if (pd.extent is null) {
return false;
}

// Appendable slabs are not supported.
if (pd.isSlab()) {
return false;
}

// Slice must not end before valid data ends, or capacity is zero:
auto startIndex = slice.ptr - pd.extent.address;
auto stopIndex = startIndex + slice.length;

// If the slice end doesn't match the used capacity, bail.
if (stopIndex != pd.extent.usedCapacity) {
return false;
}

// There must be sufficient free space to extend into:
if (getCapacity(slice) < slice.length + length) {
auto capacity = pd.extent.size - startIndex;
if (capacity < slice.length + length) {
return false;
}

// Increase the used capacity by the requested length:
auto pd = maybeGetPageDescriptor(slice.ptr);
pd.extent.setUsedCapacity(pd.extent.usedCapacity + length);

return true;
Expand Down

0 comments on commit 6983b9e

Please sign in to comment.