From 2d74f02138023d79cb1591da103bbb32d70fe6e0 Mon Sep 17 00:00:00 2001 From: Abdelrahman Said Date: Wed, 12 Jul 2023 00:45:29 +0100 Subject: [PATCH] Use the updated free_json and profile it --- haversine_02/include/profiler/ids.h | 1 + haversine_02/src/json/json_entities.c | 106 +++++++++++++++++--------- haversine_02/src/processor/main.cpp | 2 + 3 files changed, 73 insertions(+), 36 deletions(-) diff --git a/haversine_02/include/profiler/ids.h b/haversine_02/include/profiler/ids.h index b9c698a..2269361 100644 --- a/haversine_02/include/profiler/ids.h +++ b/haversine_02/include/profiler/ids.h @@ -15,6 +15,7 @@ enum profiler_ids { PROFILER_ID_HAVERSINE_DISTANCE, PROFILER_ID_HAVERSINE_AVG, PROFILER_ID_TEAR_DOWN, + PROFILER_ID_FREE_JSON, COUNT_PROFILER_IDS, }; diff --git a/haversine_02/src/json/json_entities.c b/haversine_02/src/json/json_entities.c index f95b7ef..9989ce9 100644 --- a/haversine_02/src/json/json_entities.c +++ b/haversine_02/src/json/json_entities.c @@ -1,5 +1,7 @@ #include "json/json_entities.h" #include "aliases.h" +#include "profiler/ids.h" +#include "profiler/timer.h" #include "json/dstring.h" #include #include @@ -110,58 +112,90 @@ void print_json(const jentity_t *entity, u32 indent) { } } -void free_json(jentity_t **entity) { - if (!(*entity)) { +void free_json(jentity_t **root) { + if (!(*root)) { 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; - const jval_t *value = NULL; + jval_t *value = NULL; - if (entt_ptr->type == JENTITY_SINGLE) { - value = &(entt_ptr->value); - } else { - key = entt_ptr->pair.key; - value = &(entt_ptr->pair.value); - } + while (current) { + if (current->parent) { + // Move the beginning pointer of the collection to the next child - if (key) { - dstr_free(&(entt_ptr->pair.key)); - } + // TODO (Abdelrahman): This part gets repeated for some elements. Try to + // avoid that repetition - if (!value) { - return; - } + jentity_t *parent = current->parent; + jcoll_t *collection = NULL; - switch (value->type) { - case JVAL_COLLECTION: - if (!(value->collection)) { + if (parent->type == JENTITY_SINGLE) { + collection = parent->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; } - if (value->collection->begin) { - free_json(&(value->collection->begin)); + if (value->type == JVAL_COLLECTION) { + 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) { - free_json(&(entt_ptr->next)); - } + *root = NULL; - free(*entity); - *entity = NULL; + SAMPLE_END(PROFILER_ID_FREE_JSON); } jcoll_t *get_collection_from_entity(const jentity_t *entity) { diff --git a/haversine_02/src/processor/main.cpp b/haversine_02/src/processor/main.cpp index dfb08f4..66125ba 100644 --- a/haversine_02/src/processor/main.cpp +++ b/haversine_02/src/processor/main.cpp @@ -114,6 +114,8 @@ int main(int argc, char *argv[]) { free(point_pairs); + free_json(&root); + SAMPLE_END(PROFILER_ID_TEAR_DOWN); PROFILE_END;