Skip to content

Commit

Permalink
Revert "reduce allocation size changes for resizeable array (#3326)" (#…
Browse files Browse the repository at this point in the history
…3343)

This reverts commit 3be9a7d.
  • Loading branch information
m-m-adams authored Feb 7, 2025
1 parent b312e75 commit d3607a3
Showing 1 changed file with 11 additions and 17 deletions.
28 changes: 11 additions & 17 deletions src/deluge/util/container/array/resizeable_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
#include "io/debug/log.h"
#include "memory/general_memory_allocator.h"
#include "util/functions.h"

#include <ranges>
#include <string.h>

#if RESIZEABLE_ARRAY_DO_LOCKS
Expand Down Expand Up @@ -209,7 +207,7 @@ void ResizeableArray::swapStateWith(ResizeableArray* other) {
}

void ResizeableArray::attemptMemoryShorten() {
if (staticMemoryAllocationSize || !emptyingShouldFreeMemory) {
if (staticMemoryAllocationSize) {
return;
}
if ((uint32_t)memoryAllocationStart >= (uint32_t)INTERNAL_MEMORY_BEGIN) {
Expand All @@ -218,7 +216,7 @@ void ResizeableArray::attemptMemoryShorten() {

uint32_t allocatedSize = GeneralMemoryAllocator::get().getAllocatedSize(memoryAllocationStart);

if (allocatedSize > (std::max(memorySize + maxNumEmptySpacesToKeep, memorySize * 2)) * elementSize) {
if (allocatedSize > (memorySize + maxNumEmptySpacesToKeep) * elementSize) {
int32_t extraSpaceLeft = (uint32_t)memory - (uint32_t)memoryAllocationStart;

int32_t extraSpaceRight = allocatedSize - extraSpaceLeft - memorySize * elementSize;
Expand Down Expand Up @@ -324,7 +322,7 @@ void ResizeableArray::deleteAtIndex(int32_t i, int32_t numToDelete, bool mayShor

// If we're not going to end up with more free memory than we're allowed, then the best option is to do
// the most basic delete
if (freeMemory < memorySize / 2) {
if (freeMemory < maxNumEmptySpacesToKeep) {
goto mostBasicDelete;
}

Expand Down Expand Up @@ -528,9 +526,8 @@ bool ResizeableArray::ensureEnoughSpaceAllocated(int32_t numAdditionalElementsNe
if (getRandom255() < 10)
goto getBrandNewMemory;
#endif
auto ideal =
std::max<int32_t>(((newNum + numExtraSpacesToAllocate) * elementSize) - allocatedSize, allocatedSize);
GeneralMemoryAllocator::get().extend(memoryAllocationStart, ((newNum)*elementSize) - allocatedSize, ideal,
GeneralMemoryAllocator::get().extend(memoryAllocationStart, (newNum)*elementSize - allocatedSize,
(newNum + numExtraSpacesToAllocate) * elementSize - allocatedSize,
&amountExtendedLeft, &amountExtendedRight);

// If successfully extended...
Expand Down Expand Up @@ -943,10 +940,8 @@ Error ResizeableArray::insertAtIndex(int32_t i, int32_t numToInsert, void* thing

// If not enough memory...
if (newNum > memorySize) {
auto ideal = std::max<int32_t>((numToInsert + numExtraSpacesToAllocate) - memorySize, memorySize);

bool success =
attemptMemoryExpansion(numToInsert, ideal, !staticMemoryAllocationSize, thingNotToStealFrom);
bool success = attemptMemoryExpansion(numToInsert, numToInsert + numExtraSpacesToAllocate,
!staticMemoryAllocationSize, thingNotToStealFrom);
if (!success) {
goto getBrandNewMemory;
}
Expand Down Expand Up @@ -994,8 +989,7 @@ Error ResizeableArray::insertAtIndex(int32_t i, int32_t numToInsert, void* thing
else {

// ... and we can't extend memory...
if (!attemptMemoryExpansion(numToInsert,
std::max(memorySize, numToInsert + numExtraSpacesToAllocate),
if (!attemptMemoryExpansion(numToInsert, numToInsert + numExtraSpacesToAllocate,
!staticMemoryAllocationSize, thingNotToStealFrom)) {

// Only choice is to get brand new memory
Expand All @@ -1008,8 +1002,9 @@ Error ResizeableArray::insertAtIndex(int32_t i, int32_t numToInsert, void* thing
else {

// If can't or won't grab some extra memory at the wrap-point...
if (!attemptMemoryExpansion(numToInsert, std::max(memorySize, numToInsert + numExtraSpacesToAllocate),
if (!attemptMemoryExpansion(numToInsert, numToInsert + numExtraSpacesToAllocate,
!staticMemoryAllocationSize, thingNotToStealFrom)) {

// If we do actually have enough memory, working "normally" is still an option, and it's now the
// best option
if (newNum <= memorySize) {
Expand Down Expand Up @@ -1094,8 +1089,7 @@ Error ResizeableArray::insertAtIndex(int32_t i, int32_t numToInsert, void* thing
// D_PRINTLN("getting new memory");

// Otherwise, manually get some brand new memory and do a more complex copying process

uint32_t desiredSize = (std::max(2 * memorySize, newNum + numExtraSpacesToAllocate)) * elementSize;
uint32_t desiredSize = (newNum + numExtraSpacesToAllocate) * elementSize;

getBrandNewMemoryAgain:
uint32_t allocatedSize = desiredSize;
Expand Down

0 comments on commit d3607a3

Please sign in to comment.