]> granicus.if.org Git - graphviz/commitdiff
gv2gxl: undo micro-optimization in 'writeHdr'
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Fri, 20 May 2022 05:16:14 +0000 (22:16 -0700)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Mon, 23 May 2022 03:47:58 +0000 (20:47 -0700)
Allocation within this function chose between an on-stack buffer and a
heap-allocated buffer based on the length of the input string. With a modern
performant allocator this is often a de-optimization. This change switches to
unconditionally using a heap-allocated buffer, simplifying the code.

cmd/tools/gv2gxl.c

index 32d0ae56a6c3fb21c5cb41485e0b0e88ec088b16..c553332eb8e2a480cb291628ee33afc87ffd6b98 100644 (file)
@@ -440,9 +440,6 @@ writeHdr(gxlstate_t * stp, Agraph_t * g, FILE * gxlFile, int top)
     char *name;
     char *kind;
     char *uniqueName;
-    char buf[BUFSIZ];
-    char *bp;
-    char *dynbuf = 0;
     size_t len;
 
     Level++;
@@ -457,11 +454,7 @@ writeHdr(gxlstate_t * stp, Agraph_t * g, FILE * gxlFile, int top)
        /* this must be anonymous graph */
 
        len = strlen(name) + sizeof("N_");
-       if (len <= BUFSIZ)
-           bp = buf;
-       else {
-           bp = dynbuf = N_NEW(len, char);
-       }
+       char *bp = N_NEW(len, char);
        sprintf(bp, "N_%s", name);
        if (idexists(stp->idList, bp) || !legalGXLName(bp)) {
            bp = createNodeId(stp->idList);
@@ -472,7 +465,7 @@ writeHdr(gxlstate_t * stp, Agraph_t * g, FILE * gxlFile, int top)
 
        tabover(gxlFile);
        fprintf(gxlFile, "<node id=\"%s\">\n", bp);
-       free(dynbuf);
+       free(bp);
        Level++;
     } else {
        Tailport = agattr(g, AGEDGE, "tailport", NULL);