]> granicus.if.org Git - graphviz/commitdiff
gv2gxl: undo micro-optimization in 'createEdgeId'
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Fri, 20 May 2022 05:11:53 +0000 (22:11 -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 6e5ee550b0aa70ca07d091bb830e9dadeab34f41..32d0ae56a6c3fb21c5cb41485e0b0e88ec088b16 100644 (file)
@@ -242,19 +242,14 @@ static char *nodeID(gxlstate_t * stp, Agnode_t * n)
 static char *createEdgeId(gxlstate_t * stp, Agedge_t * e)
 {
     int edgeIdCounter = 1;
-    char buf[BUFSIZ];
     char *hname = nodeID(stp, AGHEAD(e));
     char *tname = nodeID(stp, AGTAIL(e));
     size_t baselen = strlen(hname) + strlen(tname) + sizeof(EDGEOP);
     size_t len = baselen + EXTRA;
-    char *bp;
     char *endp;                        /* where to append ':' and number */
     char *rv;
 
-    if (len <= BUFSIZ)
-       bp = buf;
-    else
-       bp = N_NEW(len, char);
+    char *bp = N_NEW(len, char);
     endp = bp + (baselen - 1);
 
     sprintf(bp, "%s%s%s", tname, EDGEOP, hname);
@@ -263,8 +258,7 @@ static char *createEdgeId(gxlstate_t * stp, Agedge_t * e)
     }
 
     rv = addid(stp->idList, bp);
-    if (bp != buf)
-       free(bp);
+    free(bp);
     return rv;
 }