From bc76d0be944a5641614485f73a51ed892e4153e2 Mon Sep 17 00:00:00 2001 From: Tim Morgan Date: Fri, 22 Feb 2019 22:29:33 -0600 Subject: [PATCH] Use realloc instead of malloc for growing strings/vectors --- Makefile | 3 +++ types.c | 10 ++-------- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index 84821d5..6ec2be6 100644 --- a/Makefile +++ b/Makefile @@ -97,6 +97,9 @@ test-self-hosted: all $(RUN_TEST_CMD) --test-timeout 30 step9_try.mal ../../self_hosted_run $(RUN_TEST_CMD) --test-timeout 30 stepA_mal.mal ../../self_hosted_run +perf: all + cd mal/tests && ../../malcc perf1.mal && ../../malcc perf2.mal && ../../malcc perf3.mal + cloc: cloc --exclude-dir='tinycc,mal' --not-match-f='hashmap.*|step.*' . diff --git a/types.c b/types.c index 3b56860..6d8eddc 100644 --- a/types.c +++ b/types.c @@ -131,9 +131,7 @@ MalType* mal_string(char *str) { void mal_grow_string(MalType *val, size_t capacity) { size_t len = strlen(val->str); assert(capacity >= len); - char *new_str = GC_MALLOC(capacity + 1); - snprintf(new_str, capacity + 1, "%s", val->str); - val->str = new_str; + val->str = GC_REALLOC(val->str, capacity + 1); val->str_cap = capacity; } @@ -237,11 +235,7 @@ void mal_vector_push(MalType *vector, MalType *value) { size_t len = mal_vector_len(vector); if (len >= capacity) { vector->vec_cap *= VECTOR_GROW_FACTOR; - MalType **new_ary = GC_MALLOC(sizeof(MalType*) * vector->vec_cap); - for (size_t i=0; ivec = new_ary; + vector->vec = GC_REALLOC(vector->vec, sizeof(MalType*) * vector->vec_cap); } vector->vec_len++; vector->vec[len] = value; -- 2.45.2