[*] Bytebuf resize: Fix perf regression in 6d49de43

This commit is contained in:
Reece Wilson 2023-10-23 07:49:18 +01:00
parent a7af61e9bf
commit a5b7ea9af4

View File

@ -200,26 +200,48 @@ namespace Aurora::Memory
return Allocate(length, false);
}
auto uOldHead = this->readPtr - this->base;
auto uOldWrite = this->writePtr - this->base;
if (!this->flagCircular)
{
if (this->allocSize >= length)
{
this->length = length;
return true;
}
auto newLength = AuMax(length, (this->allocSize / 4) * 3);
if (auto pNext = ZRealloc(this->base, newLength))
{
this->base = pNext;
this->length = length;
this->allocSize = newLength;
this->readPtr = uOldHead + this->base;
this->writePtr = uOldWrite + this->base;
return true;
}
}
ByteBuffer replacement(length, (bool)this->flagCircular, (bool)this->flagExpandable);
if (!replacement)
{
return false;
}
replacement.flagAlwaysExpandable = this->flagAlwaysExpandable;
auto pOldHead = this->readPtr;
auto pOldWrite = this->writePtr;
AuUInt uBytesRem {};
if (!this->flagCircular && length < (uBytesRem = this->RemainingBytes()))
{
this->writePtr = pOldHead + uBytesRem;
this->writePtr = (uOldHead + uBytesRem) + this->base;
}
if (!replacement.WriteFrom(*this))
{
this->readPtr = pOldHead;
this->writePtr = pOldWrite;
this->readPtr = uOldHead + this->base;
this->writePtr = uOldWrite + this->base;
return false;
}