free() was called way too often before, this patch reduces it
significantly. Minor speed-up here too, I don't mention it individually
since numbers are within noise margins.
2011-08-22 Dimitrios Apostolou <ji...@gmx.net>
* graphds.h (struct graph): Added edge_pool as a pool for
allocating edges.
* graphds.c (new_graph): Initialise edge_pool.
(add_edge): Allocate edge from edge_pool rather than with malloc.
(free_graph): Instead of iterating across the graph freeing edges,
just destroy the edge_pool.
=== modified file 'gcc/graphds.c'
--- gcc/graphds.c 2009-11-25 10:55:54 +0000
+++ gcc/graphds.c 2011-08-19 16:44:41 +0000
@@ -62,7 +62,8 @@ new_graph (int n_vertices)
g->n_vertices = n_vertices;
g->vertices = XCNEWVEC (struct vertex, n_vertices);
-
+ g->edge_pool = create_alloc_pool ("edge_pool",
+ sizeof (struct graph_edge), 32);
return g;
}
@@ -71,7 +72,7 @@ new_graph (int n_vertices)
struct graph_edge *
add_edge (struct graph *g, int f, int t)
{
- struct graph_edge *e = XNEW (struct graph_edge);
+ struct graph_edge *e = (struct graph_edge *) pool_alloc (g->edge_pool);
struct vertex *vf = &g->vertices[f], *vt = &g->vertices[t];
@@ -326,19 +327,7 @@ for_each_edge (struct graph *g, graphds_
void
free_graph (struct graph *g)
{
- struct graph_edge *e, *n;
- struct vertex *v;
- int i;
-
- for (i = 0; i < g->n_vertices; i++)
- {
- v = &g->vertices[i];
- for (e = v->succ; e; e = n)
- {
- n = e->succ_next;
- free (e);
- }
- }
+ free_alloc_pool (g->edge_pool);
free (g->vertices);
free (g);
}
=== modified file 'gcc/graphds.h'
--- gcc/graphds.h 2009-02-20 15:20:38 +0000
+++ gcc/graphds.h 2011-08-19 16:44:41 +0000
@@ -18,6 +18,10 @@ You should have received a copy of the G
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
+
+#include "alloc-pool.h"
+
+
/* Structure representing edge of a graph. */
struct graph_edge
@@ -44,10 +48,10 @@ struct vertex
struct graph
{
- int n_vertices; /* Number of vertices. */
- struct vertex *vertices;
- /* The vertices. */
- htab_t indices; /* Fast lookup for indices. */
+ int n_vertices; /* Number of vertices. */
+ struct vertex *vertices; /* The vertices. */
+ htab_t indices; /* Fast lookup for indices. */
+ alloc_pool edge_pool; /* Pool for allocating edges. */
};
struct graph *new_graph (int);