From: Matthew Fernandez Date: Sun, 19 Dec 2021 19:30:03 +0000 (-0800) Subject: dijkstra: remove an allocator optimization X-Git-Tag: 3.0.0~120^2~3 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6469d01baadaa1503e9f69951ba06c1a73e41b6b;p=graphviz dijkstra: remove an allocator optimization This function was using a static local for the index array under the assumption that resizing an existing dynamically allocated buffer is cheaper than allocating a new one. This is generally not true in this use case. Calling `realloc` here was (incorrectly) telling the allocator the caller needed to retain the prior content of the array. In other words, this was actually a _deoptimization_. Undoing this and letting the allocator more accurately perceive that this is a transient array allocation results in (1) more efficient behavior and (2) less unnecessarily retained memory. --- diff --git a/lib/neatogen/dijkstra.c b/lib/neatogen/dijkstra.c index 377b35326..2c5011b07 100644 --- a/lib/neatogen/dijkstra.c +++ b/lib/neatogen/dijkstra.c @@ -17,7 +17,7 @@ ******************************************/ - +#include #include #include #include @@ -140,9 +140,8 @@ void dijkstra(int vertex, vtx_data * graph, int n, DistType * dist) heap H; int closestVertex, neighbor; DistType closestDist, prevClosestDist = MAX_DIST; - static int *index; - index = realloc(index, n * sizeof(int)); + int *index = gcalloc(n, sizeof(int)); /* initial distances with edge weights: */ for (i = 0; i < n; i++) @@ -172,6 +171,7 @@ void dijkstra(int vertex, vtx_data * graph, int n, DistType * dist) if (dist[i] == MAX_DIST) /* 'i' is not connected to 'vertex' */ dist[i] = prevClosestDist + 10; freeHeap(&H); + free(index); } /* Dijkstra bounded to nodes in *unweighted* radius */