mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 14:54:07 +10:00
Prevented NO-OP calls to the memory allocator
Also shaved off about 1-2 milliseconds of execution time of fib-memo.toy
This commit is contained in:
@@ -40,17 +40,21 @@ static void freeASTNodeCustom(Toy_ASTNode* node, bool freeSelf) {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case TOY_AST_NODE_BLOCK:
|
case TOY_AST_NODE_BLOCK:
|
||||||
|
if (node->block.capacity > 0) {
|
||||||
for (int i = 0; i < node->block.count; i++) {
|
for (int i = 0; i < node->block.count; i++) {
|
||||||
freeASTNodeCustom(node->block.nodes + i, false);
|
freeASTNodeCustom(node->block.nodes + i, false);
|
||||||
}
|
}
|
||||||
TOY_FREE_ARRAY(Toy_ASTNode, node->block.nodes, node->block.capacity);
|
TOY_FREE_ARRAY(Toy_ASTNode, node->block.nodes, node->block.capacity);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case TOY_AST_NODE_COMPOUND:
|
case TOY_AST_NODE_COMPOUND:
|
||||||
|
if (node->compound.capacity > 0) {
|
||||||
for (int i = 0; i < node->compound.count; i++) {
|
for (int i = 0; i < node->compound.count; i++) {
|
||||||
freeASTNodeCustom(node->compound.nodes + i, false);
|
freeASTNodeCustom(node->compound.nodes + i, false);
|
||||||
}
|
}
|
||||||
TOY_FREE_ARRAY(Toy_ASTNode, node->compound.nodes, node->compound.capacity);
|
TOY_FREE_ARRAY(Toy_ASTNode, node->compound.nodes, node->compound.capacity);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case TOY_AST_NODE_PAIR:
|
case TOY_AST_NODE_PAIR:
|
||||||
@@ -71,10 +75,12 @@ static void freeASTNodeCustom(Toy_ASTNode* node, bool freeSelf) {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case TOY_AST_NODE_FN_COLLECTION:
|
case TOY_AST_NODE_FN_COLLECTION:
|
||||||
|
if (node->fnCollection.capacity > 0) {
|
||||||
for (int i = 0; i < node->fnCollection.count; i++) {
|
for (int i = 0; i < node->fnCollection.count; i++) {
|
||||||
freeASTNodeCustom(node->fnCollection.nodes + i, false);
|
freeASTNodeCustom(node->fnCollection.nodes + i, false);
|
||||||
}
|
}
|
||||||
TOY_FREE_ARRAY(Toy_ASTNode, node->fnCollection.nodes, node->fnCollection.capacity);
|
TOY_FREE_ARRAY(Toy_ASTNode, node->fnCollection.nodes, node->fnCollection.capacity);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case TOY_AST_NODE_FN_DECL:
|
case TOY_AST_NODE_FN_DECL:
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
#define TOY_VERSION_MAJOR 1
|
#define TOY_VERSION_MAJOR 1
|
||||||
#define TOY_VERSION_MINOR 1
|
#define TOY_VERSION_MINOR 1
|
||||||
#define TOY_VERSION_PATCH 0
|
#define TOY_VERSION_PATCH 1
|
||||||
#define TOY_VERSION_BUILD __DATE__ " " __TIME__
|
#define TOY_VERSION_BUILD __DATE__ " " __TIME__
|
||||||
|
|
||||||
//platform/compiler-specific instructions
|
//platform/compiler-specific instructions
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ void Toy_freeLiteral(Toy_Literal literal) {
|
|||||||
TOY_FREE_ARRAY(unsigned char, TOY_AS_FUNCTION(literal).inner.bytecode, TOY_AS_FUNCTION_BYTECODE_LENGTH(literal));
|
TOY_FREE_ARRAY(unsigned char, TOY_AS_FUNCTION(literal).inner.bytecode, TOY_AS_FUNCTION_BYTECODE_LENGTH(literal));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (TOY_IS_TYPE(literal)) {
|
if (TOY_IS_TYPE(literal) && TOY_AS_TYPE(literal).capacity > 0) {
|
||||||
for (int i = 0; i < TOY_AS_TYPE(literal).count; i++) {
|
for (int i = 0; i < TOY_AS_TYPE(literal).count; i++) {
|
||||||
Toy_freeLiteral(((Toy_Literal*)(TOY_AS_TYPE(literal).subtypes))[i]);
|
Toy_freeLiteral(((Toy_Literal*)(TOY_AS_TYPE(literal).subtypes))[i]);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,9 +18,11 @@ void Toy_freeLiteralArray(Toy_LiteralArray* array) {
|
|||||||
Toy_freeLiteral(array->literals[i]);
|
Toy_freeLiteral(array->literals[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (array->capacity > 0) {
|
||||||
TOY_FREE_ARRAY(Toy_Literal, array->literals, array->capacity);
|
TOY_FREE_ARRAY(Toy_Literal, array->literals, array->capacity);
|
||||||
Toy_initLiteralArray(array);
|
Toy_initLiteralArray(array);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int Toy_pushLiteralArray(Toy_LiteralArray* array, Toy_Literal literal) {
|
int Toy_pushLiteralArray(Toy_LiteralArray* array, Toy_Literal literal) {
|
||||||
if (array->capacity < array->count + 1) {
|
if (array->capacity < array->count + 1) {
|
||||||
|
|||||||
@@ -73,7 +73,9 @@ static void adjustEntryCapacity(Toy_private_dictionary_entry** dictionaryHandle,
|
|||||||
}
|
}
|
||||||
|
|
||||||
//clear the old array
|
//clear the old array
|
||||||
|
if (oldCapacity > 0) {
|
||||||
TOY_FREE_ARRAY(Toy_private_dictionary_entry, *dictionaryHandle, oldCapacity);
|
TOY_FREE_ARRAY(Toy_private_dictionary_entry, *dictionaryHandle, oldCapacity);
|
||||||
|
}
|
||||||
|
|
||||||
*dictionaryHandle = newEntries;
|
*dictionaryHandle = newEntries;
|
||||||
}
|
}
|
||||||
@@ -133,10 +135,12 @@ void Toy_initLiteralDictionary(Toy_LiteralDictionary* dictionary) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Toy_freeLiteralDictionary(Toy_LiteralDictionary* dictionary) {
|
void Toy_freeLiteralDictionary(Toy_LiteralDictionary* dictionary) {
|
||||||
|
if (dictionary->capacity > 0) {
|
||||||
freeEntryArray(dictionary->entries, dictionary->capacity);
|
freeEntryArray(dictionary->entries, dictionary->capacity);
|
||||||
dictionary->capacity = 0;
|
dictionary->capacity = 0;
|
||||||
dictionary->contains = 0;
|
dictionary->contains = 0;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void Toy_setLiteralDictionary(Toy_LiteralDictionary* dictionary, Toy_Literal key, Toy_Literal value) {
|
void Toy_setLiteralDictionary(Toy_LiteralDictionary* dictionary, Toy_Literal key, Toy_Literal value) {
|
||||||
if (TOY_IS_NULL(key)) {
|
if (TOY_IS_NULL(key)) {
|
||||||
|
|||||||
@@ -8,10 +8,10 @@
|
|||||||
|
|
||||||
//default allocator
|
//default allocator
|
||||||
void* Toy_private_defaultMemoryAllocator(void* pointer, size_t oldSize, size_t newSize) {
|
void* Toy_private_defaultMemoryAllocator(void* pointer, size_t oldSize, size_t newSize) {
|
||||||
if (newSize == 0 && oldSize == 0) {
|
//causes issues, so just skip out with a NO-OP (DISABLED for performance reasons)
|
||||||
//causes issues, so just skip out with a NO-OP
|
// if (newSize == 0 && oldSize == 0) {
|
||||||
return NULL;
|
// return NULL;
|
||||||
}
|
// }
|
||||||
|
|
||||||
if (newSize == 0) {
|
if (newSize == 0) {
|
||||||
free(pointer);
|
free(pointer);
|
||||||
|
|||||||
@@ -10,25 +10,32 @@
|
|||||||
int currentMemoryUsed = 0;
|
int currentMemoryUsed = 0;
|
||||||
int maxMemoryUsed = 0;
|
int maxMemoryUsed = 0;
|
||||||
int memoryAllocCalls = 0;
|
int memoryAllocCalls = 0;
|
||||||
|
int memoryAllocFree = 0;
|
||||||
|
int memoryAllocRealloc = 0;
|
||||||
|
|
||||||
static void* trackerAllocator(void* pointer, size_t oldSize, size_t newSize) {
|
static void* trackerAllocator(void* pointer, size_t oldSize, size_t newSize) {
|
||||||
if (newSize == 0 && oldSize == 0) {
|
//the number of raw calls
|
||||||
|
memoryAllocCalls++;
|
||||||
|
|
||||||
//causes issues, so just skip out with a NO-OP
|
//causes issues, so just skip out with a NO-OP
|
||||||
|
if (newSize == 0 && oldSize == 0) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
memoryAllocCalls++;
|
|
||||||
|
|
||||||
//track the changes
|
//track the changes
|
||||||
currentMemoryUsed = currentMemoryUsed - oldSize + newSize;
|
currentMemoryUsed = currentMemoryUsed - oldSize + newSize;
|
||||||
maxMemoryUsed = currentMemoryUsed > maxMemoryUsed ? currentMemoryUsed : maxMemoryUsed;
|
maxMemoryUsed = currentMemoryUsed > maxMemoryUsed ? currentMemoryUsed : maxMemoryUsed;
|
||||||
|
|
||||||
if (newSize == 0) {
|
if (newSize == 0) {
|
||||||
|
//the number of frees
|
||||||
|
memoryAllocFree++;
|
||||||
free(pointer);
|
free(pointer);
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//the number of reallocations
|
||||||
|
memoryAllocRealloc++;
|
||||||
void* mem = realloc(pointer, newSize);
|
void* mem = realloc(pointer, newSize);
|
||||||
|
|
||||||
if (mem == NULL) {
|
if (mem == NULL) {
|
||||||
@@ -69,7 +76,7 @@ int main(int argc, const char* argv[]) {
|
|||||||
Toy_freeDriveDictionary();
|
Toy_freeDriveDictionary();
|
||||||
|
|
||||||
//report output
|
//report output
|
||||||
printf("Memory report: %d max bytes, %d calls\n", maxMemoryUsed, memoryAllocCalls);
|
printf("Heap Memory Report:\n\t%d max bytes\n\t%d calls to the allocator\n\t%d calls to realloc()\n\t%d calls to free()\n\t%d discrepancies\n", maxMemoryUsed, memoryAllocCalls, memoryAllocRealloc, memoryAllocFree, memoryAllocCalls - memoryAllocRealloc - memoryAllocFree);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user