mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 14:54:07 +10:00
Benchmarked memory models for Toy_Array, read more
The results can be found in 'tests/benchmarks/array_allocation/results.md' The results are disappointing, as 'malloc()' is simply faster in every possible situation compared to my custom arena allocator.
This commit is contained in:
34
tests/benchmarks/array_allocation/toy_array_malloc.c
Normal file
34
tests/benchmarks/array_allocation/toy_array_malloc.c
Normal file
@@ -0,0 +1,34 @@
|
||||
#include "toy_array.h"
|
||||
#include "toy_console_colors.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
Toy_Array* Toy_resizeArray(Toy_Array* paramArray, unsigned int capacity) {
|
||||
if (capacity == 0) {
|
||||
free(paramArray);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
//if some values will be removed, free them first
|
||||
if (paramArray != NULL && paramArray->count > capacity) {
|
||||
for (unsigned int i = capacity; i < paramArray->count; i++) {
|
||||
Toy_freeValue(paramArray->data[i]);
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int originalCapacity = paramArray == NULL ? 0 : paramArray->capacity;
|
||||
|
||||
Toy_Array* array = realloc(paramArray, capacity * sizeof(Toy_Value) + sizeof(Toy_Array));
|
||||
|
||||
if (array == NULL) {
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Failed to resize a 'Toy_Array' from %d to %d capacity\n" TOY_CC_RESET, (int)originalCapacity, (int)capacity);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
array->capacity = capacity;
|
||||
array->count = paramArray == NULL ? 0 :
|
||||
(array->count > capacity ? capacity : array->count); //truncate lost data
|
||||
|
||||
return array;
|
||||
}
|
||||
Reference in New Issue
Block a user