From c134adc3b15577628543ca44b500d8dc041fb830 Mon Sep 17 00:00:00 2001 From: ToyB-Chan Date: Mon, 8 Jun 2026 07:47:44 +0200 Subject: [PATCH] performance speedup --- minimal_heap.c | 22 ++++++++++++++++++++-- minimal_heap.h | 4 +++- minimal_heap_test.c | 4 ++-- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/minimal_heap.c b/minimal_heap.c index fa40da2..c80dff9 100644 --- a/minimal_heap.c +++ b/minimal_heap.c @@ -24,6 +24,7 @@ MIHP_HeapInfo* MIHP_CreateHeap(MIHP_HeapConfig config) heap->Config = config; heap->FirstArea = MIHP_CreateHeapMemoryArea(heap, config.MinimalMemoryAreaSize); + heap->LastAllocationArea = heap->FirstArea; if (heap->FirstArea == NULL) { MIHP_PlatformFreeMemory(heap, sizeof(MIHP_HeapInfo)); @@ -70,12 +71,24 @@ void* MIHP_Allocate(MIHP_HeapInfo* heap, size_t size) MIHP_LOCK_HEAP(&heap->HeapLock); + bool checkedLastAllocationArea = false; MIHP_HeapMemoryAreaHeader* nextArea = heap->FirstArea; while (nextArea) { - MIHP_HeapMemoryAreaHeader* currentArea = nextArea; + MIHP_HeapMemoryAreaHeader* currentArea = NULL; + + if (!checkedLastAllocationArea) + { + currentArea = heap->LastAllocationArea; + checkedLastAllocationArea = true; + } + else + { + currentArea = nextArea; + nextArea = currentArea->NextArea; + } + MIHP_ValidateHeapMemoryAreaHeader(heap, currentArea); - nextArea = currentArea->NextArea; if (currentArea->NumOccupiedSegments == currentArea->NumSegments) continue; @@ -108,6 +121,8 @@ void* MIHP_Allocate(MIHP_HeapInfo* heap, size_t size) currentArea->Checksum = MIHP_GenerateHeapMemoryAreaChecksum(currentArea); heap->Stats.NumTotalOccupiedSegments++; + heap->Stats.TotalOccupiedSize += targetSegment->OccupiedSize; + heap->LastAllocationArea = currentArea; MIHP_UNLOCK_HEAP(&heap->HeapLock); @@ -163,6 +178,8 @@ bool MIHP_Free(MIHP_HeapInfo* heap, void* ptr) MIHP_HeapMemoryAreaHeader* area = segment->OwningMemoryArea; MIHP_ValidateHeapMemoryAreaHeader(heap, area); + size_t occupiedSize = segment->OccupiedSize; + segment->OccupiedSize = 0; segment->Checksum = MIHP_GenerateHeapSegmentChecksum(segment); @@ -170,6 +187,7 @@ bool MIHP_Free(MIHP_HeapInfo* heap, void* ptr) area->Checksum = MIHP_GenerateHeapMemoryAreaChecksum(area); heap->Stats.NumTotalOccupiedSegments--; + heap->Stats.TotalOccupiedSize -= occupiedSize; if (area->NumOccupiedSegments == 0 && heap->Stats.NumMemoryAreas > 1) { diff --git a/minimal_heap.h b/minimal_heap.h index b722674..dee7193 100644 --- a/minimal_heap.h +++ b/minimal_heap.h @@ -28,6 +28,7 @@ typedef struct _MIHP_HeapStats size_t NumTotalSegments; size_t NumTotalOccupiedSegments; size_t TotalSize; + size_t TotalOccupiedSize; } MIHP_HeapStats; // This structs size must be multiple of 8 @@ -73,7 +74,7 @@ typedef struct _MIHP_HeapCorruptionInfo } MIHP_HeapCorruptionInfo; /* Hooks to be implemented by the library user */ -void* MIHP_PlatformRequestMemory(size_t size); // Alignment must at least be alingment of size_t +void* MIHP_PlatformRequestMemory(size_t size); // Alignment must at least be alignment of size_t bool MIHP_PlatformFreeMemory(void* ptr, size_t size); void MIHP_PlatformOnHeapCorruptionDetected(MIHP_HeapCorruptionInfo info); /* === */ @@ -93,6 +94,7 @@ typedef struct _MIHP_HeapInfo MIHP_HeapConfig Config; MIHP_HeapStats Stats; MIHP_HeapMemoryAreaHeader* FirstArea; + MIHP_HeapMemoryAreaHeader* LastSuccessfulAllocationArea; MIHP_HEAP_LOCK_TYPE HeapLock; } MIHP_HeapInfo; diff --git a/minimal_heap_test.c b/minimal_heap_test.c index 151f29e..52d76aa 100644 --- a/minimal_heap_test.c +++ b/minimal_heap_test.c @@ -21,7 +21,7 @@ void MIHP_PlatformOnHeapCorruptionDetected(MIHP_HeapCorruptionInfo info) __debugbreak(); } -#define NUM_ALLOCATIONS (4096 * 512) +#define NUM_ALLOCATIONS (4096 * 128) void* myptrs[NUM_ALLOCATIONS] = { 0 }; @@ -31,7 +31,7 @@ int main() config.AllocationInitialValue = 0xbe; config.AllocationAlignment = 16; config.MinimalMemoryAreaSize = 4096 * 32; - config.MaximalMemoryAreaSize = 4096ull * 4096ull * 4096ull; + config.MaximalMemoryAreaSize = 4096ull * 4096ull; MIHP_HeapInfo* myHeap = MIHP_CreateHeap(config); for (int i = 0; i < NUM_ALLOCATIONS; i++)