From d831ad34092fa8a43b26caa6f56188bee2d18042 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Mon, 21 Apr 2025 13:10:44 -0500 Subject: [PATCH] perf(queue): improved performance of queue by changing to dynamic list instead of doubly linked list --- Include/Fledasty/Core/Queue.h | 16 ++++---- Src/Core/Queue.c | 71 ++++++++++++++--------------------- Tests/Main.c | 11 +----- 3 files changed, 37 insertions(+), 61 deletions(-) diff --git a/Include/Fledasty/Core/Queue.h b/Include/Fledasty/Core/Queue.h index b0e6a34..44aad6b 100644 --- a/Include/Fledasty/Core/Queue.h +++ b/Include/Fledasty/Core/Queue.h @@ -27,19 +27,17 @@ #include "../Utils/Error.h" -typedef struct FledastyQueueNode { - void *value; - struct FledastyQueueNode *previous, *next; -} FledastyQueueNode; - typedef struct { - size_t size; - FledastyQueueNode *start, *end; + size_t size, capacity; + size_t head, tail; + + size_t element_byte_size; + unsigned char *buffer; } FledastyQueue; -FledastyError fledasty_queue_initialize(FledastyQueue *new_queue); +FledastyError fledasty_queue_initialize(FledastyQueue *new_queue, const size_t element_byte_size); FledastyError fledasty_queue_destroy(FledastyQueue *current_queue); -FledastyError fledasty_queue_push(FledastyQueue *current_queue, void *value, const size_t size); +FledastyError fledasty_queue_push(FledastyQueue *current_queue, void *value); #endif \ No newline at end of file diff --git a/Src/Core/Queue.c b/Src/Core/Queue.c index 6f3a41a..12de23e 100644 --- a/Src/Core/Queue.c +++ b/Src/Core/Queue.c @@ -12,9 +12,10 @@ * limitations under the License. * * ----------------------------------------------------------------------------- - * File: Main.c + * File: Queue.c * Description: - * Executes all tests. + * This file contains the functions for modifying the queue. It includes functions + * to Push, Pop, Peek and check if empty. * * Author: Mineplay * ----------------------------------------------------------------------------- @@ -22,17 +23,26 @@ #include "../../Include/Fledasty/Core/Queue.h" #include +#include #include #include -FledastyError fledasty_queue_initialize(FledastyQueue *new_queue) { +FledastyError fledasty_queue_initialize(FledastyQueue *new_queue, const size_t element_byte_size) { if (new_queue == NULL) { return FLEDASTY_ERROR_INVALID_POINTER; } new_queue->size = 0; - new_queue->start = NULL; - new_queue->end = NULL; + new_queue->capacity = 10; + new_queue->element_byte_size = element_byte_size; + + new_queue->buffer = (unsigned char*)hallocy_malloc(new_queue->capacity * element_byte_size); + if (new_queue->buffer == NULL) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + + new_queue->head = 0; + new_queue->tail = 0; return FLEDASTY_ERROR_NONE; } @@ -42,55 +52,30 @@ FledastyError fledasty_queue_destroy(FledastyQueue *current_queue) { return FLEDASTY_ERROR_INVALID_POINTER; } - FledastyQueueNode *previousNode = NULL; - FledastyQueueNode *current_node = current_queue->start; - while (current_node != NULL) { - previousNode = current_node; - current_node = current_node->next; - - HallocyError error = hallocy_free(previousNode->value); - if (error != HALLOCY_ERROR_NONE) { - return FLEDASTY_ERROR_FAILED_ALLOCATION; - } - - error = hallocy_free(previousNode); - if (error != HALLOCY_ERROR_NONE) { - return FLEDASTY_ERROR_FAILED_ALLOCATION; - } + HallocyError result = hallocy_free(current_queue->buffer); + if (result != HALLOCY_ERROR_NONE) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; } - current_queue->size = 0; - current_queue->start = NULL; - current_queue->end = NULL; - + current_queue->buffer = NULL; return FLEDASTY_ERROR_NONE; } -FledastyError fledasty_queue_push(FledastyQueue *current_queue, void *value, const size_t size) { +FledastyError fledasty_queue_push(FledastyQueue *current_queue, void *value) { if (current_queue == NULL || value == NULL) { return FLEDASTY_ERROR_INVALID_POINTER; } - FledastyQueueNode *new_queue_node = (FledastyQueueNode*)hallocy_malloc(sizeof(FledastyQueueNode)); - if (new_queue_node == NULL) { - return FLEDASTY_ERROR_FAILED_ALLOCATION; + if (current_queue->capacity <= current_queue->size + 1) { + current_queue->capacity = current_queue->capacity + (current_queue->capacity / 2); + current_queue->buffer = (unsigned char*)hallocy_realloc(current_queue->buffer, current_queue->capacity * current_queue->element_byte_size); + + if (current_queue->buffer == NULL) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } } - new_queue_node->value = (FledastyQueueNode*)hallocy_malloc(size); - hallocy_copy_memory(new_queue_node->value, value, size); - - new_queue_node->next = NULL; - if (current_queue->start == NULL) { - current_queue->start = new_queue_node; - current_queue->end = new_queue_node; - - new_queue_node->previous = NULL; - } else { - new_queue_node->previous = current_queue->end; - - current_queue->end->next = new_queue_node; - current_queue->end = new_queue_node; - } + hallocy_copy_memory(current_queue->buffer + (current_queue->size * current_queue->element_byte_size), value, current_queue->element_byte_size); current_queue->size += 1; return FLEDASTY_ERROR_NONE; diff --git a/Tests/Main.c b/Tests/Main.c index 4a0fac6..f07647b 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -24,17 +24,10 @@ int main() { FledastyQueue test_queue; - fledasty_queue_initialize(&test_queue); + fledasty_queue_initialize(&test_queue, sizeof(int)); - int value = 10; for (int i = 0; i < 10; i += 1) { - fledasty_queue_push(&test_queue, &i, sizeof(int)); - } - - FledastyQueueNode *current_node = test_queue.start; - for (int i = 0; i < test_queue.size; i++) { - printf("Element %d\n", *((int*)current_node->value)); - current_node = current_node->next; + fledasty_queue_push(&test_queue, &i); } fledasty_queue_destroy(&test_queue);