Use the updated free_json and profile it

This commit is contained in:
Abdelrahman Said 2023-07-12 00:45:29 +01:00
parent 0360a2da35
commit 2d74f02138
3 changed files with 73 additions and 36 deletions

View File

@ -15,6 +15,7 @@ enum profiler_ids {
PROFILER_ID_HAVERSINE_DISTANCE, PROFILER_ID_HAVERSINE_DISTANCE,
PROFILER_ID_HAVERSINE_AVG, PROFILER_ID_HAVERSINE_AVG,
PROFILER_ID_TEAR_DOWN, PROFILER_ID_TEAR_DOWN,
PROFILER_ID_FREE_JSON,
COUNT_PROFILER_IDS, COUNT_PROFILER_IDS,
}; };

View File

@ -1,5 +1,7 @@
#include "json/json_entities.h" #include "json/json_entities.h"
#include "aliases.h" #include "aliases.h"
#include "profiler/ids.h"
#include "profiler/timer.h"
#include "json/dstring.h" #include "json/dstring.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -110,58 +112,90 @@ void print_json(const jentity_t *entity, u32 indent) {
} }
} }
void free_json(jentity_t **entity) { void free_json(jentity_t **root) {
if (!(*entity)) { if (!(*root)) {
return; return;
} }
jentity_t *entt_ptr = *entity; SAMPLE_START(PROFILER_ID_FREE_JSON, "FREE JSON");
jentity_t *current = *root;
jentity_t *temp = NULL;
dstr_t *key = NULL; dstr_t *key = NULL;
const jval_t *value = NULL; jval_t *value = NULL;
if (entt_ptr->type == JENTITY_SINGLE) { while (current) {
value = &(entt_ptr->value); if (current->parent) {
} else { // Move the beginning pointer of the collection to the next child
key = entt_ptr->pair.key;
value = &(entt_ptr->pair.value);
}
if (key) { // TODO (Abdelrahman): This part gets repeated for some elements. Try to
dstr_free(&(entt_ptr->pair.key)); // avoid that repetition
}
if (!value) { jentity_t *parent = current->parent;
return; jcoll_t *collection = NULL;
}
switch (value->type) { if (parent->type == JENTITY_SINGLE) {
case JVAL_COLLECTION: collection = parent->value.collection;
if (!(value->collection)) { } else {
collection = parent->pair.value.collection;
}
if (collection) {
collection->begin = current->next;
}
}
if (current->type == JENTITY_SINGLE) {
key = NULL;
value = &(current->value);
} else {
key = current->pair.key;
value = &(current->pair.value);
}
if (key) {
dstr_free(&(current->pair.key));
}
if (!value) {
break; break;
} }
if (value->collection->begin) { if (value->type == JVAL_COLLECTION) {
free_json(&(value->collection->begin)); if (!(value->collection->begin)) {
// Once all children of the collection has been freed, free the memory
// allocated to the collection and the entity that holds it
free(value->collection);
temp = current;
current = current->next != NULL ? current->next : current->parent;
free(temp);
temp = NULL;
continue;
}
current = value->collection->begin;
} else {
if (value->type == JVAL_STRING) {
dstr_free(&(value->string));
}
temp = current;
current = current->next != NULL ? current->next : current->parent;
free(temp);
temp = NULL;
} }
free(value->collection);
break;
case JVAL_STRING:
dstr_free(&(entt_ptr->pair.value.string));
break;
default:
break;
} }
if (entt_ptr->next) { *root = NULL;
free_json(&(entt_ptr->next));
}
free(*entity); SAMPLE_END(PROFILER_ID_FREE_JSON);
*entity = NULL;
} }
jcoll_t *get_collection_from_entity(const jentity_t *entity) { jcoll_t *get_collection_from_entity(const jentity_t *entity) {

View File

@ -114,6 +114,8 @@ int main(int argc, char *argv[]) {
free(point_pairs); free(point_pairs);
free_json(&root);
SAMPLE_END(PROFILER_ID_TEAR_DOWN); SAMPLE_END(PROFILER_ID_TEAR_DOWN);
PROFILE_END; PROFILE_END;