From: Matthew Fernandez Date: Sat, 6 Mar 2021 02:16:17 +0000 (-0800) Subject: remove intermediate string construction altogether in dot_polygon X-Git-Tag: 2.47.1~55^2~16 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ab22da5d5e557fcd2c21130f2758ba052d7f4835;p=graphviz remove intermediate string construction altogether in dot_polygon There's no need for this and we can achieve it in one (well two) shot. The better way to do this would be with an in-memory expanding buffer (like open_memstream), but this change is at least an improvement over the previous code. --- diff --git a/cmd/gvmap/make_map.c b/cmd/gvmap/make_map.c index 4e2ed2631..4382cb9a6 100644 --- a/cmd/gvmap/make_map.c +++ b/cmd/gvmap/make_map.c @@ -313,8 +313,7 @@ static void dot_polygon(char **sbuff, int *len, int *len_max, int np, float *xp, int fill, int close, char *cstring){ int i; int ret = 0; - char swidth[10000]; - size_t len_swidth; + size_t len_swidth = 0; if (np > 0){ /* figure out the size needed */ @@ -324,8 +323,8 @@ static void dot_polygon(char **sbuff, int *len, int *len_max, int np, float *xp, assert(line_width >= 0); if (line_width > 0){ len_swidth = (size_t)snprintf(NULL, 0, "%f", line_width); - sprintf(swidth,"S %zu -setlinewidth(%f)",len_swidth+14, line_width); - ret += snprintf(NULL, 0, " c %zu -%s %s L %d ", strlen(cstring), cstring, swidth, np); + ret += snprintf(NULL, 0, " c %zu -%s S %zu -setlinewidth(%f) L %d ", + strlen(cstring), cstring, len_swidth + 14, line_width, np); } else { ret += snprintf(NULL, 0, " c %zu -%s L %d ", strlen(cstring), cstring, np); } @@ -345,7 +344,8 @@ static void dot_polygon(char **sbuff, int *len, int *len_max, int np, float *xp, ret = sprintf(&((*sbuff)[*len]), " c %zu -%s C %zu -%s P %d ", strlen(cstring), cstring, strlen(cstring), cstring, np); } else { if (line_width > 0){ - ret = sprintf(&((*sbuff)[*len]), " c %zu -%s %s L %d ", strlen(cstring), cstring, swidth, np); + ret = sprintf(&((*sbuff)[*len]), " c %zu -%s S %zu -setlinewidth(%f) L %d ", + strlen(cstring), cstring, len_swidth + 14, line_width, np); } else { ret = sprintf(&((*sbuff)[*len]), " c %zu -%s L %d ", strlen(cstring), cstring, np); }