perf(queue): improved performance of queue by changing to dynamic list instead of doubly linked list

This commit is contained in:
Mineplay 2025-04-21 13:10:44 -05:00
parent 04744bb890
commit d831ad3409
3 changed files with 37 additions and 61 deletions

View file

@ -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

View file

@ -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 <Hallocy/Core/Allocator.h>
#include <Hallocy/Core/Memory.h>
#include <Hallocy/Utils/Error.h>
#include <string.h>
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;

View file

@ -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);