From 71ba1f945428cfa95eabac971adfe9d6bf274139 Mon Sep 17 00:00:00 2001 From: venko Date: Fri, 7 May 2021 14:07:57 -0700 Subject: [PATCH] Minimizes memory footprint of id-tree search --- src/identcheck.c | 25 +++++++++++++++---------- src/main.c | 14 +------------- 2 files changed, 16 insertions(+), 23 deletions(-) diff --git a/src/identcheck.c b/src/identcheck.c index a05543d..9cb69f7 100644 --- a/src/identcheck.c +++ b/src/identcheck.c @@ -163,26 +163,31 @@ IdNode* FindId(IdNode *root, NodeType targetType, char *targetName) { IdNode **frontier = (IdNode**)malloc(sizeof(IdNode*)); frontier[0] = root; uint32_t frontierCount = 1; - uint32_t cursor = 0; - while (frontierCount > 0 && cursor < frontierCount) { - IdNode *current = frontier[cursor]; + while (frontierCount > 0) { + IdNode *current = frontier[0]; if (current->type == targetType && strcmp(current->name, targetName) == 0) { result = current; break; } - int newSize = frontierCount + current->childCount; - frontier = realloc(frontier, sizeof(IdNode*) * newSize); uint32_t i; - for (i = 0; i < current->childCount; i++) { - frontier[frontierCount + i] = current->children[i]; + for(i = 1; i < frontierCount; i++) { + frontier[i-1] = frontier[i]; } - frontierCount += current->childCount; - cursor += 1; + + size_t newSize = frontierCount + current->childCount - 1; + if (frontierCount != newSize) { + frontier = realloc(frontier, sizeof(IdNode*) * newSize); + } + for (i = 0; i < current->childCount; i++) { + frontier[frontierCount + i - 1] = current->children[i]; + } + + frontierCount = newSize; } free(frontier); return result; -} \ No newline at end of file +} diff --git a/src/main.c b/src/main.c index 03985fc..691c1db 100644 --- a/src/main.c +++ b/src/main.c @@ -65,21 +65,9 @@ int main(int argc, char *argv[]) } else { - { + if (parseVerbose) { IdNode *idTree = MakeIdTree(rootNode, NULL); PrintIdTree(idTree, /*tabCount=*/0); - printf("\n"); - - printf("Searching for a variable named 'lol' and printing its ancestors\n"); - IdNode *deepest = FindId(idTree, Variable, "lol"); - PrintAncestors(deepest); - printf("\n"); - - printf("Now searching for the same variable but with the caveat that it" - " must be inside an if-block, and printing its ancestors\n"); - deepest = FindId(idTree, LexicalScope, "if"); - deepest = FindId(deepest, Variable, "lol"); - PrintAncestors(deepest); } exitCode = Codegen(rootNode, optimizationLevel); }